简体   繁体   中英

Laravel Backpack CRUD not working

i followed this tutorial to setup a CRUD example. But i can't get it to work and i don't know why. here is my code

SomeitemCrudController.php

<?php

namespace App\Http\Controllers\Admin;

use Backpack\CRUD\app\Http\Controllers\CrudController;

// VALIDATION: change the requests to match your own file names if you need form validation
use App\Http\Requests\SomeitemRequest as StoreRequest;
use App\Http\Requests\SomeitemRequest as UpdateRequest;

class SomeitemCrudController extends CrudController
{

    public function setUp()
    {
        /*
        |--------------------------------------------------------------------------
        | BASIC CRUD INFORMATION
        |--------------------------------------------------------------------------
        */
        $this->crud->setModel("App\Models\Someitem");
        $this->crud->setRoute("admin/someitem");
        $this->crud->setEntityNameStrings('someitem', 'someitems');

        /*
        |--------------------------------------------------------------------------
        | BASIC CRUD INFORMATION
        |--------------------------------------------------------------------------
        */

        $this->crud->setFromDb();

        // $this->crud->setColumns(['nama']);
        $this->crud->addField([
            'nama' => 'Nama',
            'keterangan' => 'Keterangan',
            'harga' => 'Harga'
        ]);
    }

    public function store(StoreRequest $request)
    {
        $redirect_location = parent::storeCrud();
        return $redirect_location;
    }

    public function update(UpdateRequest $request)
    {
        $redirect_location = parent::updateCrud();
        return $redirect_location;
    }
}

the model, Someitem.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Backpack\CRUD\CrudTrait;

class SomeItem extends Model
{
    use CrudTrait;
    //

    protected $table = 'someitem';
    protected $fillable = ['nama', 'keterangan', 'harga'];
    public $timestamps = true;
}

the request, SomeitemRequest.php

<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class SomeitemRequest extends \Backpack\CRUD\app\Http\Requests\CrudRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return \Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            // 'name' => '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 [
            //
        ];
    }
}

and then, the routes

<?php

Route::group([
    'prefix' => config('backpack.base.route_prefix', 'admin'),
    'middleware' => ['admin'],
    'namespace' => 'Admin'
], function() {
    CRUD::resource('pelayanan', 'Admin\PelayananCrudController');
    Route::get('/test', function () {
        return view('welcome');
    });
});

i can access http://localhost/myapp/public/admin/test successfully, but i can't access http://localhost/myapp/public/admin/someitem , it always return Error 500

i'm new to laravel (and PHP), any helps is appreciated. thanks!

I want to make my answer very detailed, so that's why I've started it from the beginning. The version for backpack CRUD I use is ^3.2.

  1. look at this file your-project/vendor/backpack/crud/src/CrudServiceProvider.php Yes I know that class name is not CRUD but CrudServiceProvider . That is because they have register method in CrudServiceProvider and in it they do $loader->alias('CRUD', \\Backpack\\CRUD\\CrudServiceProvider::class); which makes CRUD class alias to CrudServiceProvider . To know more about how it works inside read through Laravel Service Providers , Service Container and Facades .

  2. So we've figured out that CRUD::resource() actually means CrudServiceProvider::resource() , so get to this method, there you will find only one line return new CrudRouter($name, $controller, $options); as far as I know (if I'm wrong correct me) it's called Factory pattern/method . So go to CrudRouter constructor ( Github ). Read through it. So the basic thing it does is instead of this

    CRUD::resource('pelayanan', 'Admin\\PelayananCrudController');

it will put something like this

Route::post('pelayanan/search', [
        'as' => 'crud.pelayanan.search',
        'uses' => 'Admin\PelayananCrudController@search',
    ]);

    Route::get('pelayanan/reorder', [
        'as' => 'crud.pelayanan.reorder',
        'uses' => 'Admin\PelayananCrudController@reorder',
    ]);

    Route::post('pelayanan/reorder', [
        'as' => 'crud.pelayanan.save.reorder',
        'uses' => 'Admin\PelayananCrudController@saveReorder',
    ]);

    Route::get('pelayanan/{id}/details', [
        'as' => 'crud.pelayanan.showDetailsRow',
        'uses' => 'Admin\PelayananCrudController@showDetailsRow',
    ]);

    Route::get('pelayanan/{id}/translate/{lang}', [
        'as' => 'crud.pelayanan.translateItem',
        'uses' => 'Admin\PelayananCrudController@translateItem',
    ]);

    Route::get('pelayanan/{id}/revisions', [
        'as' => 'crud.pelayanan.listRevisions',
        'uses' => 'Admin\PelayananCrudController@listRevisions',
    ]);

    Route::post('pelayanan/{id}/revisions/{revisionId}/restore', [
        'as' => 'crud.pelayanan.restoreRevision',
        'uses' => 'Admin\PelayananCrudController@restoreRevision',
    ]);

You said that you can't acces you public/admin/someitem and that's true, because you don't have route for it (actually check routes in cli with artisan route:list ). But Backpack developers made one thing (don't actually know why) they put extra routes in destructor method by calling

`Route::resource($this->name, $this->controller, $options_with_default_route_names);`. 

And by it should actually work, if it's not, check how index (CrudController) behaves.

One more thing maybe you forgot to php artisan vendor:publish --provider="Backpack\\Base\\BaseServiceProvider" and you don't have view files and that's why you get 500 error.

If you have more questions, just ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM