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);
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire