Laravel CRUD, Edit and Delete ID Not Found, But ID Exist

最后都变了- 提交于 2019-12-11 17:46:09

问题


Here is migration.

Schema::create('ruanganjns', function (Blueprint $table) {
            $table->increments('id');
            $table->string('kode_jenis_ruangan');
            $table->string('jenis_ruangan');
            $table->date('tgl_berlaku');
            $table->string('status');
            $table->timestamps();
        });

Here is model.

protected $table = 'ruanganjns';
    protected $fillable = ['kode_jenis_ruangan','jenis_ruangan','tgl_berlaku','status'];
    public $timestamps = true;
    public function Ruangan()
    {
        return $this->HasMany('App\Ruangan','id_ruangan');
    }

here is controller edit code.

public function edit(Ruanganjns $ruanganjns)
    {
        $ruanganjns = Ruanganjns::findOrFail($ruanganjns->id);
        return view('ruanganjns.edit', compact('ruanganjns'));
    }

here is the route.

here

The main error always comes from an ID not found. even though all of my code is copied and pasted from the first code source, but the first source code all goes well. and the third source code and so on goes well. the source of the problem is always in this second table even i change it name or make it from beginning it's always the same. are there misspellings or case sensitive or something wrong with my code?

this is the result in the web browser here


回答1:


Your are trying to implement Implicit model binding but you have some wrong variable name in controller action. It should match with the route placeholder like this

Your route is like this

Route::get('admin/ruangjns/{ruangjn}/edit', 'RuanganjnsController@edit');

Then your controller action code should be like this

public function edit(Ruanganjns $ruangjn)  //here $ruangjn should match with route placeholder
{
    return view('ruanganjns.edit', compact('ruangjn'));
}

It automatically fetch model record and if record not found then it will return 404 error. For details check the above link



来源:https://stackoverflow.com/questions/52472586/laravel-crud-edit-and-delete-id-not-found-but-id-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!