@@ -715,6 +715,109 @@ <h6>Soft Delete <a href="http://laravel.com/docs/5.0/eloquent#soft-deleting" tit
715
715
Model::onlyTrashed()-> where('cars', 2)-> get();
716
716
</ pre >
717
717
718
+ < h6 > Relationships</ h6 >
719
+ < pre class ="prettyprint lang-php ">
720
+ // One To One - User::phone()
721
+ return $this-> hasOne('App\Phone', 'foreign_key', 'local_key');
722
+ // One To One - Phone::user(), The Inverse Of The Relation
723
+ return $this-> belongsTo('App\User', 'foreign_key', 'other_key');
724
+
725
+ // One To Many - Post::comments()
726
+ return $this-> hasMany('App\Comment', 'foreign_key', 'local_key');
727
+ // One To Many - Comment::post()
728
+ return $this-> belongsTo('App\Post', 'foreign_key', 'other_key');
729
+
730
+ // Many To Many - User::roles();
731
+ return $this-> belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');
732
+ // Many To Many - Role::users();
733
+ return $this-> belongsToMany('App\User');
734
+ // Many To Many - Retrieving Intermediate Table Columns
735
+ $role-> pivot-> created_at;
736
+ // Many To Many - Pivot table with extra attributes
737
+ return $this-> belongsToMany('App\Role')-> withPivot('column1', 'column2');
738
+ // Many To Many - Automatically maintained created_at and updated_at timestamps
739
+ return $this-> belongsToMany('App\Role')-> withTimestamps();
740
+
741
+ // Has Many Through - Country::posts(), A Country model have
742
+ // many Post models through an intermediate User model (User::country_id)
743
+ return $this-> hasManyThrough('App\Post', 'App\User', 'country_id', 'user_id');
744
+
745
+ // Polymorphic Relations - Photo::imageable()
746
+ return $this-> morphTo();
747
+ // Polymorphic Relations - Staff::photos()
748
+ return $this-> morphMany('App\Photo', 'imageable');
749
+ // Polymorphic Relations - Product::photos()
750
+ return $this-> morphMany('App\Photo', 'imageable');
751
+ // Polymorphic Relations - Register the morphMap in your AppServiceProvider
752
+ Relation::morphMap([
753
+ 'Post' => App\Post::class,
754
+ 'Comment' => App\Comment::class,
755
+ ]);
756
+
757
+ // Many To Many Polymorphic Relations - Tables: posts,videos,tags,taggables
758
+ // Post::tags()
759
+ return $this-> morphToMany('App\Tag', 'taggable');
760
+ // Video::tags()
761
+ return $this-> morphToMany('App\Tag', 'taggable');
762
+ // Tag::posts()
763
+ return $this-> morphedByMany('App\Post', 'taggable');
764
+ // Tag::videos()
765
+ return $this-> morphedByMany('App\Video', 'taggable');
766
+
767
+ // Querying Relations
768
+ $user-> posts()-> where('active', 1)-> get();
769
+ // Retrieve all posts that have at least one comment...
770
+ $posts = App\Post::has('comments')-> get();
771
+ // Retrieve all posts that have three or more comments...
772
+ $posts = Post::has('comments', '> =', 3)-> get();
773
+ // Retrieve all posts that have at least one comment with votes...
774
+ $posts = Post::has('comments.votes')-> get();
775
+ // Retrieve all posts with at least one comment containing words like foo%
776
+ $posts = Post::whereHas('comments', function ($query) {
777
+ $query-> where('content', 'like', 'foo%');
778
+ })-> get();
779
+
780
+ // Eager Loading
781
+ $books = App\Book::with('author')-> get();
782
+ $books = App\Book::with('author', 'publisher')-> get();
783
+ $books = App\Book::with('author.contacts')-> get();
784
+
785
+ // Lazy Eager Loading
786
+ $books-> load('author', 'publisher');
787
+
788
+ // Inserting Related Models
789
+ $comment = new App\Comment(['message' => 'A new comment.']);
790
+ $post-> comments()-> save($comment);
791
+ // save multiple related models
792
+ $post-> comments()-> saveMany([
793
+ new App\Comment(['message' => 'A new comment.']),
794
+ new App\Comment(['message' => 'Another comment.']),
795
+ ]);
796
+ $post-> comments()-> create(['message' => 'A new comment.']);
797
+
798
+ // Updating a belongsTo relationship
799
+ $user-> account()-> associate($account);
800
+ $user-> save();
801
+ $user-> account()-> dissociate();
802
+ $user-> save();
803
+
804
+ // Inserting Related Models - Many To Many Relationships
805
+ $user-> roles()-> attach($roleId);
806
+ $user-> roles()-> attach($roleId, ['expires' => $expires]);
807
+ // Detach a single role from the user...
808
+ $user-> roles()-> detach($roleId);
809
+ // Detach all roles from the user...
810
+ $user-> roles()-> detach();
811
+ $user-> roles()-> detach([1, 2, 3]);
812
+ $user-> roles()-> attach([1 => ['expires' => $expires], 2, 3]);
813
+
814
+ // Any IDs that are not in the given array will be removed from the intermediate table.
815
+ $user-> roles()-> sync([1, 2, 3]);
816
+ // You may also pass additional intermediate table values with the IDs:
817
+ $user-> roles()-> sync([1 => ['expires' => true], 2, 3]);
818
+
819
+ </ pre >
820
+
718
821
< h6 > Events</ h6 >
719
822
< pre class ="prettyprint lang-php "> Model::creating(function($model){});
720
823
Model::created(function($model){});
0 commit comments