0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

laravelでscopeを使って複数のカラムを検索

Last updated at Posted at 2021-12-20

#会員管理画面

index.php
@extends('admin.layout')
@section('content')
    <div class="m-5">
        <h2>会員管理</h2>
    </div>
    <div class="card m-5">
        {!! Form::open(['url' => route('member_users'), 'method' => 'get']) !!}
        <div class="card-body">
            <div class="row g-3">
                <div class="col-md-6">
                    {{Form::label('name','ユーザー名')}}
                    {{Form::text('name', null, ['class' => 'form-control', 'id' => 'name'])}}
                </div>
                <div class="col-md-6">
                    {{Form::label('email','メールアドレス')}}
                    {{Form::text('email', null, ['class' => 'form-control', 'id' => 'email'])}}
                </div>
            </div>
        </div>
        <div class="card-footer">
            <div class="text-center">
                {{Form::submit(' 検索 ', ['class'=>'btn btn-primary'])}}
            </div>
        </div>
        {{Form::close()}}
    </div>
    <div class="m-5">
        {{ $users->appends(request()->input())->links('pagination.admin') }}
    </div>
    <div class="card m-5">
        <div class="card-body">
            <div class="mt-3">
                <table class="table table-bordered">
                    <thead>
                    <tr>
                        <th><div class="text-center">ID</div></th>
                        <th scope="col"> ユーザー名</th>
                        <th scope="col"> メールアドレス</th>
                        <th scope="col"><div class="text-right">登録日時 </div>
                        </th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach ($users as $user)
                        <tr>
                            <td><div class="text-center">{{ $user->id }}</div></td>
                            <td> {{ $user->name }}</td>
                            <td> {{ $user->email }}</td>
                            <td><div class="text-right">{{ $user->created_at->format('Y/m/d H:i ') }}</div></td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
                @if($users->isNotEmpty())
                @elseif(request('name') || request('email')!==null)
                    <p class="text-center">検索結果に基づく会員ユーザーは見つかりませんでした</p>
                @else
                    <p class="text-center">登録されている会員ユーザーはまだいません</p>
                @endif
            </div>
        </div>
    </div>
@endsection

#モデル

以下を追加

User.php
use Illuminate\Database\Eloquent\Builder;

scopeを定義するにはscopeを名前の初めに付ける必要がある.

public function scope+名前($query, $引数)
    {
      return $query->where('検索するもの','検索条件');
    }
User.php
パターン1
    public function scopeNamesearch($query, $name)
    {
        return $query->where('name', 'like', '%' . $name . '%');

    }

    public function scopeEmailsearch($query, $email)
    {
        return $query->where('email', 'like', '%' . $email . '%');
    }

もしくは

User.php
パターン2
    /**
     * Like検索用のスコープ
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param string $attribute
     * @param string|null $value
     * @return \Illuminate\Database\Eloquent\Builder 検索結果
     */
    public function scopeWhereLike(Builder $query, string $attribute, ?string $value)
    {
        return $query->where($attribute, 'like', '%' . $value . '%');
    }

#コントローラー
↓scopeを取り除いたメソッド名で呼び出せる!

MemberController.php
パターン1
<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;

class MemberController extends Controller
{
    /** @var int ページ毎の最大表示件数 */
    const LIMIT = 15;

    /**
     * 会員ユーザー一覧画面表示
     * @param Request $request
     * @return View 会員ユーザー一覧画面
     */
    public function index(Request $request)
    {
        $name = $request->input('name');
        $email = $request->input('email');
        $users = User::Namesearch($name)->Emailsearch($email)->orderBy('id', 'asc')->paginate(self::LIMIT);
        return view('admin/member/index', compact('users'));
    }
}
MemberController.php
パターン2
    public function index(Request $request)
    {
        $name = $request->input('name');
        $email = $request->input('email');
        $users = User::WhereLike('name', $name)
            ->WhereLike('email', $email)
            ->oldest()->paginate(self::LIMIT);
        return view('admin.member.index', compact('users'));
    }

whereLike(小文字)でも OK

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?