Laravel 通配符字符串搜索

Laravel wildcard string search

我做了一个查询,得到了一个 collection 的 11 个数据库数组 objects。基本上11个职位。如果在任何这些帖子中(标题或模型或描述中)存在字符串,我需要进行通配符搜索。如何最好地实现它?我尝试了多个 orWhere,但似乎没有用。

我的函数

public function search(Request $request)
{
    $this->validate($request, [
        'search' => 'required',
        'country' => 'required',
    ]);
    $search=$request->input('search');
    $posts = Country::where('country_name', $request->input('country'))->first()->posts;

    $customers=$posts::where('title','LIKE','%'.$request->search.'%')
    ->orWhere('model','LIKE','%'.$request->search.'%')
    ->orWhere('notes','LIKE','%'.$request->search.'%')
    ->orWhere('description','LIKE','%'.$request->search.'%')
    ->get();

    dd($posts);
}

$posts - 回复

 Collection {#367 ▼
          #items: array:11 [▼
            0 => Post {#368 ▼
              #table: "posts"
              #dates: array:1 [▶]
              #fillable: array:18 [▶]
              #connection: "mysql"
              #primaryKey: "id"
              #keyType: "int"
              +incrementing: true
              #with: []
              #withCount: []
              #perPage: 15
              +exists: true
              +wasRecentlyCreated: false
              #attributes: array:22 [▼
                "id" => 1
                "title" => "Brand New Harley Davidson For Sale"
                "make_id" => "1"
                "model" => "Sportster"
                "year" => 2019
                "price" => "987890.00"
                "mileage" => 8798
                "mileage_unit" => "KM"
                "negotiable" => "negotiable"
                "condition" => "All Good"
                "plate" => null
                "vin" => null
                "color" => null
                "notes" => null
                "description" => null
                "created_by" => 1
                "status_change" => "2019-06-12 02:35:51"
                "status" => "Deleted"
                "slug" => "1560305498-Brand-New-Harley-Davidson-For-Sale"
                "created_at" => "2019-06-12 02:11:38"
                "updated_at" => "2019-06-12 02:36:50"
                "laravel_through_key" => "IND"
              ]
              #original: array:22 [▶]
              #changes: []
              #casts: []
              #dateFormat: null
              #appends: []
              #dispatchesEvents: []
              #observables: []
              #relations: []
              #touches: []
              +timestamps: true
              #hidden: []
              #visible: []
              #guarded: array:1 [▶]
            }
            1 => Post {#369 ▶}
            2 => Post {#370 ▶}
            3 => Post {#371 ▶}
            4 => Post {#372 ▶}
            5 => Post {#373 ▶}
            6 => Post {#374 ▶}
            7 => Post {#375 ▶}
            8 => Post {#376 ▶}
            9 => Post {#377 ▶}
            10 => Post {#378 ▶}
          ]
        }

$posts指的是

$posts = Country::where('country_name', $request->input('country'))->first()->posts;

不包括搜索查询,您应该转储 $customers 以查看结果。

除此之外,您的 $customers 查询与 $posts 查询无关,您正在进行两个不同的查询。您当前 $posts 将 return 所有国家/地区的帖子。

尝试 Query Relations:

$posts = Country::where('country_name', $request->input('country'))->first()->posts()
    ->where('title','LIKE','%'.$search.'%')
    ->orWhere('model','LIKE','%'.$search.'%')
    ->orWhere('notes','LIKE','%'.$search.'%')
    ->orWhere('description','LIKE','%'.$search.'%')
    ->get();

尝试以下查询。

$posts = Country::with(['posts' => function($oQuery) use($search){ 
 $oQuery->where('title','LIKE','%'.$search.'%')
        ->orWhere('model','LIKE','%'.$search.'%')
        ->orWhere('notes','LIKE','%'.$search.'%')
        ->orWhere('description','LIKE','%'.$search.'%');}])->where('country_name', $request->input('country'))->first();