S
S
Sergey2020-02-29 19:20:58
Laravel
Sergey, 2020-02-29 19:20:58

How to make hasMany work in Laravel BackPack?

I'm trying to connect the BackPack admin panel to the project, when using the hasMany connection, a bunch of errors come out, it can't find the fields, then there is no method ...
App\Models\Question

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\app\Models\Traits\CrudTrait;

class Question extends Model{
  use CrudTrait;

  public function answers() {
    return $this->hasMany(\App\Models\Answer::class);
  }
}


App\Models\Answer
<?php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\app\Models\Traits\CrudTrait;

class Answer extends Model{
  use CrudTrait;
  public function question() {
    return $this->belongsTo(\App\Models\Question::class, 'question_id');
  }
}


In the question editor, I want to add the ability to select multiple answers:
App\Http\Controllers\Admin\QuestionCrudController
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\QuestionRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;

class QuestionCrudController extends CrudController{
  use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
  use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
  use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
  use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
  use \Backpack\CRUD\app\Http\Controllers\Operations\ShowOperation;

  public function setup() {
    $this->crud->setModel('App\Models\Question');
    $this->crud->setRoute(config('backpack.base.route_prefix') . '/question');
    $this->crud->setEntityNameStrings('Вопрос', 'Вопросы');
  }

  protected function setupUpdateOperation() {
    $this->setupCreateOperation();


    $this->crud->addField([    // Select2Multiple = n-n relationship (with pivot table)
      'label' => "Ответы",
      'type' => 'select2_multiple',
      'name' => 'answers', // the method that defines the relationship in your Model
      'entity' => 'question', // the method that defines the relationship in your Model
      'attribute' => 'title',
      'model' => "App\Models\Answer",
    ]);
  }
}


In the edit form it is displayed correctly: An error occurs when
5e5a8bc989e1d466324004.png

saving:
5e5a8bf2c72cd328961793.png
The error is more adequate in the logs:
[2020-02-29 16:05:49] local.ERROR: Call to undefined method App\Models\Question::question() {"exception" :"[object] (BadMethodCallException(code: 0): Call to undefined method App\\Models\\Question::question() at /project/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php :50)
[stacktrace]

and the dance begins with no function you add and everything goes wrong...
Can anyone come across how to cure this? Or can you advise a good admin panel with adequate support for links?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Kulaxyz, 2020-02-29
@Kulaxyz

Use hasOne and BelongsToMany (just the opposite, a question doesn't have many answers, one question belongs to many answers).
I myself had such a problem, I did not understand what it was connected with, but it only works in this direction.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question