I
I
iiiideb2020-04-17 01:29:09
Laravel
iiiideb, 2020-04-17 01:29:09

Error with Route::resource() in Laravel?

The error is

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

when changed routes from original value

Route::get('post/', '[email protected]')->name('post.index');
Route::get('post/show/{id}', '[email protected]')->name('post.show');
Route::get('post/create', '[email protected]')->name('post.create');
Route::get('post/edit/{id}', '[email protected]')->name('post.edit');
Route::post('post/', '[email protected]')->name('post.store');
Route::patch('post/show/{id}', '[email protected]')->name('post.update');
Route::delete('post/{id}', '[email protected]')->name('post.destroy');

on the route
Route::resource('/post', 'PostController');
In the controller, all methods are native (if necessary, I will throw them in the comments)
Here is index.blade.php

@extends('layouts.layout')

@section('content')
  @if(isset($_GET['search']))
    @if( count($posts)>0)
      <h2>Результат поиска:</h2>
      <p class="lead">Всего найдено {{ count($posts) }} постов</p>
    @else
      <h2>По запросу <?php echo $_GET['search']?> ничего не найдено</h2>
      <a class="btn btn-outline-primary" href="{{ route('post.index') }}"> Все посты </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">Автор: {{$post->name}}</div>
          <a href="{{route('post.show', ['id'=> $post->post_id])}}" class="btn btn-outline-primary">Посмотреть пост</a>
        </div>
      </div>
    </div>
    @endforeach
  </div>

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

@endsection

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jazzus, 2020-04-17
@iiiideb

Resource routes do not need to pass parameter names. Those. either the resource or its identifier if the resource does not exist. Laravel will automatically connect and the controller will already have the required resource
route('posts.show', $post)

public function show(Post $post)
{
  dd($post);
}

If key is not id, but for example slug, then define it in the model like this . In the database, you do not need to give prefixes for fields like $post->post_id. It's correct to do $post->id, name, active, etc. because post will always precede them in the variable name. And resources should be called not post, but posts in routes
protected $primaryKey = 'slug';
Route::resource('posts', 'PostController');

A
Anton Anton, 2020-04-17
@Fragster

The easiest way is to routeuse actionand pass an instance of the model to it instead:
action('[email protected]', $post);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question