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');
Route::resource('/post', 'PostController');
@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
route('posts.show', $post)
public function show(Post $post)
{
dd($post);
}
protected $primaryKey = 'slug';
Route::resource('posts', 'PostController');
the route
to use action
and pass in the model instance: action('PostController@show', $post);
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. commented on April 19th 20 at 12:36Find more questions by tags Laravel
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
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