Crud: FetchOperation error when using custom query to filter Ajax response.

Created on 13 May 2020  路  2Comments  路  Source: Laravel-Backpack/CRUD

Bug report

In the Fetch Operation, I'm trying to use a custom function to filter the results returned by fetch().

What I did

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();
        }
    ]);
}

What I expected to happen

The active regions would be searchable in the region field.

What happened

The Ajax request gave an error: Call to a member function active() on string.

What I've already tried to fix it

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:

Backpack, Laravel, PHP, DB version

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

PS

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.

triage

Most helpful comment

Excellent bug report @dave-mills , thanks a lot!
Excellent PR too - couldn't have wished for more 馃槃

giphy

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!

All 2 comments

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:

  • Bug Reports, Feature Requests - Github Issues (here);
  • Quick help (_How do I do X_) - Gitter Chatroom;
  • Long questions (_I have done X and Y and it won't do Z wtf_) - Stackoverflow, using the backpack-for-laravel tag;
  • Showing off something you've made, asking for opinion on Backpack/Laravel matters - Reddit;

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 馃槃

giphy

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AlexanderWM picture AlexanderWM  路  3Comments

deepaksp picture deepaksp  路  3Comments

lotarbo picture lotarbo  路  3Comments

sonoftheweb picture sonoftheweb  路  3Comments

packytagliaferro picture packytagliaferro  路  3Comments