jeudi 27 avril 2017

How to show user's name based on user_id of a blog post on laravel 5.4?

I am trying to show the user name based on the user_id column of the post on the view.

But I keep getting this error: Call to undefined method Illuminate\Database\Query\Builder::belonsTo()

Here is my Post model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
  public function user()
    {
      return $this->belonsTo('App\User');
    }
}

My User model:

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

My Pages Controller based on this:

use App\User;
use App\Post;
    class PagesController extends Controller {
        public function getIndex() {
            $post = Post::find(1);
            echo $post->user->name;
        return view('pages.welcome')->withPost($post);
    }



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2oLXFcz
via IFTTT

Aucun commentaire:

Enregistrer un commentaire