How to use Model binding in larvel

Laravel

Posted on 21 May 15 by Vickey
  497



I have two model Blog and User. One user can write many blogs for bind both model we can use the below code

class Blog extends Eloquent {

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

}



class User extends Eloquent {

    public function blogs()
    {
        return $this->hasMany('Blog');
    }
}


$user = User::find(1);
$blogs = $user->blogs;


$blogs = Blog::find(1);
$user = $blogs->user;

If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com