Duffman88's avatar

General error: 1364 Field 'article_id' doesn't have a default value

So what im trying to do is to save reply on article. After a lot of error i think i got to the final one. Basically im not sure how to pass article_id to data.

Controller

public function store(Request $request)
    
    {

        request()->validate(['body' => 'required']);

        Comment::create([
            'user_id' => auth()->id(),
            'body' => request('body'),
        ]);
        
        return redirect('/articles');
    }

route

Route::resource('articles.comments', 'CommentController');

or

// Route::post('articles/{article}/comments', 'CommentController@store');

view

<form class="comment-form" method="POST" action="/articles/{article}/comments">
@csrf                                            	
<textarea rows="18" cols="6" name="body" placeholder="Messages"></textarea>
<input type="submit" value="Submit">
</form><!-- .comment-form -->

migration

Schema::create('comments', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->constrained()->onDelete('cascade');
            $table->foreignId('article_id')->constrained()->onDelete('cascade');
            $table->text('body');
            $table->timestamps();
        });
0 likes
6 replies
Duffman88's avatar

Its gonna save it if I hard code article_id, but that's not i want ofc....

Duffman88's avatar

Sorry i forgot to include models. I declared the relationships but i wasnt sure how to pass it in controller

Article model

    public function comments()
    {
        return $this->hasMany(Comment::class);
    }


Comment model

    public function articles()
    {
        return $this->belongsTo(Articles::class, 'article_id');
    }

I did try what you recommended, and i got undefined variable $articles,

So i included them and my CommentController looks like

<?php

namespace App\Http\Controllers;

use App\Comment;
use App\Article;
use Illuminate\Http\Request;

class CommentController extends Controller
{

    public function store(Request $request, Article $article)
    
    {

        request()->validate(['body' => 'required']);

        $article->comments()->create([
            'user_id' => auth()->id(),
            'body' => request('body'),
        ]);
        
        return redirect('/articles');
    }

}

But now i get 404 with URL /articles/%7Barticle%7D/comments

frankielee's avatar

@duffman88, it should be /articles/{{$article->id}}/comments with double {{ }}. Since you are using Resources route, you can write like this

//example of route name : articles.comments.store
action = "{{route('the route name',$article->id)}}";

You can check the route name by using the command php artisan route:list

Duffman88's avatar

@frankielee Yes, those are the routes, but i replaced $article->id with $article since it was interfering.

action = "{{route('the route name',$article->id)}}";

so now its

action="{{route('articles.comments.store',$article)}}"

And now its working

Just to figure how to return back to that article

Thank you for all the help :).

frankielee's avatar

Np. If you are returning back to the same page,

	return redirect()->back();//redirect to the same page without any data
	return redirect()->back()->with("message","success");//redirect to the same page with data

Please or to participate in this conversation.