jeudi 12 juillet 2018

Eloquent Relationship returning empty array

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)
  
@endforeach

What am I doing wrong here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire