eloquent

Eloquent select rows with empty string or null value

非 Y 不嫁゛ 提交于 2020-12-29 09:10:24
问题 I have something like $user->albums()->where('col', NULL) , it works fine then I tried to extend it to empty strings with $user->albums()->where('col', NULL)->or_where('col', '') and it's not working. Also I saw on this post that I could use where_null('col') but it's not working and it's not documented. Any simple method to select where empty or NULL col 回答1: Try using orWhereNull for the second clause: $users = DB::table('users') ->where('col', '=', '') ->orWhereNull('col') ->get(); 回答2:

Eloquent select rows with empty string or null value

纵然是瞬间 提交于 2020-12-29 09:06:47
问题 I have something like $user->albums()->where('col', NULL) , it works fine then I tried to extend it to empty strings with $user->albums()->where('col', NULL)->or_where('col', '') and it's not working. Also I saw on this post that I could use where_null('col') but it's not working and it's not documented. Any simple method to select where empty or NULL col 回答1: Try using orWhereNull for the second clause: $users = DB::table('users') ->where('col', '=', '') ->orWhereNull('col') ->get(); 回答2:

How to query distances between two coordinates with Eloquent

╄→尐↘猪︶ㄣ 提交于 2020-12-29 04:29:45
问题 I know this has been ask many times. But I didn't figure out to make it according to my needs. I need to query the nearest users from another user. Basically, I have a users table this table has a one to one relation with the users_locations table which has a latitude and a longitude field. So I've seen this https://laravel.io/forum/04-23-2014-convert-this-geolocation-query-to-query-builder-for?page=1 and this may be the best solution. But my basic query is : \App\Model\User::whereNotIn('id',

Laravel Group By Date

旧时模样 提交于 2020-12-26 04:31:00
问题 I am trying to make an elqouent query for a following sql, select sum(w.total_item_count) as Day_count, w.created_at as created_at from `sales_flat_orders` as `w` group by DATE(`w`.`created_at`) order by `w`.`created_at` asc and I have tried this $orderbydate = DB::table('sales_flat_orders as w') ->select(array(DB::Raw('sum(w.total_item_count) as Day_count'),DB::Raw('DATE(w.created_at) as created_at'))) ->groupBy('w.created_at') ->orderBy('w.created_at') ->get(); I am getting correct output

Laravel Group By Date

99封情书 提交于 2020-12-26 04:27:07
问题 I am trying to make an elqouent query for a following sql, select sum(w.total_item_count) as Day_count, w.created_at as created_at from `sales_flat_orders` as `w` group by DATE(`w`.`created_at`) order by `w`.`created_at` asc and I have tried this $orderbydate = DB::table('sales_flat_orders as w') ->select(array(DB::Raw('sum(w.total_item_count) as Day_count'),DB::Raw('DATE(w.created_at) as created_at'))) ->groupBy('w.created_at') ->orderBy('w.created_at') ->get(); I am getting correct output

Laravel limit eager loading for each model

拈花ヽ惹草 提交于 2020-12-26 04:26:21
问题 With our User model we are eager loading conversations and messages of these conversations. Of each conversation, we would like to lead the last 25 messages. So we wrote the following query. User::where('status', 3)->with(['conversations.messages' => function ($q) { $q->take(25)->orderBy('created_at', 'DESC'); }])->get(); However, this function only returns 25 messages in total (so not per conversation). Even if you have a 1000 users it will load the last 25 messages in the messages table but

Need to let users login with multiple credentials same as login with other account functionality in Gmail services- Laravel

二次信任 提交于 2020-12-25 01:32:45
问题 I want to let my users to login with different credentials in the same browser window, which is using the single users table. If tables were different then I will surely do that with guards , but the problem is I have to manage the user logins through single table. Please help me how to manage multiple sessions in the same browser window, as when I login with other account in a new tab the first one goes logout. Thanks in advance. 回答1: What I wanted to do was to maintain multiple session for

Laravel Eloquent limit results for relationship

感情迁移 提交于 2020-12-25 00:39:50
问题 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:32:40
问题 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:29:11
问题 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