本文介绍了laravel资源url取决于模型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为模型,迁移,资源控制器运行此命令. php artisan make:model QuestionAnswer -mc -r ..

i am run this command for model, migration, resource controller. php artisan make:model QuestionAnswer -mc -r ..

资源路线 Route :: resource('faq','QuestionAnswerController');

我的编辑功能

public function edit(QuestionAnswer $questionAnswer)
    {
        // return $questionAnswer;
        return view('backend.faq.edit',get_defined_vars());
    }

修改路线 {{route('admin.faq.edit',$ questionAnswer-> id)}}

编辑函数返回$ questionAnswer 返回null

Edit function return $questionAnswer return null

下面的图片

当我更改型号名称之类的资源路线时 Route :: resource('question-answer','QuestionAnswerController');

编辑功能返回$ questionAnswer 返回 object 平均预期输出..

edit function return $questionAnswer return object mean expected output ..

图片

问题

laravel资源url是否取决于模型?如果我在 Route :: resource('faq','QuestionAnswerController');的某个地方输入错误; 请发表评论,我将删除我的问题.

laravel resource url depend on model or something ?if i am wrong somewhere for Route::resource('faq','QuestionAnswerController'); please comment i will remove my question..

推荐答案

由于您的路由参数为 question_answer ,因此请将控制器更改为:

Bacause your route parameter is question_answer, so change the controller to :

public function edit(QuestionAnswer $question_answer)
{
   dd($question_answer);
}

或者,您可以专门告诉资源应该将route参数命名为什么:

Alternatively, you can specifically tell the resource what the route parameter should be named :

Route::resource('faq','QuestionAnswerController')
           ->parameters(['faq' => 'questionAnswer']);

现在,您可以作为参数访问 $ questionAnswer :

Now you can access $questionAnswer as parameter :

public function edit(QuestionAnswer $questionAnswer)
{
   dd($questionAnswer);
}

命名资源路由参数的官方文档将找到 此处

The official documentation of Naming Resource Route Parameters will be found here

这篇关于laravel资源url取决于模型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 17:04