eloquent

Laravel Eloquent limit results for relationship

坚强是说给别人听的谎言 提交于 2020-12-25 00:24:33
问题 I have a simple set-up of Albums and Images, each album has many images. I can get all the data fine but I want to limit the returned number of images to 3. I have tried passing a closure like so: Album::with(['images' => function($query) { $query->take(3);}])->get(); This does limit the number of images to 3 but it limits the total count of images to 3 but I want to limit each album to 3 images. So the first album will show 3 images as expected but all the other albums have no images. I have

Laravel Eloquent limit results for relationship

社会主义新天地 提交于 2020-12-25 00:23:12
问题 I have a simple set-up of Albums and Images, each album has many images. I can get all the data fine but I want to limit the returned number of images to 3. I have tried passing a closure like so: Album::with(['images' => function($query) { $query->take(3);}])->get(); This does limit the number of images to 3 but it limits the total count of images to 3 but I want to limit each album to 3 images. So the first album will show 3 images as expected but all the other albums have no images. I have

Laravel Eloquent limit results for relationship

白昼怎懂夜的黑 提交于 2020-12-25 00:17:14
问题 I have a simple set-up of Albums and Images, each album has many images. I can get all the data fine but I want to limit the returned number of images to 3. I have tried passing a closure like so: Album::with(['images' => function($query) { $query->take(3);}])->get(); This does limit the number of images to 3 but it limits the total count of images to 3 but I want to limit each album to 3 images. So the first album will show 3 images as expected but all the other albums have no images. I have

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

Joining distinct tables in Laravel

邮差的信 提交于 2020-12-15 05:01:30
问题 I'm building a small search in my Laravel 7.0 application. I'm having two models Project and Company which is into many to many relationship with project_associate_company intermediate table. My tables are Project ******************** projects *********************** | | | id | name | area | cost | created_at | updated_at | | 1 | Development Project | 1461 | 243 |2018-09-17 21:42:41|2018-09-17 21:42:41| | 2 | Testing Project | 1500 | 200 |2018-09-18 21:42:41|2018-09-18 21:42:41| Company Model

How to get sum of array in foreach in laravel 6

故事扮演 提交于 2020-12-13 18:58:49
问题 from my controller sum is not working public function show($id) { $workers = DB::table('workers') ->select('wrk_id','f_name','m_name','l_name') ; $attendance = DB::table('attendance')->where('payroll_daily_id', $id) ->select('attendance.*','f_name','l_name','m_name',DB::raw('SUM(reg_hour)as tots')) ->rightjoinSub($workers,'worker', function($join){ $join->on('attendance.wrk_id','=','worker.wrk_id'); }) ->groupBy('payroll_attn_id') ->get(); foreach($attendance as $key){ $fetch[$key->wrk_id][

How to add additional column relationship in pivot table in Laravel

冷暖自知 提交于 2020-12-13 10:46:41
问题 Version: Laravel 5.4 I have 3 Models Model: Employee protected $fillable = ['name']; public function emails(){ return $this->belongsToMany('App\Email')->using('App\EmailEmployee'); } Model: Email protected $fillable = ['username']; public function employees(){ return $this->belongsToMany('App\Employee')->using('App\EmailEmployee'); } Every Employee has many email access and emails allocates to many employees. But I have another column in email_employee table email_id (emails table) employee

How to add additional column relationship in pivot table in Laravel

 ̄綄美尐妖づ 提交于 2020-12-13 10:46:38
问题 Version: Laravel 5.4 I have 3 Models Model: Employee protected $fillable = ['name']; public function emails(){ return $this->belongsToMany('App\Email')->using('App\EmailEmployee'); } Model: Email protected $fillable = ['username']; public function employees(){ return $this->belongsToMany('App\Employee')->using('App\EmailEmployee'); } Every Employee has many email access and emails allocates to many employees. But I have another column in email_employee table email_id (emails table) employee

Laravel / Eloquent whereIn with null

北慕城南 提交于 2020-12-05 07:13:21
问题 How do I apply Laravel's Eloquent whereIn() so that it includes null? I've tried: User::whereIn("column", [null, 1, 2]) -> get(); And User::whereIn("column", [DB::raw("null"), 1, 2]) -> get(); But both queries return no results. Thanks! 回答1: I don't think you can pass null in in mysql statement. If you run the query in mysql console it will skip the null. Try User::whereNull('column')->orWhereIn('column', array(1, 2))->get(); 回答2: Fetching data with either null and value on where conditions