Can you please start by formatting your code by adding ``` on a line before and after it? :)
multiple where query
Hi, I'm new to laravel and have only been programming for 6 months. I've been able to make some great progress thanks to Laracast but I've run into an issue that I can not seem to resolve. So I'm hoping one of you can tell me why this is not returning the expected result
Within my livewire dashboard class I'm calling the FilterAds method in my Ad Model. This method filters the model based on a status. When I don't do a search everything works as expected I get items with a specific $adStatus but when I do a search I never get a result. What blows my mind is that if I try ('status_id','=',2) in my query the search works fine and searches within items with a status of 2.
class Dashboard extends Component
{
use WithPagination; //call pagination on render view
//Default vars
protected $pageName = 'Dashboard';
protected $adStatus = 1;
//Search
public $search ='';
// render the page
public function render(Request $request)
{
$this->setpageattributes($request->path());
return view('livewire.dashboard', [
'ads' => Ad::FilterAds('name', $this->search, $this->adStatus),
'pageName' => $this->pageName,
]);
}
}
public static function FilterAds($field, $string, $status)
{
return Ad::where([
['status_id','=',$status],
[$field, 'like', '%' . $string . '%'],
])->paginate(10);
//also tried this syntax with the same result
//return Ad::where('status_id','=',$status)->where($field, 'like', '%' . $string . '%')->paginate(10);
}
a bit convoluted there. Look...
use WithPagination;
public $pageName = 'Dashboard';
public $status = 1;
public $search;
public function getAdsProperty()
{
return Ad::query()
->where('status_id', $this->status)
->where($this->field, 'like', '%' . $this->search. '%')
->paginate(10);
}
public function render()
{
return view('livewire.dashboard');
}
then in your view just type and the search will filter. And change the status $set('status', 3) and the status will change. easy peasy. one small change $ads now needs to be $this->ads in the blade.
one issue $this->setpageattributes($request->path()) I assume isn't working properly UNLESS you already realize that the request path changes to a different route on the second render.
Please or to participate in this conversation.