-1

When i'm trying to create a Rating in RatingCrudController i'm getting an error Call to undefined method App\Models\Rating::rateable_id() in function setupCreateOperation.

protected
function setupCreateOperation() {
  $user = backpack_user();
  CRUD::setValidation(RatingStoreCrudRequest::class);
  $this - > crud - > addFields([
    [
      'name' => 'value',
      'label' => 'Note',
      'type' => 'number'
    ],
    [
      'name' => 'comment',
      'label' => 'Commentaire',
      'type' => 'text'
    ],
    [
      'name' => 'from_user',
      'type' => 'hidden',
      'default' => $user - > id
    ]
  ]);
  $this - > crud - > addField([
    'name' => 'rateable_type',
    'label' => 'Rateable Type',
    'type' => 'select_from_array',
    'options' => [
      'App\Models\User' => 'User',
      'App\Models\Estate' => 'Estate',
    ],
    'allows_null' => false,
    'default' => 'App\Models\User',
  ]);

  $this - > crud - > addField([
    'name' => 'rateable_id',
    'label' => 'Rating Target',
    'type' => 'select',
    'options' => function($query) {
      // Fetch options based on selected rateable_type
      if (request('rateable_type') === 'user') {
        return User::pluck('firstname', 'id');
      } else {
        return Estate::pluck('title', 'id');
      }
    },
  ]);

}

However, i have defined the rateable relation inside the Model Rating :

public function rateable() 
{
  return $this - > morphTo();
}

And the ratings relation in the models User and Estate:

publicfunction ratings() 
{
  return $this - > morphMany(Rating::class, 'rateable');
}

How can i fix it ?

Abdo Rabah
  • 1,670
  • 2
  • 15
  • 31
  • I think from the error that Backpack is assuming a relationship. You can avoid it by adding `entity => false` to your fields definition. – Pedro X Aug 17 '23 at 08:47

0 Answers0