Я получаю сообщение об ошибке ниже. Я впервые использую laravel. Я понятия не имею, как я могу изменить свои коды.
Ниже приведено мое окружение P C: Моя PHP версия: 7.4.2 Laravel версия: 7.3.0 ОС: Linux MySQL версия: 5.7.26
Спасибо за ваш совет заранее!
SQLSTATE[42S22]: Column not found: 1054 Unknown column ' id' in 'where clause' (SQL: select count(*) as aggregate from `posts` where ` id` = 52)
КомментарииКонтроллер. php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class CommentsController extends Controller
{
public function store(Request $request)
{
$params = $request->validate([
'post_id' => 'required|exists:posts, id',
'body' => 'required|max:2000',
]);
$post = Post::findOrFail($params['post_id']);
$post->comments()->create($params);
return redirect()->route('posts.show', ['post' => $post]);
}
}
web. php
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', 'PostsController@index')->name('top');
Route::resource('posts', 'PostsController', ['only' => ['create', 'store']]);
Route::resource('posts', 'PostsController', ['only' => ['create', 'store', 'show']]);
Route::resource('comments', 'CommentsController', ['only' => ['store']]);
show.blade. php
@extends('layout')
@section('content')
<div class="container mt-4">
<div class="border p-4">
<h1 class="h5 mb-4">
{{ $post->title }}
</h1>
<p class="mb-5">
{!! nl2br(e($post->body)) !!}
</p>
<section>
<h2 class="h5 mb-4">
Comment
</h2>
<form class="mb-4" method="POST" action="{{ route('comments.store') }}">
@csrf
<input name="post_id" type="hidden" value="{{ $post->id }}">
<div class="form-group">
<label for="body">
Context
</label>
<textarea
id="body"
name="body"
class="form-control {{ $errors->has('body') ? 'is-invalid' : '' }}"
rows="4"
>{{ old('body') }}</textarea>
@if ($errors->has('body'))
<div class="invalid-feedback">
{{ $errors->first('body') }}
</div>
@endif
</div>
<div class="mt-4">
<button type="submit" class="btn btn-primary">
Add comments
</button>
</div>
</form>
@forelse($post->comments as $comment)
<div class="border-top p-4">
<time class="text-secondary">
{{ $comment->created_at->format('Y.m.d H:i') }}
</time>
<p class="mt-2">
{!! nl2br(e($comment->body)) !!}
</p>
</div>
@empty
<p>No comments yet.</p>
@endforelse
</section>
</div>
</div>
@endsection