Tutorial by Examples

Eloquent also lets you query on defined relationships, as show below: User::whereHas('articles', function (Builder $query) { $query->where('published', '!=', true); })->get(); This requires that your relationship method name is articles in this case. The argument passed into the clos...
Suppose you have a Post model with a hasMany relationship with Comment. You may insert a Comment object related to a post by doing the following: $post = Post::find(1); $commentToAdd = new Comment(['message' => 'This is a comment.']); $post->comments()->save($commentToAdd); You ca...
Eloquent relationships are defined as functions on your Eloquent model classes. Since, like Eloquent models themselves, relationships also serve as powerful query builders, defining relationships as functions provides powerful method chaining and querying capabilities. For example, we may chain addi...
One to Many Lets say that each Post may have one or many comments and each comment belongs to just a single Post. so the comments table will be having post_id. In this case the relationships will be as follows. Post Model public function comments() { return $this->belongsTo(Post::class...
Lets say there is roles and permissions. Each role may belongs to many permissions and each permission may belongs to many role. so there will be 3 tables. two models and one pivot table. a roles, users and permission_role table. Role Model public function permissions() { return $this->bel...

Page 1 of 1