I started learning 2 days ago. I have made a simple user login and blog posting system.
Here is my code that will fetch all the blog posts, but should also join the tables together
public function index()
{
$blog = new Blog;
$view = View::make('blogs.index');
$view->posts = $blog->get()->user;
return $view;
}
Is this right? Looking at documentation and searching, it seems to be right but I keep getting the following error:
Undefined property: Illuminate\Database\Eloquent\Collection::$user
My blog table is structed like this:
And user table like so
Blog Model
class Blog extends Eloquent {
public function user()
{
return $this->belongsTo('User', 'user_id', 'id');
}
}
User Model
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
public function blog()
{
return $this->hasMany('Blog', 'id', 'user_id');
}
}