[Bug] Newly created CRUD causes getRelated() error
See original GitHub issueBug report
What I did
Created a CRUD using devtools. I have created a migration to go with it, as well as the model, crud controller, migration and request. I’ve created a few CRUDS using devtools, this is the first one I’m having issues with. The other ones I’ve created have all had relationships defined in them though, so possible that is the key difference.
REQUEST
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;
class CubeRefreshRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
// only allow updates if the user is logged in
return backpack_auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'refresh_id' => 'required|string',
'refresh_status' => 'required|string',
'refresh_details' => 'required|string',
'refresh_time' => 'required',
];
}
/**
* Get the validation attributes that apply to the request.
*
* @return array
*/
public function attributes()
{
return [
//
];
}
/**
* Get the validation messages that apply to the request.
*
* @return array
*/
public function messages()
{
return [
//
];
}
}
CONTROLLER
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\CubeRefreshRequest;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
/**
* Class CubeRefreshCrudController
* @package App\Http\Controllers\Admin
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
*/
class CubeRefreshCrudController 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;
/**
* Configure the CrudPanel object. Apply settings to all operations.
*
* @return void
*/
public function setup()
{
CRUD::setModel(\App\Models\CubeRefresh::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/cube-refresh');
CRUD::setEntityNameStrings('cube refresh', 'cube refreshes');
}
/**
* Define what happens when the List operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-list-entries
* @return void
*/
protected function setupListOperation()
{
CRUD::column('refresh_id');
CRUD::column('refresh_status');
CRUD::column('refresh_details');
CRUD::column('refresh_time');
/**
* Columns can be defined using the fluent syntax or array syntax:
* - CRUD::column('price')->type('number');
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
*/
}
/**
* Define what happens when the Create operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-create
* @return void
*/
protected function setupCreateOperation()
{
CRUD::setValidation(CubeRefreshRequest::class);
CRUD::field('refresh_id');
CRUD::field('refresh_status');
CRUD::field('refresh_details');
CRUD::field('refresh_time');
/**
* Fields can be defined using the fluent syntax or array syntax:
* - CRUD::field('price')->type('number');
* - CRUD::addField(['name' => 'price', 'type' => 'number']));
*/
}
/**
* Define what happens when the Update operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-update
* @return void
*/
protected function setupUpdateOperation()
{
$this->setupCreateOperation();
}
}
MODEL
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class CubeRefresh extends Model
{
use \Backpack\CRUD\app\Models\Traits\CrudTrait;
use HasFactory;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'refresh_id',
'refresh_status',
'refresh_details',
'refresh_time',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'refresh_time' => 'datetime',
];
}
MIGRATION
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCubeRefreshesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('cube_refreshes', function (Blueprint $table) {
$table->id();
$table->string('refresh_id', 100);
$table->string('refresh_status', 50);
$table->string('refresh_details', 1000);
$table->dateTime('refresh_time');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('cube_refreshes');
}
}
What I expected to happen
To view the CRUD in the application.
What happened
When I go to /admin/cube-refresh I get the below error.
Call to undefined method App\Models\CubeRefresh::getRelated()
Illuminate\Database\Eloquent\Model::throwBadMethodCallException
D:\laragon\www\MyApp\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php:71
* @return mixed
*
* @throws \BadMethodCallException
*/
protected function forwardDecoratedCallTo($object, $method, $parameters)
{
$result = $this->forwardCallTo($object, $method, $parameters);
if ($result === $object) {
return $this;
}
return $result;
}
/**
* Throw a bad method call exception for the given method.
*
* @param string $method
* @return void
*
* @throws \BadMethodCallException
*/
protected static function throwBadMethodCallException($method)
{
throw new BadMethodCallException(sprintf(
'Call to undefined method %s::%s()', static::class, $method
));
}
}
What I’ve already tried to fix it
Searched for any instances of getrelated() in my project, I can’t find any, this is a new CRUD on a system where devtools is working fine.
Backpack, Laravel, PHP, DB version
PHP VERSION:
PHP 7.4.19 (cli) (built: May 4 2021 14:24:38) ( ZTS Visual C++ 2017 x64 ) Copyright © The PHP Group Zend Engine v3.4.0, Copyright © Zend Technologies
LARAVEL VERSION:
v8.64.0@3337c029e1bb31d9712d27437cc27010ba302c9e
BACKPACK VERSION:
4.1.54@6f0ddffbe2017ef0e0d9becf5e9f422a81d94c45
Operating System and Server Setup
I encountered the problem above on my [Windows 10 ] machine, and my web server is set up using [LARAGON].
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (7 by maintainers)
Top GitHub Comments
@hakkahio I have good news for you. Pedro’s PR #3606 did provide a fix for this, it’s been merged to
master
and is available to you right now with acomposer update
. Additionally, I’ve submitted https://github.com/Laravel-Backpack/CRUD/pull/3926 and https://github.com/Laravel-Backpack/CRUD/pull/3927 which fix the same problem inside the ShowOperation. They all work now, take a look:So that’s the good news 🎉 We have a workaround for you to use
refresh_id
for both fields and columns - you just have to specifyentity
tofalse
for Backpack to no longer look for a relationship. But PLEASE NOTE that this workaround only works with the array syntax. So:Hope it helps! I’ll close the issue, let me know if that doesn’t fix it for you and we’ll reopen.
@pxpm , @promatik … I say we now have a workaround…
'entity' => false
works for both fields and columns, for the Create, Update and Show operations. The only thing left to do would be to… make this happen in fewer cases. Which would be nice, but… not a priority at the moment. We’ll increase the priority if more people stumble into this, but for the moment… it’s one person in one year that had the problem, so I don’t think it qualifies for a high importance. If you’re interested in pursuing the ideas above:'entity' => false
automatically in DevTools;then please open an issue with that specific suggestion, so we don’t forget to do it, and link to this issue. BUT. They’re not MUSTs. They’re SHOULDs/COULDs. For now let’s focus on getting 4.2 out the door.
Cheers!
Hi @hakkahio! Indeed, the problem is in
refresh_id
. Backpack is trying to infer a relationship because of the_id
and since the methodrefresh
exists in theIlluminate\Database\Eloquent\Model
it is causing that issue.@pxpm I tried to add a
entity => false
on the field, but it was not enough to solve this, do you know what am I missing?Anyway, should DevTools prevent this situations? by adding the
->entity(false)
when the field has_id
and is not a relation?