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

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('AppRuangan','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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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', '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="26745347484147484c48556549485254494a4a43546643424f52">[email protected]</a>');

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x