3

I'm New to laravel and I'm trying to achieve something very basic stuff but still getting stuck.

I have two models namely Post.php and Like.php

I'm trying to fetch all the likes linked to a post using eloquent relationship but it is returning an empty array. Here is my code-

Post.php

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

Route.php

Route::get('/', function(){
  $posts = Post::all()->sortByDesc("post_id");
  return view('index')->with(['posts' => $posts]);
});

View.blade.php

@foreach($posts as $post)
  {{ $post->likes }}
@endforeach

What am I doing wrong here?

Update- likes table migration

public function up()
{
    Schema::create('likes', function (Blueprint $table) {
        $table->increments('like_id');
        $table->integer('post_id')->unsigned();
        $table->integer('user_id')->unsigned();
        $table->foreign('post_id')->references('post_id')->on('posts')->onDelete('cascade');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->timestamps();
    });
}

Post Migration

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->increments('post_id');
        $table->integer('user_id')->unsigned();
        $table->string('post_message');
        $table->timestamps();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

Post Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

  public function user(){
    return $this->belongsTo('App\User');
  }

  public function likes(){
    return $this->hasMany('App\Like');
  }
}
13
  • Please post the likes table migration. Commented Jul 12, 2018 at 21:47
  • I've added the likes table migration in the update section of the question Commented Jul 13, 2018 at 1:36
  • $table->increments('like_id'); change that to $table->increments('id'); Commented Jul 13, 2018 at 1:38
  • I've changed that to id, but it is still returning the same [] null results Commented Jul 13, 2018 at 1:43
  • What's the result of {{ $post->likes->count() }}? Commented Jul 13, 2018 at 1:45

1 Answer 1

4

Laravel expects the primary key to be id, but you are using the custom post_id.

Specify it in your model and adjust the relationship:

class Post extends Model {
    protected $primaryKey = 'post_id';

    public function likes() {
        return $this->hasMany('App\Like', 'post_id');
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

this returns an error- Column not found: 1054 Unknown column 'likes.post_post_id' in 'where clause'
Does Laravel pick up on a variable specifically called $primaryKey?
Yes, you can override the default: github.com/laravel/framework/blob/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.