Čendi
Čendi

Reputation: 39

Missing required parameters in route Laravel 5.7

what could be wrong with this:

route

Route::get('admin/view-news/{id}', 'AdminNewsController@show')->name('admin.view-news');

controller

public function index()
    {
        $news = News::all();
        return view('admin.news.news');
    }

public function show($id)
    {
        $news = News::Find($id);
        return view('admin.news.view_news')->with('news', $news);

in controller i tried this as well:

    `return view('admin.news.view_news', ['news' => News::findOrFail($id)])`;

view

{{ route ('admin.view-news') }}

An important note is, almost the same thing for users is working:

route:

Route::get('/user/{id}', 'UsersController@show');

controller:

public function index()
    {
        $users = User::orderBy('name', 'asc')->paginate(30);

        return view('admin.users.users')->with('users', $users);
    }


 public function show($id)
    {
        $user = User::find($id);

        return view('admin.users.view_user')->with('user', $user);
    }

The error is:

Missing required parameters for [Route: admin.view-news] [URI: admin/view-news/{id}].

What am i missing here, how im not getting the id, and in users controller i do, with almost the same code? Thanks.

Upvotes: 1

Views: 5809

Answers (3)

ganji
ganji

Reputation: 844

I found a better solution: In your blade file do like this

<a href="{{route('admin.view-news',"$id")}}">
  View
</a>

with this route , in route file

Route::get('admin/view-news/{id}', 'AdminNewsController@show');

$id is sent form your controller with compact

return view('viewPage', compact('id'));

Upvotes: 0

Ayorinde-codes
Ayorinde-codes

Reputation: 51

This worked for me:

Route::get('admin/view-news/{id?}', 'AdminNewsController@show')->name('admin.view-news');

for the view:

{{ route ('admin.view-news', ['id'=> $id ]) }}

Upvotes: 0

Kiran Patel
Kiran Patel

Reputation: 379

You should try this

{{ route('admin.view-news', $id) }}

Instead of

{{ route('admin.view-news') }}

Upvotes: 3

Related Questions