We are going to create the filters that we implemented before using conditionals, using "Laravel mode" which is the one recommended by the Laravel team and it is up to the reader to decide which one they prefer to use.
For this, we will use when() in which the first parameter corresponds to the property to be evaluated, which in case the value is defined, executes the second parameter that corresponds to the closure:
Model::when(request('search'), function (Builder $query, string $search) ...
The closure that corresponds to the second parameter receives as parameters the $query to apply any type of operation with Eloquent and the evaluated parameter:
Model::when(request('search'), function (Builder $query, string $search) {
$query->where(function ($query) use ($search) {
$query-><WHERES>
})
Remember that I had told you at the beginning of this section that I find it very interesting to also show you the solution with the web here I started to do it and now I will explain a little and we will get into context here what I would ask you if you want to do this practice is that you duplicate the one we had before you can see that I give you the previous solution be careful that it is my favorite I prefer to use conditionals here:
// public function render()
// {
// // $posts = Post::paginate(15);
// // $posts = Post::where('id', '>=', 1);
// $posts = new Post();
// // $categories = Category::get();
// $categories = Category::pluck("title", "id");
// //filters
// if ($this->type) {
// $posts = $posts->where('type', $this->type);
// }
// if ($this->category_id) {
// $posts = $posts->where('category_id', $this->category_id);
// }
// if ($this->posted) {
// $posts = $posts->where('posted', $this->posted);
// }
// if ($this->search) {
// $posts = $posts->where(function($query){
// $query
// ->orWhere('id', 'like', '%'.$this->search.'%')
// ->orWhere('title', 'like', '%'.$this->search.'%')
// ->orWhere('description', 'like', '%'.$this->search.'%')
// ;
// });
// }
// if ($this->from && $this->to) {
// $posts = $posts->whereBetween('date', [date($this->from), date($this->to)]);
// }
// //filters
// $posts = $posts->paginate(15);
// return view('livewire.dashboard.post.index', compact('posts', 'categories'));
// }
Since with this one it seems to me Well here I started a little bit and we are going to finish the implementation to do it in practice Remember that the use of the web here you can see in the official documentation of Laravel is the way in which we have official Laravel to add conditionals as you can see here conditional clauses
So its operation is very simple the issue here is the configuration since I grabbed Simply the most complicated we have many ways but everything starts by placing post obviously it is no longer necessary to place here the distance as who says the base query or or flat that we have here to build the post neither with the New post nor with this one but directly we go from distance from the model then we place when:
$posts = Post::when($this->search, function (Builder $query, string $search) {
$query->where(function ($query) use ($search) {
$query->orWhere('id', 'like', "%" . $search . "%")
->orWhere('title', 'like', "%" . $search . "%")
->orWhere('description', 'like', "%" . $search . "%");
});
})
From here on it would basically be the filter based on the syntax Here you have to pass it what the parameter is and it will apply the filters only if it exists From here on this parameter is what is supplied to what would be the clause and here you can put any name Here you put search and
For the rest of the parameters we also define the when clauses:
$posts = Post::when($this->search, function (Builder $query, string $search) {
$query->where(function ($query) use ($search) {
$query->orWhere('id', 'like', "%" . $search . "%")
->orWhere('title', 'like', "%" . $search . "%")
->orWhere('description', 'like', "%" . $search . "%");
});
})
->when($this->type, function(Builder $query, $type) {
$query->where('type', $type);
})
->when($this->category_id, function(Builder $query, $category_id) {
$query->where('category_id', $category_id);
})
->when($this->posted, function(Builder $query, $posted) {
$query->where('posted', $posted);
})
->when($this->type, function(Builder $query, $type) {
$query->where('type', $type);
})
->when($this->to, function(Builder $query, $to) use($from) {
$query->whereBetween('date', [date($from), date($this->to)]);
})->with('category')
->orderBy($this->sortColumn, $this->sortDirection)
->paginate(15);
And their respective filters:
<div class="grid grid-cols-2 gap-2 mb-3">
<select class="block w-full" wire:model.live='posted'>
<option value="">{{ __('Posted') }}</option>
<option value="not">{{ __('Not') }}</option>
<option value="yes">{{ __('Yes') }}</option>
</select>
<select class="block w-full" wire:model.live='type'>
<option value="">{{ __('Type') }}</option>
<option value="advert">{{ __('Advert') }}</option>
<option value="post">{{ __('Post') }}</option>
<option value="course">{{ __('Course') }}</option>
<option value="movie">{{ __('Movie') }}</option>
</select>
<select class="block w-full" wire:model.live='category_id'>
<option value="">{{ __('Category') }}</option>
@foreach ($categories as $i => $c)
<option value="{{ $i }}">{{ $c }}</option>
@endforeach
</select>
<x-input wire:model.live='search' placeholder="{{ __('Search...') }}" />
<div class="grid grid-cols-2 gap-2">
<x-input wire:model='from' placeholder="From" type='date' />
<x-input wire:model.live='to' placeholder="To" type='date' />
</div>
</div>
In short the use of the when method is nothing more than a query that is optionally executed if a true or false condition is met
https://github.com/libredesarrollo/book-course-laravel-livewire/blob/main/resources/views/livewire/dashboard/post/index.blade.php
- Andrés Cruz
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter
I agree to receive announcements of interest about this Blog.
!Courses from!
10$
On Udemy
There are 3d 00:06!
!Courses from!
4$
In Academy
View courses!Books from!
1$
See the books