I am trying to search for a record in the Individualprofile model based on a search key. The route below when viewed from the browser throws a Call to undefined method Illuminate\Database\Eloquent\Collection::whereRaw()
exception.
Inside the foreach loop I have tried Individualprofile::whereRaw(..)
but still the same problem.
Below is my full route implementation.
Route::get('/get-individualprofiles',function(){
$text = "Lamin";
if(trim($text) == ""){
return Individualprofile::take(10)->get();
}
$substr = preg_split("/[\s,.()&;:_-]+/",preg_replace("/(\w+)/","%$1%",trim($text)),-1,PREG_SPLIT_NO_EMPTY);
$profiles = Individualprofile::all();
foreach ($substr as $key) {
$profiles = $profiles->whereRaw('(name like ? or mobile like ? or address like ? or occupation like ? or mstatus like ?)',[$key,$key,$key,$key,$key]);
}
return $profiles->take(100)->get();
});
You're trying use a Query Builder method with a Collection instance. Try using this:
$profiles = Individualprofile::query();
foreach ($substr as $key) {
$profiles = $profiles->whereRaw('(name like ? or mobile like ? or address like ? or occupation like ? or mstatus like ?)',[$key,$key,$key,$key,$key]);
}
return $profiles->take(100)->get();
Individualprofile::all()
returns the result of get()
which means you get a Collection
not a Builder
instance which has the whereRaw
method. The query()
method will return a Builder
instance for your model, which you can use to build your query.