In the Fetch Operation, I'm trying to use a custom function to filter the results returned by fetch().
In a few places I've tried to use the Fetch operation to replace some custom Ajax controllers in my model. Whenever I add a custom query to the fetch() method, I get an error: Call to a member function ### on string (where ### changes based on my query).
Example:
I have a Region model and a District model, with a 1 (Region) to many (Districts) relationship setup. My Region model has a scope called "active".
On my DistrictCrudController, I setup a relationship field with Ajax enabled:
[
'name' => 'region_id',
'type' => 'relationship',
'label' => 'Region Name',
'ajax' => true,
],
I added a fetchRegion() method and passed an array to the fetch() method:
use \Backpack\CRUD\app\Http\Controllers\Operations\FetchOperation;
public function fetchRegion ()
{
return $this->fetch([
'model' => Region::class,
'query' => function($model) {
return $model->active();
}
]);
}
The active regions would be searchable in the region field.
The Ajax request gave an error: Call to a member function active() on string.
It looks like the query closure receives the model's FQDN as a string, rather than an instance of the model class. I tracked it down to line 61 in the FetchOperation.php, and updated that by passing $model_instance to the closure instead of $config['model'].
I then got another error: Call to undefined method Illuminate\\Database\\Eloquent\\Builder::getColumnType(), pointing to line 91 of FetchOperation.php. I think this happens because now $config['query'] returns an instance of EloquentBuilder rather than the model class.
Updating line 91 to use the $model_instance fixed this error, and now the fetch() operation is returning my active regions as expected.
This fix seems to work for all the custom queries I've tried. Happy to submit a PR for this :smiley:
When I run php artisan backpack:version the output is:
### PHP VERSION:
PHP 7.4.1 (cli) (built: Dec 18 2019 14:46:18) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies
### LARAVEL VERSION:
v7.11.0@f4563bd2e0875c59a1f7967abdbe5cef7f240117
### BACKPACK VERSION:
4.1.4@4303aa07ec0c597abfcfe83896558ca6e98f0d6a
Really love the recent update to 4.1 so far - we use select2_from_ajax a lot and the relationship field + fetch() combo feels much more intuitive than the old way of defining custom controllers to respond to the ajax calls.
Hello there! Thanks for opening your first issue on this repo!
Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps _a lot_ in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.
Backpack communication channels:
backpack-for-laravel tag;Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome _awesome_ community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.
Thank you!
--
Justin Case
The Backpack Robot
Excellent bug report @dave-mills , thanks a lot!
Excellent PR too - couldn't have wished for more 馃槃

Really love the recent update to 4.1 so far - we use select2_from_ajax a lot and the relationship field + fetch() combo feels much more intuitive than the old way of defining custom controllers to respond to the ajax calls.
Very glad to hear that!
Most helpful comment
Excellent bug report @dave-mills , thanks a lot!
Excellent PR too - couldn't have wished for more 馃槃
Very glad to hear that!