V
V
Vyacheslav Shevchenko2020-05-04 22:19:10
Laravel
Vyacheslav Shevchenko, 2020-05-04 22:19:10

Why doesn't laravel instantiate the class in the controller?

Hello.
I write this code:
Route

Route::get('article/{id}/edit', 'Cms\[email protected]')->name('cms.blog.article.edit');

Controller
<?php
namespace App\Http\Controllers\Cms;

use App\Http\Controllers\Controller;
use App\Models\Blog\BlogArticle;

class BlogArticleController extends Controller
{
    public function edit(BlogArticle $article)
    {
        dd($article);
    }
}


At the same time, there is nothing in $article. It seems that he did not even understand which model was being addressed.
5eb06eb1e1c95896958525.png

However, if you do:
<?php
namespace App\Http\Controllers\Cms;

use App\Http\Controllers\Controller;
use App\Models\Blog\BlogArticle;

class BlogArticleController extends Controller
{
    public function edit($article)
    {
        dd(BlogArticle::find($article));
    }
}

That's all right.
5eb06ebc1ca3e525510426.png
Why can this be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Shevchenko, 2020-05-04
@WebDev2030

Everything turned out to be very simple:
the name of the parameter should not be $article, but $id. {id} is written in the route

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question