Error Route::resource() in Laravel?

Error occurs

Missing required parameters for [Route: post.show] [URI: post/{post}]. (View: E:\ospanel\OSPanel\domains\akademka\resources\views\posts\index.blade.php)

when I changed the routes with the original values
Route::get('post', 'PostController@index')->name('post.index');
Route::get('post/show/{id}', 'PostController@show')->name('post.show');
Route::get('post/create', 'PostController@create')->name('post.create');
Route::get('post/edit/{id}', 'PostController@edit')->name('post.edit');
Route::post('post/', 'PostController@store')->name('post.store');
Route::patch('post/show/{id}', 'PostController@update')->name('post.update');
Route::delete('post/{id}', 'PostController@destroy')->name('post.destroy');

the route
Route::resource('/post', 'PostController');
In the controller, all methods are native(if need be, throw in the comments)
Here index.blade.php

@extends('layouts.layout')

@section('content')
@if(isset($_GET['search']))
 @if( count($posts)>0)
 <h2>search Result:</h2>
 <p class="lead">found {{ count($posts) }} posts</p>
@else
 <h2>request <?php echo $_GET['search']?> nothing found</h2>
 <a class="btn btn-outline-primary" href="{{ route('post.index') }}"> All posts </a>
@endif
@endif

 <div class="row">
 @foreach($posts as $post)
 <div class="col-6">
 <div class="card">
 <div class="card-header"><h2>{{ $post->short_title }}</h2></div>
 <div class="card-body">
 <div class="card-img" style="background-image: url({{$post->img ?? asset('img/who.jpg')}})"></div>
 <div class="card-author">Author: {{$post->name}}</div>
 <a href="{{route('post.show', ['id'=> $post->post_id])}}" class="btn btn-outline-primary">View post</a>
</div>
</div>
</div>
@endforeach
</div>

@if(!isset($_GET['search']))
 {{ $posts->links() }}
@endif

@endsection

April 19th 20 at 12:26
2 answers
April 19th 20 at 12:28
Solution
In the resource routes don't have to pass the parameter names.
route('posts.show', $post)
I.e. either the resource or its identifier if not a resource. Larabel machine will bind and the controller will have the desired resource
public function show(Post $post)
{
dd($post);
}

If no key id and slug for example to define in the model so
protected $primaryKey = 'slug';
In the database do not need to give a prefix for fields of type $post->post_id. The right thing to do $post->id, name, active, etc. because post and so always will be before them in the name of the variable.
And resources need to call the post, and posts in the ranting
Route::resource('posts', 'PostController');
In the database do not need to give a prefix for fields of type $post->post_id

About it many broken copies. But even if you not specify id, the model name is the primary key field can be overridden.
https://laravel.com/docs/7.x/eloquent#eloquent-mod... see primary keys. Then he will be substituted in the station, without any additional overrides in getRouteKeyName(). - junius40 commented on April 19th 20 at 12:31
@junius40, corrected the answer, thank you. About the key I do not know who is there spear breaks post_id saw only beginners in less examples -modules id and Larval by default is waiting for Eid. Well, the code itself is much more logical looks like with $post->id than $post->post_id enity_id when I see I immediately think about the relationship - emerald_Trantow commented on April 19th 20 at 12:34
@emerald_Trantow,
post_id saw only beginners

it is not in beginner/intermediate, these spears regardless lurvely go. In laravale by default the field name is id, and crap on the description in the model less. In the latter also zaslugifunctions to quickly create such a field (named id, type int, primary key and auto-increment) in the migrations added.

The fact that the key field and fields in related tables are the same. In some cases it's a little easier life. For example, when compounds. But it is certainly a matter of taste.

For example, I prefer a UUID as the key. And the name - id. - junius40 commented on April 19th 20 at 12:37
April 19th 20 at 12:30
The easiest way is the route to use action and pass in the model instance:
action('PostController@show', $post);
What is the easier route('posts.show', $post)? - emerald_Trantow commented on April 19th 20 at 12:33
@emerald_Trantow, At the time of writing this answer, like the answer chosen by the decision about route('posts.show', $post) were not. Well, this feature on the actions of the controllers is hung, not on a named route. If the naming Convention routes Route::resource will change - it will not break. - junius40 commented on April 19th 20 at 12:36
@junius40, everything was there where you are. If you think so, they can do anything to change it. For example, to cancel the action helper - emerald_Trantow commented on April 19th 20 at 12:39

Find more questions by tags Laravel