Laravel CRUD,找不到编辑ID,但存在ID [英] Laravel CRUD, Edit and Delete ID Not Found, But ID Exist

查看:232
本文介绍了Laravel CRUD,找不到编辑ID,但存在ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是迁移.

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();
        });

这是模特.

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');
    }

这是控制器编辑代码.

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

这是路线.

此处

主要错误始终来自未找到的ID.即使我的所有代码都是从第一个代码源复制并粘贴的,但是第一个源代码都运行良好.第三个源代码等等运行得很好.问题的根源始终在第二个表中,即使我更改了它的名称或使其从一开始就始终保持相同.是否存在拼写错误或区分大小写或我的代码有问题?

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?

这是Web浏览器中的结果 此处

this is the result in the web browser here

推荐答案

您正在尝试实施

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

您的路线就是这样

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'));
}

它会自动获取模型记录,如果找不到记录,则将返回404错误.有关详细信息,请检查上面的链接

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

这篇关于Laravel CRUD,找不到编辑ID,但存在ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆