I have a problem with using the query builders that give me the undefined method error for using post() in the routes file.
Usually I use the return of
User::find($id)->post;
but when I call post as a function, it doesn't work and gives me:
Call to undefined method Illuminate\Database\Query\Builder::post()
User Model
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected function post()
{
return $this->hasOne('App\Post');
}
}
Routes
Route::get('/', function () {
return view('welcome');
});
Route::get('/user/{id}/post',function($id){
return User::find($id)->post()->get();
});