簡體   English   中英

Laravel 背包列表功能不起作用

[英]Laravel Backpack List function not working

當我加載頁面時,它給我一個錯誤並告訴我重新加載。
但是,當我將數據添加到數據庫時,它可以正常工作。
所以,基本上我的列表功能不起作用,我不知道為什么。
我檢查了拼寫錯誤,這不可能是更新,因為我所有其他 CRUD 的工作都使用相同的方法。

控制器 :

<?php

namespace App\Http\Controllers\Admin;

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

/**
 * Class ProjectCrudController
 * @package App\Http\Controllers\Admin
 * @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
 */
class ProjectCrudController 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\Project');
        $this->crud->setRoute(config('backpack.base.route_prefix') . '/project');
        $this->crud->setEntityNameStrings('project', 'projects');
    }

    protected function setupListOperation()
    {      
        $this->crud->setColumns([
            'name' => 'status', 
            'type' => 'text', 
            'label' => 'Status'
        ]);
        $this->crud->setColumns([
            'name' => 'topic', 
            'type' => 'text', 
            'label' => 'Topic'
        ]);
        $this->crud->setColumns([
            'name' => 'leader', 
            'type' => 'text', 
            'label' => 'Leader'
        ]);
        $this->crud->setColumns([
            'name' => 'email', 
            'type' => 'text', 
            'label' => 'Name'
        ]);
        $this->crud->setColumns([
            'name' => 'tags', 
            'type' => 'text', 
            'label' => 'Tags'
        ]);
    }

    protected function setupCreateOperation()
    {
        $this->crud->setValidation(ProjectRequest::class);
        $this->crud->addField([
            'name' => 'status', 
            'type' => 'text', 
            'label' => 'Status'
        ]);
        $this->crud->addField([
            'name' => 'topic', 
            'type' => 'text', 
            'label' => 'Topic'
        ]);
        $this->crud->addField([
            'name' => 'leader', 
            'type' => 'text', 
            'label' => 'Leader'
        ]);
        $this->crud->addField([
            'name' => 'email', 
            'type' => 'text', 
            'label' => 'Name'
        ]);
        $this->crud->addField([
            'name' => 'tags', 
            'type' => 'text', 
            'label' => 'Tags'
        ]);
    }

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

    public function store()
    {
        $this->crud->hasAccessOrFail('create');

        // execute the FormRequest authorization and validation, if one is required
        $request = $this->crud->validateRequest();

        // insert item in the db
        $item = $this->crud->create($this->crud->getStrippedSaveRequest());
        $this->data['entry'] = $this->crud->entry = $item;

        // show a success message
        \Alert::success(trans('backpack::crud.insert_success'))->flash();

        // save the redirect choice for next time
        $this->crud->setSaveAction();

        return $this->crud->performSaveAction($item->getKey());
    }
}


模型:

<?php

namespace App\Models;

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

class Project extends Model
{
    use CrudTrait;

    /*
    |--------------------------------------------------------------------------
    | GLOBAL VARIABLES
    |--------------------------------------------------------------------------
    */

    protected $table = 'projects';
    // protected $primaryKey = 'id';
    // public $timestamps = false;
    protected $guarded = ['project_id'];
    protected $fillable = ['topic', 'status', 'leader', 'email', 'tags'];

    // protected $fillable = [];
    // protected $hidden = [];
    // protected $dates = [];

    /*
    |--------------------------------------------------------------------------
    | FUNCTIONS
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | RELATIONS
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | SCOPES
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | ACCESSORS
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | MUTATORS
    |--------------------------------------------------------------------------
    */
}

要求:

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;
use Illuminate\Foundation\Http\FormRequest;

class ProjectRequest 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 [
            'topic' => 'required|min:5|max:255',
            'status' => '',
            'leader' => 'required|min:5|max:255',
            'email' => 'required|min:5|max:255',            
            'tags' => 'required|min:5|max:255'
        ];
    }

    /**
     * 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 [
            //
        ];
    }
}

受保護的 $primaryKey = 'project_id';

數據庫有 project_id 而不僅僅是 id,所以這應該有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM