yuuki0205nano
@yuuki0205nano (よしおか)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

ユーザー情報の表示方法がわからない

解決したいこと・やりたいこと

1.新規登録しているuserのnameを出力する
2.データベースでmemosのchecklistが1だった時はカウントして結果を表示する

2の部分のやり方がいまいちわからないです

該当するソースコード

AccountController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use \App\Memo;
use \App\User;

class AccountController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }


    public function newpost()
    {
        $user = \Auth::user();

        return view('newpost',compact('user'));
    }

    public function store(Request $request)
    {
        $data = $request->all();
        //dd($data);
        $image = $request->file('image');
        //dd($image);
        if($request->hasFile('image')){
            $path = \Storage::put('/public', $image);
            $path = explode('/', $path);
        }else{
            $path = null;
        }
        // POSTされたデータをDB(memosテーブル)に挿入
        // MEMOモデルにDBへ保存する命令を出す
        $memo_id = Memo::insertGetId(
            ['title' => $data['title'],
            'content' => $data['content'],
            'user_id' => $data['user_id'],  
            'image' => $path[1],
            'url' => $data['url'],
            'status' => 1]);

        // リダイレクト処理
        return redirect()->route('home');
    }

    public function edit($id){
        $user = \Auth::user();

        $memo = Memo::where('status','1')->where('id',$id)->where('user_id',$user['id'])->first();

        $memos = Memo::where('user_id',$user['id'])->where('status',1)->orderBy('updated_at','DESC')->get();


        return view ('edit',compact('memo','user','memos'))->with('flash_edit', '投稿が完了しました');

    }

    public function update(Request $request , $id){
        $inputs = $request->all();
        //dd($inputs);
        Memo::where('id',$id)->update(['title' => $inputs['title'],
                                        'content' => $inputs['content'],
                                       'image' => $inputs['image'],
                                       'url' => $inputs['url']
                                    ]);

        return redirect()->route('home');
    }

    public function delete(Request $request , $id){
        $inputs = $request->all();
        //dd($inputs);
        Memo::where('id',$id)->update([ 'status' => 2 ]);

        return redirect()->route('home')->with('flash-delete','メモの削除が完了しました');
    }

    public function userlist(){
        $userlist = User::get();
        $memolists = Memo::where('status','1')->where('checklist','1')->get();
        $memoyet = Memo::where('status','1')->where('checklist','0')->get();
        //dd($userlist);
        //dd($memolists);
        return view('userlist',compact('userlist','memolists','memoyet'));        
    }    

    public function userdetail($id){
        $user = \Auth::user();
        //dd($user);
        return view('userdetail',compact('user'));
    }    
}

userlist.blade.php


@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <table class="table">

                <h1>test</h1>
                    <thead class = "table-dark">
                        <tr>
                        <th scope="col">ユーザー名前</th>
                        <th scope="col">研修済</th>
                        <th scope="col">研修未完</th>
                        </tr>
                    </thead>
                    <tbody>
                    @foreach($userlist as $list)
                    <tr>
                    <td>
                        <a href = "/userdetail/{{ $list['id'] }}" >
                                {{ $list['name'] }}
                            </a>
                    </td>

                    <th scope="row">

                    <?php $i=0; ?>
                    @foreach($memolists as $memolist)
                        <?php $i++; ?>
                    @endforeach
                    <?php echo $i; ?>
                    </th>

                    <th scope="row">
                    <?php $i=0; ?>
                    @foreach($memoyet as $yet)
                        <?php $i++; ?>
                    @endforeach
                    <?php echo $i; ?>
                    </th>


                        </tr>
                        @endforeach

                    </tbody>

                    </table>

                </div>
    </div>
</div>
@endsection


web.php


<?php

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();



//一般ユーザー
Route::group(['middleware' => ['auth', 'can:user-higher']], function () {
    Route::get('/home', 'HomeController@index')->name('home');
    Route::get('/content/{id}', 'HomeController@content')->name('content');
    Route::post('/checklist/{id}', 'HomeController@checklist')->name('checklist');
  });

  // 管理者以上
  Route::group(['middleware' => ['auth', 'can:admin-higher']], function () {
    Route::get('/newpost', 'AccountController@newpost')->name('newpost');
    Route::get('/create', 'AccountController@create')->name('create');
    Route::post('/store', 'AccountController@store')->name('store');
    Route::get('/edit/{id}', 'AccountController@edit')->name('edit');
    Route::post('/update/{id}', 'AccountController@update')->name('update');
    Route::post('/delete/{id}', 'AccountController@delete')->name('delete');
    Route::get('/userlist', 'AccountController@userlist')->name('userlist');
    Route::get('/userdetail/{id}', 'AccountController@userdetail')->name('userdetail');

  });
0

1Answer

Your answer might help someone💌