Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataTables ajax fails on 1-n relation #153

Closed
mpixelz opened this issue Oct 1, 2016 · 5 comments
Closed

DataTables ajax fails on 1-n relation #153

mpixelz opened this issue Oct 1, 2016 · 5 comments

Comments

@mpixelz
Copy link

mpixelz commented Oct 1, 2016

Hi,
i have three relations (country, state, city) so 1 country with many states, 1 state with many cities..
when i have more than 1 belongsTo in my city model it does not fetch the connected country.. without ajax it times out max_execution time error.. but when i enable
$this->crud->enableAjaxTable();
it gives me:
DataTables warning: table id=crudTable - Ajax error. For more information about this error, please see http://datatables.net/tn/7

here is my CityCrudController:

<?php namespace App\Http\Controllers\Admin;

use Backpack\CRUD\app\Http\Controllers\CrudController;

// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\CityRequest as StoreRequest;
use App\Http\Requests\CityRequest as UpdateRequest;

class CityCrudController extends CrudController {

    public function __construct() {
        parent::__construct();
        $this->crud->enableAjaxTable(); 

        $this->crud->setModel("App\Models\City");
        $this->crud->setRoute("admin/cities");
        $this->crud->setEntityNameStrings('City', 'Cities');

        $this->crud->addColumns([
                [
                    'name'  => 'name',
                    'label' => 'Name',
                    'type'  => 'text',
                ],
                [
                   // 1-n relationship
                   'label' => "State", // Table column heading
                   'type' => "select",
                   'name' => 'state_id', // the method that defines the relationship in your Model
                   'entity' => 'state', // the method that defines the relationship in your Model
                   'attribute' => "name", // foreign key attribute that is shown to user
                   'model' => "App\Models\State", // foreign key model
                ],
                [
                   // 1-n relationship
                   'label' => "Country", // Table column heading
                   'type' => "select",
                   'name' => 'country_id', // the method that defines the relationship in your Model
                   'entity' => 'country', // the method that defines the relationship in your Model
                   'attribute' => "name", // foreign key attribute that is shown to user
                   'model' => "App\Models\Country", // foreign key model
                ]
                              ]);
        $this->crud->addFields([
                [
                    'name'  => 'name',
                    'label' => 'Name',
                    'type'  => 'text',
                ],
                [
                   // 1-n relationship
                   'label' => "State", // Table column heading
                   'type' => "select",
                   'name' => 'state_id', // the method that defines the relationship in your Model
                   'entity' => 'state', // the method that defines the relationship in your Model
                   'attribute' => "name", // foreign key attribute that is shown to user
                   'model' => "App\Models\State", // foreign key model
                ],
                [
                   // 1-n relationship
                   'label' => "Country", // Table column heading
                   'type' => "select",
                   'name' => 'country_id', // the method that defines the relationship in your Model
                   'entity' => 'country', // the method that defines the relationship in your Model
                   'attribute' => "name", // foreign key attribute that is shown to user
                   'model' => "App\Models\Country", // foreign key model
                ]
                              ]);
    }

    public function store(StoreRequest $request)
    {
        return parent::storeCrud();
    }

    public function update(UpdateRequest $request)
    {
        return parent::updateCrud();
    }
}

here is my model City.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;

class City extends Model
{

    use CrudTrait;

    protected $table = 'citys';
    protected $primaryKey = 'id';
    // public $timestamps = false;
    // protected $guarded = ['id'];
    // protected $fillable = [];
    // protected $hidden = [];
    // protected $dates = [];

    public function state()
    {
        return $this->belongsTo('App\Models\State');
    }
    public function country()
    {
        return $this->belongsTo('App\Models\Country');
    }
}

how can i connect the country to the city model so it will display connected country through state relation?

@mpixelz
Copy link
Author

mpixelz commented Oct 1, 2016

Ive already tried the method here: #87 (comment)

P.S i just did composer update and the crud and base were updated along with laravel framework update but the issue still persists.

@OwenMelbz
Copy link
Contributor

Just to clarify something,

You've got

                [
                   // 1-n relationship
                   'label' => "State", // Table column heading
                   'type' => "select",
                   'name' => 'state_id', // the method that defines the relationship in your Model
                   'entity' => 'state', // the method that defines the relationship in your Model
                   'attribute' => "name", // foreign key attribute that is shown to user
                   'model' => "App\Models\State", // foreign key model
                ],

However, you only posted your Country model - I think yu should have a relationship inside your State model called "country" with another belongsTo in it

// State.php
 public function country()
    {
        return $this->belongsTo('App\Models\Country');
    }

then within your original addFields call change the 'name' => 'state_id', and 'entity' => 'state' to contain country instead of state

I believe the way it works is, It looks for the method defined within name/entity, inside the model you pass in. As you're passing in the State model, its looking for state in it currently, which wouldnt make sense. So you need to say "look at the country, within the state" instead.

@tabacitu
Copy link
Member

tabacitu commented Oct 2, 2016

Hi @mpixelz ,

I completely agree with @OwenMelbz - the 1-n column (select) works with 1-n relationships only. The relationship between your City and Country isn't 1-n. It's more like 1-n-n :-)

I have only one thing to add to @OwenMelbz 's response, only if his solution doesn't work for you: I think you might be able to create a fake 1-n relationship between City and Country using Laravel's hasManyThrough(). However, I haven't tried that myself in Backpack.

Cheers!

PS. If you end up trying hasManyThrough(), I'm very eager to find out if it worked :-)

@OwenMelbz
Copy link
Contributor

@mpixelz just doing the cleaning, so closing the issue, feel free to re-open if you still need help!

@mpixelz
Copy link
Author

mpixelz commented Jul 21, 2017

reopening this as the issue still persists.
here is the scenario.. User have PersonalDetail table where all the personal information is stored.
User model have:

public function PersonalDetail()
    {
        return $this->hasOne('App\Models\PersonalDetail' , 'user_id');
    }

in my crud.. when i use this model on select for 1-n relation:

$this->crud->addColumn([
               // 1-n relationship
               'label' => "Mobile", // Table column heading
               'type' => "select",
               'name' => 'user_id', // the column that contains the ID of that connected entity;
               'entity' => 'PersonalDetail', // the method that defines the relationship in your Model
               'attribute' => "mobileno", // foreign key attribute that is shown to user
               'model' => "App\Models\PersonalDetail", // foreign key model
            ]);

now when i enable ajax tables i get the error.. but if i remove this column it works fine.

This was referenced Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants