Support the ongoing development of Laravel.io →
Requests Database Eloquent
Last updated 2 years ago.
0
$comments = comments::where('post_id', '=', $post_id)->first();

On a side note... Models are generally singular and start with an uppercase letter, ie Comment

$comments = Comment::where('post_id', '=', $post_id)->first();
Last updated 2 years ago.
0

pogachar said:

$comments = comments::where('post_id', '=', $post_id)->first();

On a side note... Models are generally singular and start with an uppercase letter, ie Comment

$comments = Comment::where('post_id', '=', $post_id)->first();

This returns a null value. Also:

<?php
class Comments extends Eloquent {   
   
	protected $table = 'comments';
}
Last updated 2 years ago.
0

DrPrez said:

Do you have relations between posts and comments models?

In that case

class Post extends Eloquent {   
    public function comments()
    {
        return $this->hasMany('Comment');
    }
}

class Comment extends Eloquent {   
    public function post()
    {
        return $this->belongsTo('Post');
    }
}

one to many relationship

$comments = Post::find($id)->comments;
Last updated 2 years ago.
0

pogachar said:

DrPrez said:

Do you have relations between posts and comments models?

In that case

class Post extends Eloquent {   
   public function comments()
   {
       return $this->hasMany('Comment');
   }
}

class Comment extends Eloquent {   
   public function post()
   {
       return $this->belongsTo('Post');
   }
}

one to many relationship

$comments = Post::find($id)->comments;

Thank you, could you explain the hasMany and belongsTo function to me, because right now I understand that they link together somehow, but if you could tell me how it works I would have a much better understanding. I did read the documentation on it, but it didn't really cover how it works.

Last updated 2 years ago.
0

Look for database design tutorials/books. They will explain much better than I could.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

MineSQL minesql Joined 18 Jun 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.