$this->crud->addColumn([
'name' => 'full_name', // there is no full_name column in the db, it's an accessor
'type' => 'text',
'label' => 'Full Name',
'searchLogic' => function ($query, $column, $searchTerm) {
$query->orWhere('first_name', 'like', '%'.$searchTerm.'%');
$query->orWhere('last_name', 'like', '%'.$searchTerm.'%');
}
]);
the searchLogic applied
searchLogic always return false
Modify Columns Traits
// check if the column exists in the DB table
if (\Schema::hasColumn($this->model->getTable(), $column_with_details['name'])) {
$column_with_details['tableColumn'] = true;
} else {
$column_with_details['tableColumn'] = false;
$column_with_details['orderable'] = false;
// $column_with_details['searchLogic'] = false; // This line always override the closure
}
This should hit:
I can't see why it wouldn't.
If you are able, try:
if (is_callable($searchLogic)) {
dd($searchLogic);
return $searchLogic($query, $column, $searchTerm);
}
(or get a debugger to work - but dd may be much less fussing about).
This will tell you if that is being called.
If it is then try something like:
if (is_callable($searchLogic)) {
$it = $searchLogic($query, $column, $searchTerm);
info($it->toSql());
info($it->getBindings());
return $it; // or still dd($it);
}
(in theory that will go to storage/log/laravel.log if I've got the helper name correct; in essence, figure out what SQL is being made).
And make sure that the search logic function is getting called with the values you expect. And make sure that the thing you are searching for does actually exist 馃槃
I am also wondering why it doesn't work.
somehow this line overriding my CrudController
$column_with_details['searchLogic'] = false;
i got false on this debugging, it never get into if (is_callable($searchLogic))
if (isset($column['searchLogic'])) {
$searchLogic = $column['searchLogic'];
dd($searchLogic);
if (is_callable($searchLogic)) {
return $searchLogic($query, $column, $searchTerm);
}
if ($searchLogic == false) {
return;
}
}
I think the Column Traits need to check user declared the searchLogic or not... this is how i fix the issue...
// check if the column exists in the DB table
if (\Schema::hasColumn($this->model->getTable(), $column_with_details['name'])) {
$column_with_details['tableColumn'] = true;
} else {
$column_with_details['tableColumn'] = false;
$column_with_details['orderable'] = false;
if (! array_key_exists('searchLogic', $column_with_details)) { // Check before set to false
$column_with_details['searchLogic'] = false;
}
}
I have the same issue here
@jkudish @kiddtang I was able to implement it like this:
> 'searchLogic' => function ($q, $column, $searchTerm) {
> $q->orWhereHas('user', function ($query) use ($searchTerm) {
> $query->where('first_name', 'like', '%' . $searchTerm . '%');
> $query->orWhere('last_name', 'like', '%' . $searchTerm . '%');
> });
> },
Edit:
In case you do this on the user, use self join, I implemented it on relation
So not a bug, but a documentation oversight
Edit 2: Please use stackoverflow for questions
Thank you!
I'm using the searchLogic as explained in the "Upgrading CRUD 3.2 to 3.3" guide but it is not working for me. I'm only able to fix it by modifying the column traits. What am I missing here?
$this->crud->addColumn([
'name' => 'full_name', // there is no full_name column in the db, it's an accessor
'type' => 'text',
'label' => 'Full Name',
'searchLogic' => function ($query, $column, $searchTerm) {
$query->orWhere('first_name', 'like', '%'.$searchTerm.'%');
$query->orWhere('last_name', 'like', '%'.$searchTerm.'%');
}
]);
@matism did you use the appends property to add full_name? If you did it should work
Again: please use stackoverflow for questions
$this->crud->addColumn([
'type' => 'text',
'label' => trans('drivers.name'),
'name' => 'name',
]);
(I don't have name, just first_name and last_name)
In model:
protected $appends = ['name'];
public function getNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}
@indra1 Yes I've added full_name to the appends property. But still $this->hasColumn($this->model->getTable(), $column_with_details['name']) returns false.
@matism please open a stack overflow question, add the code and write where you think you have an issue and tag it with backpack-for-laravel
I just updated my backpack from 3.1 to 3.3 and it works seamlessly
@matism let's discuss here...
https://stackoverflow.com/questions/47835198/backpack-crud-searchlogic-always-considered-not-declared
@indra1 I met same issue again while this time I need to search on "currency" (a relationship) & "amount" (own table). Hope you could give me some hints on the stackoverflow. Thanks in advance!
Edit:
@lloy0076 @tabacitu
while i am still suggesting to adding the "searchLogic" declaration checking for "non-existing" column (the accessor attributes). At least for my case now, it is not working again...
Yes... some table joining could solve the problem, but i think we need simpler solution...
$this->crud->addColumn([
'name' => 'full_amount', // custom attributes
'label' => "Amount",
'type' => 'text',
'searchLogic' => function ($query, $column, $searchTerm) {
$query->orWhereHas('currency', function ($q) use ($searchTerm) { // a relationship
$q->where('code', 'like', '%'.$searchTerm.'%');
});
$query->orWhere('amount', 'like', '%'.$searchTerm.'%'); // at this own table
}
]);
@kiddtang - I think you were totally right on this, I'm sorry I didn't see this earlier. I just pushed a fix to this, will be published at the end of the day, together with a few other fixes.
Guys, let me rephrase this the way I understand it:
addColumn() method;Fixed now. We now place those values ONLY IF they're not explicitly defined. Thanks for bringing this @kiddtang .
Cheers!
@tabacitu Thanks a lot! Now it's working for me as well.
Most helpful comment
@jkudish @kiddtang I was able to implement it like this:
Edit:
In case you do this on the user, use self join, I implemented it on relation
So not a bug, but a documentation oversight
Edit 2: Please use stackoverflow for questions