Kee Nops
Kee Nops

Reputation: 43

LARAVEL Eloquent: link post to user then user to profile

I am aiming on retrieving the user image (from profile table column 'profile_img') and display on each post's footer.

I can retrieve the name of the author using $post->author->name and the profile picture using $post->author->profile->profile_image but it only works when i have a single record (post). when there is more than one record i get an error Trying to get property 'profile' of non-object (View: xampp/........../home.blade.php)

Can somebody show me where do i go wrong??

Models:

User

public function post() 
{
  return $this->hasMany('App\Post');  
}
public function profile()
{
  return $this->hasOne('App\Profile');
}

Profile

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

Post

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

controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class MainController extends Controller
{
  public function index() {
  $posts = Post::paginate(9);

return view('pages.home')->with('posts', $posts);
}

home view

@if(count($posts) > 0)
@foreach($posts as$posts)
<div>
  <div class="post-title"><h3>{{$post->title}}</h3></div>
  <div class="post-description">
    {!!mb_substr($post>body,10,rand(35,40)) !!} ....
  </div>
  <div class="featured-details">
    <div class="p-clearfix">
       <img class="authorimg"src="/storage/profile_images/{{ $post->author->profile->profile_image }}">
        <div class="author-title lite">{{ $post->author->name }}</div>
      <div class="lite thumbnail-date">{{ date('M j, Y', strtotime($post->created_at)) }}</div>
    </div>
  </div>
</div>
<hr>
@endforeach

@else
  no post yet
@endif

Upvotes: 0

Views: 1103

Answers (2)

Sh&#236;pu Ahamed
Sh&#236;pu Ahamed

Reputation: 480

In your controller index method try with :

public function index() {
  $posts = Post::with("author.profile")->paginate(9);

  return view('pages.home')->with('posts', $posts);
}

And update post model relationship:

public function author()
{
    return $this->belongsTo('App\User', 'user_id'); // update to users table foreign key
}

Upvotes: 0

Lamin Barrow
Lamin Barrow

Reputation: 889

There appears to be some issues in your home view. Try it again with this code and see.

Home View

@if(count($posts) > 0)
@foreach($posts as $post)
<div>
  <div class="post-title"><h3>{{$post->title}}</h3></div>
  <div class="post-description">
    {!!mb_substr($post>body,10,rand(35,40)) !!} ....
  </div>
  <div class="featured-details">
    <div class="p-clearfix">
       <img class="authorimg"src="/storage/profile_images/{{ $post->author->profile->profile_image }}">
        <div class="author-title lite">{{ $post->author->name }}</div>
      <div class="lite thumbnail-date">{{ date('M j, Y', strtotime($post->created_at)) }}</div>
    </div>
  </div>
</div>
<hr>
@endforeach

@else
  no post yet
@endif

You can even go further and avoid the n+1 problem for authors by running your eloquent model query like this om your controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;

class MainController extends Controller
{
  public function index() {
  $posts = Post::with("author")->paginate(9);

return view('pages.home')->with('posts', $posts);
}

I hope this helps.

Upvotes: 0

Related Questions