Reputation: 33
I did a query and got a collection of 11 arrays of db objects. Basically 11 posts. I need to do a wildcard search if a string is present inside any of these posts (in title or in model or description). How best to achieve it? I tried with multiple orWhere, but doesn't seem to be working.
My function
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 - response
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 ▶}
]
}
Upvotes: 0
Views: 2386
Reputation: 223
Try following query.
$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();
Upvotes: 0
Reputation: 1502
The $posts
is referring to
$posts = Country::where('country_name', $request->input('country'))->first()->posts;
which is not including the search query, you should dump $customers
to see the result.
Besides that, your $customers
query is irrelevant with the $posts
one, you are making two different queries. Your current $posts
will return all countries' posts.
Try 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();
Upvotes: 2