I've successfully implemented a search feature for normal fields in my form. However, I'm encountering difficulty when trying to search within a date range. Here's my controller code:
public function index() {
$query = Matter::query();
$query->when(request('search_client','search_file_ref','search_file_status','search_date_from','search_date_to'), function($query){
$query->where('client_company', 'like', '%' . request('search_client') . '%');
$query->where('file_ref', 'like', '%' . request('search_file_ref') . '%');
$query->where('status', 'like', '%' . request('search_file_status') . '%');
$query->whereBetween('created_at', ['search_date_from', 'search_date_to']);
});
return $query->orderBy('id','asc')->get();
}
In the view section of my application, I have the following setup for searching within a date range:
<b-col md="4">
<form @submit.prevent="searchDateFrom">
<label for="">Choose File Status:</label>
<div class="input-group">
<input
v-model="search_date_from"
type="date"
placeholder="Search File Status"
class="form-control"
/>
<input
v-model="search_date_to"
type="date"
placeholder="Search File Status"
class="form-control"
/>
<div class="input-group-append">
<button type="submit" class="btn btn-primary">
<i class="fas fa-search"></i>
</button>
</div>
</div>
</form>
</b-col>
...
data(){
return {
search_client: "",
search_file_ref: "",
search_file_status: "",
search_date_from: "",
search_date_to: "",
matters:[]
}
},
...
searchDateFrom(){
axios.get('/api/auth/matter?search_date_from=' + this.search_date_from + 'search_date_to=' + this.search_date_to)
.then(response => this.matters = response.data)
},
By directly specifying the date range in the controller like this, it works:
$query->whereBetween('created_at', ['2022-06-01', '2022-06-07']);
But when including the request variables like this:
$query->whereBetween('created_at', ['search_date_from', 'search_date_to']);
The API does not return any data. It seems that there might be an issue with how I'm handling the request. The model structure is as follows:
{
use HasFactory;
protected $fillable = [
'matter_type','client_company','description','file_group',
'control_account','pic','lawyer','task_assign','task_recipient',
'file_ref','remark','purchaser_1','purchaser_2','status'
];
protected $casts = [
'created_at' => 'datetime:m / d / Y',
];
}
I can successfully retrieve and search through data using fields other than dates. My goal is to allow users to input two dates and retrieve results within that specific range.