搜索表单中的Laravel范围问题

时间:2019-07-30 14:17:02

标签: php laravel scope search-form

我有一个搜索表单,可以通过某些过滤器搜索广告/属性。我在使用“喜欢”功能时遇到问题,我的属性ID和用户ID的搜索字段无法正常工作。例如,当我在属性ID中输入1并提交表单时,它将返回其ID中具有数字1的所有属性(例如1、11、21、111)。而且,当我将代码更改为不包含“ like”功能和“ where”功能时,它返回正确的结果,但是其他过滤器不起作用。任何帮助表示赞赏。这是我的代码。

Property.php

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Property extends Model
{
    protected $guarded = ['id'];

    public function scopeId($query, $id)
    {
        return $query->where('id', $id);
    }

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

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

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

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

    public function scopeViews($query, $views)
    {
        return $query->where('page_views', 'like', '%'.$views.'%');
    }
}

PropertyController.php

class PropertyController extends Controller
{
    public function show(Request $request)
    {
        $properties = Property::id($request->id)
        ->userId($request->user_id)
        ->title($request->title)
        ->active($request->active)
        ->views($request->views)
        ->description($request->description)
        ->paginate(10);
        return view('admin.properties.propertylist', compact('properties','request'));
    }
}

propertylist.blade.php

<form action="{{route('admin.property')}}" method="get">

    <label for="id">Id</label>
    <input type="number" name="id" id="id" value="@if(!empty($request->id)){{$request->id}}@endif">

    <label for="user_id">User id</label>
    <input type="number" name="user_id" id="user_id" value="@if(!empty($request->user_id)){{$request->user_id}}@endif">

    <label for="title">Title</label>
    <input type="text" name="title" id="title" value="@if(!empty($request->title)){{$request->title}}@endif">

    <label for="description">Description</label>
    <input type="text" name="description" id="description" value="@if(!empty($request->description)){{$request->description}}@endif">
        <label for="active">Status</label>
        <select name="active"id="active">
            <option value='' @if(empty($request->active))selected @endif>All</option>
            <option value="A" @if(!empty($request->active) && $request->active == 'A')selected @endif>Approved</option>
            <option value="Q" @if(!empty($request->active) && $request->active == 'Q')selected @endif>Queued</option>
            <option value="S" @if(!empty($request->active) && $request->active == 'S')selected @endif>Suspicious</option>
            <option value="D" @if(!empty($request->active) && $request->active == 'D')selected @endif>Denied</option>
        </select>

        <label for="views">Views</label>
        <input type="text" name="views" id="views" value="@if(!empty($request->views)){{$request->views}}@endif">

    <button class="btn btn-default" type="submit"> Submit </button>
</form>

0 个答案:

没有答案