laravel-query-builder

Many to Many to MorphToMany Relationships

自古美人都是妖i 提交于 2021-01-29 13:42:47
问题 I have an Exam , Question and Tag (Spatie/laravel-tags package) models. An Exam consists of many Questions (Many to Many), and Question has many Tags (MorphToMany). I would like to have a method on the Exam model to get all the tags of Exam through its associated questions, such that $exams→tags() returns all the tags from associated questions belonging to the exam. Can anyone point me towards what may be the best course to take in order to achieve this? 回答1: If you have proper described

use distinct with multiple columns in laravel

萝らか妹 提交于 2020-12-30 08:56:31
问题 my table structure is as below date seller unit price total 05-06-17 abc 14 700 9800 05-06-17 pqr 12 600 7200 05-06-17 abc 10 520 5200 06-06-17 abc 10 600 6000 06-06-17 pqr 15 520 7800 06-06-17 pqr 16 520 8320 I need to fetch record like 1) 'date' = '05-06-2017', 'seller' = 'abc', 'total' = '15000' 2) 'date' = '05-06-2017', 'seller' = 'pqr', 'total' = '7200' 3) 'date' = '06-06-2017', 'seller' = 'abc', 'total' = '6000' 4) 'date' = '06-06-2017', 'seller' = 'pqr', 'total' = '16120' I need data

use distinct with multiple columns in laravel

痴心易碎 提交于 2020-12-30 08:55:01
问题 my table structure is as below date seller unit price total 05-06-17 abc 14 700 9800 05-06-17 pqr 12 600 7200 05-06-17 abc 10 520 5200 06-06-17 abc 10 600 6000 06-06-17 pqr 15 520 7800 06-06-17 pqr 16 520 8320 I need to fetch record like 1) 'date' = '05-06-2017', 'seller' = 'abc', 'total' = '15000' 2) 'date' = '05-06-2017', 'seller' = 'pqr', 'total' = '7200' 3) 'date' = '06-06-2017', 'seller' = 'abc', 'total' = '6000' 4) 'date' = '06-06-2017', 'seller' = 'pqr', 'total' = '16120' I need data

Laravel Query builder with Join table as nested array

余生颓废 提交于 2020-12-15 06:40:10
问题 I have following code. the result i get is a single array. is there any way i can make it a nested array? $contents = RecentView::where('recent_views.user_id', $loggedUser) ->leftJoin('feed_posts','recent_views.post_id','=','feed_posts.id') ->leftJoin('users','feed_posts.user_id','=','users.id') ->paginate(12)->toArray(); and the result is: [id] => 71 [user_id] => 71 [post_id] => 529 [site_id] => 103 [date_time] => 2020-11-05 00:00:00 [title] => Lorem Ipsum [description] => Lorem Ipsum Lorem

Laravel Query builder with Join table as nested array

浪子不回头ぞ 提交于 2020-12-15 06:38:15
问题 I have following code. the result i get is a single array. is there any way i can make it a nested array? $contents = RecentView::where('recent_views.user_id', $loggedUser) ->leftJoin('feed_posts','recent_views.post_id','=','feed_posts.id') ->leftJoin('users','feed_posts.user_id','=','users.id') ->paginate(12)->toArray(); and the result is: [id] => 71 [user_id] => 71 [post_id] => 529 [site_id] => 103 [date_time] => 2020-11-05 00:00:00 [title] => Lorem Ipsum [description] => Lorem Ipsum Lorem

Laravel Many to Many Sync with additional column

喜你入骨 提交于 2020-08-07 15:58:35
问题 Laravel version 7.0 I have Team model and User model, team_has_users table. team_has_users table has team_id , user_id , role columns. One user can belong to one team with different roles. For instance, one user can belong to one team as a client and as an employee. in Team model, I set a relation like this. public function users(){ return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id') ->withPivot('role'); } When I attach users to the team, it worked well like this.

Laravel Many to Many Sync with additional column

南楼画角 提交于 2020-08-07 15:58:10
问题 Laravel version 7.0 I have Team model and User model, team_has_users table. team_has_users table has team_id , user_id , role columns. One user can belong to one team with different roles. For instance, one user can belong to one team as a client and as an employee. in Team model, I set a relation like this. public function users(){ return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id') ->withPivot('role'); } When I attach users to the team, it worked well like this.

Laravel Many to Many Sync with additional column

穿精又带淫゛_ 提交于 2020-08-07 15:51:26
问题 Laravel version 7.0 I have Team model and User model, team_has_users table. team_has_users table has team_id , user_id , role columns. One user can belong to one team with different roles. For instance, one user can belong to one team as a client and as an employee. in Team model, I set a relation like this. public function users(){ return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id') ->withPivot('role'); } When I attach users to the team, it worked well like this.

Laravel Many to Many Sync with additional column

核能气质少年 提交于 2020-08-07 15:51:05
问题 Laravel version 7.0 I have Team model and User model, team_has_users table. team_has_users table has team_id , user_id , role columns. One user can belong to one team with different roles. For instance, one user can belong to one team as a client and as an employee. in Team model, I set a relation like this. public function users(){ return $this->belongsToMany(User::class, 'team_has_user', 'team_id', 'user_id') ->withPivot('role'); } When I attach users to the team, it worked well like this.