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 ?