Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.19k stars 898 forks source link

[Bug] Cannot find 'model_function' field view in any of the regular locations. #5547

Closed altayevrim closed 4 months ago

altayevrim commented 5 months ago

Bug report

What I did

I ran composer update and updated backpack

backpack/crud: 6.7.11 => 6.7.17, backpack/logmanager: v5.0.1 => v5.0.2, backpack/pro: 2.1.13 => 2.2.4,

When I try to edit a model that has "model_function" it gives this error.

HTTP 500
Cannot find 'model_function' field view in any of the regular locations.

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug... is it still there? That was the problem actually

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP VERSION:

8.3.8

PHP EXTENSIONS:

Core, date, libxml, openssl, pcre, sqlite3, zlib, bcmath, bz2, calendar, ctype, curl, dba, dom, hash, FFI, fileinfo, filter, ftp, gd, gmp, json, iconv, SPL, session, standard, mbstring, igbinary, imagick, imap, intl, ldap, exif, mysqlnd, mysqli, pcntl, PDO, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, random, readline, redis, Reflection, shmop, SimpleXML, soap, sockets, sodium, sysvmsg, sysvsem, sysvshm, tokenizer, xml, xmlreader, xmlwriter, xsl, zip, zstd, Zend OPcache

LARAVEL VERSION:

11.13.0.0

BACKPACK PACKAGE VERSIONS:

backpack/activity-log: 2.0.4 backpack/basset: 1.3.4 backpack/crud: 6.7.17 backpack/filemanager: 3.0.8 backpack/generators: v4.0.5 backpack/logmanager: v5.0.2 backpack/pro: 2.2.4 backpack/revise-operation: 2.0.0 backpack/settings: 3.1.1 backpack/theme-tabler: 1.2.10

altayevrim commented 5 months ago

In order to replicate from a blank project; just a fresh install with backpack pro, I tried with it and I have the same result here.

Use these and just click edit. You'll see the error.

UserCrudController.php

<?php

namespace App\Http\Controllers\Admin;

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

/**
 * Class UserCrudController
 * @package App\Http\Controllers\Admin
 * @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
 */
class UserCrudController 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\User::class);
        CRUD::setRoute(config('backpack.base.route_prefix') . '/user');
        CRUD::setEntityNameStrings('user', 'users');

        CRUD::column('name');
        CRUD::column('email');
        CRUD::column('email_verified_at');

        CRUD::field('name');
        CRUD::addField([
            'name' => 'remember_token',
            'label' => 'Remember Token',
            'type' => 'model_function',
            'function_name' => 'getRememberToken',
        ]);
    }

    /**
     * Define what happens when the List operation is loaded.
     *
     * @see  https://backpackforlaravel.com/docs/crud-operation-list-entries
     * @return void
     */
    protected function setupListOperation()
    {

        /**
         * Columns can be defined using the fluent syntax:
         * - CRUD::column('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(UserRequest::class);

        /**
         * Fields can be defined using the fluent syntax:
         * - CRUD::field('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();
    }
}

User Model

<?php

namespace App\Models;

use Backpack\CRUD\app\Models\Traits\CrudTrait;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use CrudTrait;
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'email_verified_at' => 'datetime',
            'password' => 'hashed',
        ];
    }

    public function getRememberToken()
    {
        return $this->remember_token;
    }
}
pxpm commented 4 months ago

Hello @altayevrim

There must be some confusion here, there is no model_function field in Backpack as far as I am aware. https://backpackforlaravel.com/docs/6.x/crud-fields

If it's a custom field you created, make sure you placed it in your resources/views/vendor/backpack/crud/fields/your_custom_field.blade.php. That way Backpack will know where it is 👍

I am going to close this, as I don't think there is any bug, let me know if I am wrong and I can re-open.

Cheers

altayevrim commented 4 months ago

Well then there was a bug in the 6.7.11 that was allowing this since everything works with this composer.json file configuration can be seen here;

https://shottr.cc/s/1ICG/SCR-20240708-lvp.png

And It was working perfectly fine. Just use the example I showed you with 6.7.11 and you'll see.

Also IMHO field should be also added since there is a column for that.