Chriz74
Chriz74

Reputation: 1480

eager load comments with posts in laravel, how to setup the DB and Models?

So, I checked a tutorial for a simple blog that had function to post comments on a post. I had my post migration already and I wanted to add comments to post. The posts have a picture of an item and a description. So my migration is:

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts' ,function(Blueprint $table){

            $table->increments('id')->unsigned()->unique();
            $table->unsignedInteger('user_id')->unsigned();
            $table->unsignedInteger('item_id')->unsigned();
            $table->string('picture', 255)->nullable();
            $table->text('description')->nullable();

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');


            $table->foreign('item_id')
                ->references('id')
                ->on('items')
                ->onDelete('cascade');


            $table->timestamps();
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('posts');
    }
}

so I have a table with the post id, the user_id of the poster, the item_id of the item whose picture is being posted, the picture and a description.

Now I thought, ok I have to add comments to these posts so I need a comments table and I made one like this:

class CreateCommentsTable extends Migration {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function(Blueprint $table)
        {
            $table->increments('id');
            $table -> integer('post_id')->unsigned()->default(0);
            $table->foreign('post_id')
                ->references('id')->on('posts')
                ->onDelete('cascade');
            $table -> integer('user_id')->unsigned()->default(0);
            $table->foreign('user_id')
                ->references('id')->on('users')
                ->onDelete('cascade');
            $table->text('body');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        // drop comment
        Schema::drop('comments');
    }
}

where I have the id of the comments, the post_id of the post the comment refers to and, the user_id of the commenter and the comment body.

This should be a one to many relationship problem right? A post can have many comments.

So the models are:

class Comment extends Model
{

    /**
     *
     * The comment belongs to one author
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function author()
    {
        return $this->belongsTo('App\User');
    }

    /**
     *
     * The comment belongs to one post
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function post()
    {
        return $this->belongsTo('App\Post');
    }

}

...

class Post extends Model
{

    /**
     *
     * a post has many comments
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function comments()
    {
        return $this->hasMany('App\Comments');
    }

    /**
     *
     * a post belongs to one author
     *
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function author()
    {
        return $this->belongsTo('App\User');
    }

}

...

class User extends Model
{

  //..... (some more stuff here not related to comments.

    public function comments()
    {
        return $this->hasMany('App\Comment');
    }
}

Now the point is, if I want to return all users posts WITH comments and comments author, how should I modify the models?

Upvotes: 1

Views: 1356

Answers (2)

Achraf Khouadja
Achraf Khouadja

Reputation: 6279

try this

$user = Auth::user(); $posts = Post::with('comments', 'author')->where('user_id', $user->id )->get();

you are using the user object not the user id in the 'where'

Upvotes: 0

William R.
William R.

Reputation: 33

you could do something like

$posts_with_comments = Post::with('author', 'comments')->get();

you can add as many relations as you want to load with it, check out the eager docs

https://laravel.com/docs/4.2/eloquent#eager-loading

Upvotes: 1

Related Questions