LoginSignup
0
0

More than 3 years have passed since last update.

Laravel:検索機能をつける方法

Last updated at Posted at 2020-10-28

【概要】

1.結論

2.どのようにコーディングするか

3.開発環境

1.結論

$変数 = モデルクラス :: where(カラム名 , $request->request)->all or find or first();

をつける!

2.どのようにコーディングするか

app/Http/Controllers/PersonController.php
    public function search (Request $request)
    {
        $item = User::where('name',$request->input)->first(); #---❶
        $param = ['input' => $request->input, 'item' => $item]; #---❷
        return view('user.find', $param); #---❸
    }


❶:Userテーブルで、whereを使用し、nameカラムを指定しています。Requestは、クライアントから送られてくる情報を含んでおり$request->inputでファイル以外のデータを受け取っています。firstは条件に引っかかった最初だけを拾っています。
❷:paramの中にinput, itemを入れて連想配列にしています。
❸:user/findはgetした時にsearchはpostで処理しています。getの際に、paramの情報も入っています。

上記で記載したものをコントローラーに処理させたいので、ルーティングします。

routes/web.php
Route::get('user/find' , 'App\Http\Controllers\PersonController@find' );
Route::post('user/find' , 'App\Http\Controllers\PersonController@search' );

*部分検索についてはしっかり学習してから、また投稿します。

3.開発環境

PHP 7.4.10
Laravel 8.9
Apache 2.4.41
Mysql 5.6.47
Sequl Pro 1.1.2

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0