I'm new to laravel and I'm learning it from laracast. Here is my problem, I'm creating a comment form and it's php code looks like this:
<section >
<!-- Post form -->
<form method="POST" action="/post/{{ $post->slug }}/comments" >
@csrf
<header >
<img src="https://i.pravatar.cc/100?id={{ auth()->id() }}" alt="" width="40" height="40" >
<h2 >Want to participate?</h2>
</header>
<div >
<textarea
name="body"
cols="30" rows="10"
placeholder="Quick,think of something to say!" ></textarea>
</div>
<div>
<button type="submit" >Post</button>
</div>
this is the corresponding route:
Route::post('post/{post:slug}/comments',[PostCommentsController::class, 'store']);
Controller:, and I suspect there could be something wrong here 'user_id'=> request()->user()->id
, and I tried numerous ways for this approach like auth()->id, Auth::user()->id
<?php
namespace App\Http\Controllers;
use App\Models\Post;
class PostCommentsController extends Controller
{
public function store(Post $post){
request()->validate([
'body'=>'required'
]);
$post->comments()->create([
'user_id'=> request()->user()->id,
'body' => request('body')
]);
return back();
}
}
and this the migration table for comment
Schema::create('comments', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->text('body');
$table->timestamps();
migration table for post:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->foreignId('category_id');
$table->string('slug')->unique();
$table->string('title');
$table->text('excerpt');
$table->text('body');
$table->timestamps();
$table->timestamp('published_at')->nullable();
});
If I click on post button I get the above error,tried my best to solve this problem but I couldn't. Can someone help me what's wrong with my code ?. My question may look naive as I'm new to stackoverflow community
CodePudding user response:
use this code for controller
class PostCommentsController extends Controller
{
public function store(Post $post){
request()->validate([
'body'=>'required'
]);
$post->comments()->create([
'user_id'=> optional(auth()->user())->id,
'body' => request('body')
]);
return back();
}
}
user must logged in
CodePudding user response:
first you must logged in
and in your route you must define your middleware if you are trying to get authenticated user's id like this
Route::post('post/{post:slug}/comments',[PostCommentsController::class, 'store'])->middleware('auth');
after that in your method/function inside controller use 'Request' class(not model class name) when you try to retrieve input from form
Laravel's 'Illuminate\Http\Request' class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request.
<?php
namespace App\Http\Controllers;
use App\Models\Post;
use Illuminate\Http\Request; //don't forget this
class PostCommentsController extends Controller
{
public function store(Request $request){
request()->validate([
'body'=>'required'
]);
$post->comments()->create([
'user_id'=> auth()->user()->id,
'body' => request('body')
]);
return back();
}
}