Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

deepu07's avatar
Level 11

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'article_id' cannot be null (SQL: insert into `comments` (`body`, `article_id`, `updated_at`, `created_at`) values (adding new comment, , 2017-12-22 03:21:15, 2017-12-22 03:21:15))

i have articles table and comments table. article_id is foriegn key in comments table. i cant save the article_id in reply comments table and im i getting this error. how to solve it? CommentController:

public function store(Article $article) { $this->validate(request(),['body' => 'required|min:2']);

        $article = Comment::create([
        'body' => request('body'),
        'article_id' => $article->id,
    ]);

    return back();
}

views.blade:

{{ csrf_field() }} Add Comment

Comment model:

class Comment extends Model { public $timestamps = TRUE; protected $table = 'comments';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'body',
    'article_id'
];
public function article()
{
    return $this->belongsTo(Article::class);
}

}

Route:

Route::post('/post/{post}/comment', 'CommentController@store');

Where I'm missing. Thanks in Advance

0 likes
32 replies
Jaytee's avatar

When using Route Model Binding, Laravel expects the wildcard in the route to match the same as the method parameters.

Your method parameter is accepting an instance of Article Article $article, but your route is defined as accepting a {post}. Change {post} to {article}

1 like
Snapey's avatar

I would suggest also

    $comment = $article->comment->create([
        'body' => request('body'),
    ]);
1 like
Snapey's avatar

Thats a problem with your form post action.

Show your form open tag

deepu07's avatar
Level 11

//form tag

<div class="card">
    <div class="card-block">
        <form method="POST" action="{{ url('/post/{$article->id}/comment') }}"> 
            {{ csrf_field() }}
            <div class="form-group">
                <textarea name="body" placeholder="your comment here." class="form-control">  </textarea>
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-primary">Add Comment</button>
            </div>
        </form>
    </div>
Snapey's avatar

This

action="{{ url('/post/{$article->id}/comment') }}">

is just being treated as a literal string. Use double quotes if you want to insert the variable in the string

action="{{ url("/post/{$article->id}/comment") }}">
deepu07's avatar
Level 11

I got this after changing "Undefined variable: article (View: C:\xampp7\htdocs\Lara\resources\views\article\show.blade.php)

Rightnow form tag is

action="{{ url("/post/{$article->id}/comment") }}"

Snapey's avatar

So, do you have $article in your view? You seem confused if things should be called post or article?

deepu07's avatar
Level 11

@snapey I'm totally messup! Can you forward your github gmail. I'll add contributor to my repo. So that you can take a look. That's easy. Is it fine for you?

Snapey's avatar

Its because you are calling your article $articles (plural) when in fact it is a single article.

in your controller change $articles to $article and then change it in the view where you have articles, for instance $articles->body. It will then be correct when you use it in the form action.

deepu07's avatar
Level 11

do you mean I need to change in articlecontroller or commentcontroller??

Snapey's avatar

Its not public

Look, in the controller that shows the view containing the form. You pass $articles. This is incorrect. logically it should be $article since it is a single model.

If you need more, from the code you posted on SO

public function show(Article $articleId)
{ 
    $articles = Article::find($articleId);

    return view('article.show',compact('articles'));
}

change like below

public function show(Article $articleId)
{ 
    $article = Article::find($articleId);

    return view('article.show',compact('article'));
}

Then fix the view

<div class="container">
    <h1> {{ $article->title }} </h1>   // change $articles to $article
    <p> {{ $article->body }} </p>  // and here


then in the form action $article->id makes sense

deepu07's avatar
Level 11

Property [title] does not exist on this collection instance. (View: C:\xampp7\htdocs\Lara\resources\views\article\show.blade.php)

Snapey's avatar
Snapey
Best Answer
Level 122

in your article controller

    public function show(Article $articleId)
    {
        // $articles = Article::find($id);
        // return view('article.show',compact('articles'));
         $article = Article::find($articleId);
    return view('article.show',compact('article'));

you type hint Article so laravel will find it for you, then when you do $article = Article::find($articleId); the thing you pass into find is actually the article and not its id. so the find fails and you pass null to the view

just use the passed in article like


public function show(Article $article)
{
    return view('article.show',compact('article'));
}

deepu07's avatar
Level 11

That's fine but why I'm getting this thing ""Property [title] does not exist on this collection instance. (View: C:\xampp7\htdocs\Lara\resources\views\article\show.blade.php)""

Mikeysauce's avatar
    public function show(Article $articleId)
    {
        // $articles = Article::find($id);
        // return view('article.show',compact('articles'));
         $article = Article::find($articleId);
    return view('article.show',compact('article'));
    }

This is incorrect, the route model binding show(Article $articleId) will assign the single article to $articleId, you are then assigning $article to a collection, which is why you are getting the error in your view about the collection not containing the property title

Try this instead:

    public function show(Article $article)
    {
        // Route Model Binding will assign $article to be an instance of Article with the `id` that the route specifies. For example /articles/1 -> controller will look up the article with an id of 1 in the database and assign it to the variable $article. You then pass this through to your view.

        return view('article.show',compact('article'));
    }

edit: Changed $articleId to $article, so you don't have to modify your show.blade.php, which references $article

1 like
deepu07's avatar
Level 11

How can we call title and body of article based on articleId? //articlecontroller@show public function show(Article $articleId) { return view('article.show',compact('articleId')); }

deepu07's avatar
Level 11

What I'm trying to do is adding a comment on article. but by adding your code it is just displaying comment form but there is article content. How can we achieve that thing. My goal is I'm should able to comment on article based on article id. Does it make sense?

Mikeysauce's avatar

Yes but you should have a form within the blade view that the controller is calling for that.

So, you call show.blade.php which displays the single article, show the article, the title, and whatever else you want to show - but also include a form here for users to insert a comment on this particular article. You have access to the whole $article object, which includes the id of the article, so you can pass this through with your form, to reference the specific article that you wish to comment on.

deepu07's avatar
Level 11

Yes. You are right. For that I added form tag in show.blade.php with respective URL able to comment on particular article. When I'm trying to access http://localhost/Lara/public/article/1 [article that is associated with id one] I'm getting an error like this Property [title] does not exist on this collection instance. (View: C:\xampp7\htdocs\Lara\resources\views\article\show.blade.php)

Mikeysauce's avatar

Right because your code

$article = Article::find($articleId);

This is creating a collection, then your view is calling $article->title on a collection, thus the property title does not exist.

Mikeysauce's avatar

I quite literally explained in my post above, as did @Snapey, check my post above and modify your controller.

If you have skype or something I can show you on screen share, perhaps easier.

Snapey's avatar

why did you not just do what I suggested?

Next

Please or to participate in this conversation.