2

I installed backpack on the latest version of Laravel 6.

Upon running this command as a test:

php artisan make:migration:schema create_posts_table --schema="user_id:unsignedInteger:foreign, 
name:string, slug:string, description:text, keywords:string:nullable"

The command created a Post model in Models/Post

I would like to see the names of the users from the "user" table in my "post" CRUD.

First question: I generate my CRUD with:

php artisan backpack:crud post

I see the CRUD, at the top of the CRUD I would like to pass the users name from the User table in the select dropdown.

I tried this:

protected function setupCreateOperation()
{
$this->crud->setValidation(PostRequest::class);
// TODO: remove setFromDb() and manually define Fields
//$this->crud->setFromDb();
$this->crud->setColumnDetails([
    'label' => 'User', // Table column heading
    'type' => 'select',
    'name' => 'user_id', // the column that contains the ID of that connected entity;
    'entity' => 'backpackuser', // the method that defines the relationship in your Model
    'attribute' => 'name', // foreign key attribute that is shown to user
    'model' => 'App\Models\backpackuser' // foreign key model
    ]);
}

But I am getting the error:

Symfony\Component\Debug\Exception\FatalThrowableError
Too few arguments to function Backpack\CRUD\app\Library\CrudPanel\CrudPanel::setColumnDetails(), 1 
passed in C:\laragon\www\backpack\app\Http\Controllers\Admin\PostCrudController.php on line 41 and 
exactly 2 expected

My problem "I think" is related to the "'entity' => 'backpackuser',"

From what I understand I need to add the relation method here but what I don't get is that Backpack did not create it when running the first command, is this normal?

Also, which model shall I user to pass the user details into a select field? App/User or App/Models/BackpackUser ?

Thank you.

Benny
  • 430
  • 6
  • 17

2 Answers2

1

setColumnDetails() has two parameters:

  • the name/key of the column you're modifying
  • the attributes you're modifying and their new values

It looks like you've only provided the second parameter. Hence the error "Too few arguments to function setColumnDetails(), 1 passed and exactly 2 expected."

Try:

$this->crud->setColumnDetails('user_id', [
    'label' => 'User', // Table column heading
    'type' => 'select',
    'entity' => 'backpackuser', // the method that defines the relationship in your Model
    'attribute' => 'name', // foreign key attribute that is shown to user
]);

Pro tip: you don't need to specify the model. If on your Post model you have the backpackuser() relationship properly defined, Backpack will pick up the model from that relationship.

tabacitu
  • 6,047
  • 1
  • 23
  • 37
0

Yes, you should create a relationship manually and add it to the Post model.

In this case (see Users in the Posts CRUD), you need to add relation method into Post, not User.

Kamran
  • 523
  • 6
  • 18
  • Sorry like this? public function user(){ return $this->belongsTo('App\User'); } I deleted my previous comments as I wrote the wrong model – Benny Dec 22 '19 at 22:04
  • @Benny yes exactly just add it in the Post model. – Kamran Dec 22 '19 at 22:06
  • I have also setup my crud as: `$this->crud->addColumn([ 'label' => 'User', // Table column heading 'type' => 'select', 'name' => 'user_id', // the column that contains the ID of that connected entity; 'entity' => 'user', // the method that defines the relationship in your Model 'attribute' => 'name', // foreign key attribute that is shown to user 'model' => 'App\User' // foreign key model ]);` – Benny Dec 22 '19 at 22:06
  • It should work for the post entity, you got errors? – Kamran Dec 22 '19 at 22:09