ログイン認証したユーザーの設定について

1.目的

ユーザー認証を行なった後にホーム画面を表示させるようにする。
ユーザーが投稿したものだけを表示させるようにする。

2.必要なもの

なし

3.手順

○ユーザー認証を行なった後にホーム画面を表示

①web.phpを以下のように変更していきます。

<?php
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'ItemsController@index');
Route::resource('/item','ItemsController',['only' =>['create','store','show','destroy','edit','update']]);

Auth::routes();
//以下変更
Route::get('/home','ItemsController@index')->name('home');
//ここまで

ログイン/ユーザー登録後に表示するページを変更しています。
ここではログイン/ユーザー登録後に登録・一覧画面を表示するようにしています。

○ユーザーが投稿したものだけを表示

①ユーザーid を登録できるようにitemsテーブルのマイグレーションファイルを以下のように変更していきます。


class CreateItemsTable extends Migration
{
   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   {
       Schema::create('items', function (Blueprint $table) {
           $table->id();
           //以下追記
           $table->bigInteger('user_id');
           //ここまで
           $table->string('name');
           $table->string('url');
           $table->text('text');
           $table->timestamps();
       });
   }
   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::dropIfExists('items');
   }
}

②terminalで以下のコマンドを実行しテーブルをリセットさせます。
また、データテーブルの再構築も行います。

%php artisan migrate:reset
%php artisan migrate

③ItemsControllerの@indexを以下のように変更していきます。

//省略
public function index()
   {
       $items = Item::where('user_id',Auth::user()->id)->orderBy('created_at','desc')->paginate(5);
       return view('items.index',compact('items'));
       
   }
//省略   

ログインしているユーザーが投稿したものだけを表示させるようにしています。

④ItemsControllerの@storeを以下のように変更します。

public function store(Request $request)
   {
       $validator = Validator::make($request->all(),[
           'name' => 'required',
           'url' => 'required',
           'text' => 'required',
       ]);
       if($validator->fails()){
           return redirect('item/create')
               ->withInput()
               ->withErrors($validator);
       }
       $items = new Item;
       //以下追記
       $items->user_id = Auth::user()->id;
       //ここまで
       $items->name = $request->name;
       $items->url = $request->url;
       $items->text = $request->text;
       $items->save();
       return redirect('/');
   }

新しく作ったuser_idカラムに投稿しているユーザーのidを登録するようにしています。

⑤ItemsControllerの@editを以下のように変更していきます。

public function edit($id){
       $items = Item::where('user_id',Auth::user()->id)->find($id);
       return view('items.edit')->with('item', $items);
   }

編集画面で表示させるすでに登録されている情報は誰の情報なのか見て取得できるようにしています。

⑥ItemsControllerの@updateを以下のように変更していきます。

public function update(Request $request,$id){
       $validator = Validator::make($request->all(),[
           'name' => 'required',
           'url' => 'required',
           'text' => 'required',
       ]);
       if($validator->fails()){
           return redirect('item/'.$id.'/edit')
               ->withInput()
               ->withErrors($validator);
       }
       //以下変更
       $items = Item::where('user_id',Auth::user()->id)->find($id);
       //ここまで
       $items->name = $request->name;
       $items->url = $request->url;
       $items->text = $request->text;
       $items->save();
       return redirect('/');
   }

どのユーザーがどの投稿したものなのか見て編集を行なっています。

4.まとめ

これにて完成です。
ありがとうございました!!


この記事が気に入ったらサポートをしてみませんか?