eloquent

Laravel Eloquent join vs with

爷,独闯天下 提交于 2020-12-05 06:18:51
问题 I see that join is (by default inner join) and its returning all columns but it takes almost the same time as with keyword for just 1000 data. $user->join(‘profiles’, ‘users.id’, ‘=’, ‘profiles.user_id’) - generates the below query. select * from `users` inner join `profiles` on `users`.`id` = `profiles`.`user_id` where `first_name` LIKE '%a%'` User::with(‘profile’) - this eager loading outputs the below query select * from `users` where exists (select * from `profiles` where `users`.`id` =

Accessing Pivot table “3rd Model relationship” in Laravel Eloquent

倾然丶 夕夏残阳落幕 提交于 2020-12-03 14:59:49
问题 I have this Many-to-Many relation Laravel Eloquent relationship The Models are: class Email extends Model //actually represent the email account { protected $table = 'emails'; protected $fillable = [ 'user_id', 'name', ]; public function messages() { return $this->belongsToMany(Message::class)->withPivot('email_subtype_id'); } } class Message extends Model //actually represent the email message { protected $table = 'messages'; protected $fillable = [ 'subject', 'body ', ]; public function

laravel Eloquent model update event is not fired

时间秒杀一切 提交于 2020-12-02 06:52:38
问题 Merry Christmas guys! I am new to Laravel. Just had a beginner's question, when I am trying to use service provider and model event to log the update information. Was following the online doc: https://laravel.com/docs/5.3/eloquent#events After put all code together, I find that the model event only fire when create the use but never log anything when I edit the user. Did I miss anything? Feel like the $user didn't get assigned properly. Where is it from? from other service provider? Any

laravel Eloquent model update event is not fired

邮差的信 提交于 2020-12-02 06:49:08
问题 Merry Christmas guys! I am new to Laravel. Just had a beginner's question, when I am trying to use service provider and model event to log the update information. Was following the online doc: https://laravel.com/docs/5.3/eloquent#events After put all code together, I find that the model event only fire when create the use but never log anything when I edit the user. Did I miss anything? Feel like the $user didn't get assigned properly. Where is it from? from other service provider? Any

laravel Eloquent model update event is not fired

我与影子孤独终老i 提交于 2020-12-02 06:49:05
问题 Merry Christmas guys! I am new to Laravel. Just had a beginner's question, when I am trying to use service provider and model event to log the update information. Was following the online doc: https://laravel.com/docs/5.3/eloquent#events After put all code together, I find that the model event only fire when create the use but never log anything when I edit the user. Did I miss anything? Feel like the $user didn't get assigned properly. Where is it from? from other service provider? Any

laravel Eloquent model update event is not fired

岁酱吖の 提交于 2020-12-02 06:49:04
问题 Merry Christmas guys! I am new to Laravel. Just had a beginner's question, when I am trying to use service provider and model event to log the update information. Was following the online doc: https://laravel.com/docs/5.3/eloquent#events After put all code together, I find that the model event only fire when create the use but never log anything when I edit the user. Did I miss anything? Feel like the $user didn't get assigned properly. Where is it from? from other service provider? Any

Laravel join queries AS

允我心安 提交于 2020-11-30 06:17:04
问题 Any way of defining an AS for a query?? I have tried the following: $data = News::order_by('news.id', 'desc') ->join('categories', 'news.category_id', '=', 'categories.id') ->left_join('users', 'news.user_id', '=', 'users.id') // ['created_by'] ->left_join('users', 'news.modified_by', '=', 'users.id') // ['modified_by'] ->paginate(30, array('news.title', 'categories.name as categories', 'users.name as username')); The problem is that ['name'] from categories will be replaces with the one from

Laravel join queries AS

你离开我真会死。 提交于 2020-11-30 06:15:04
问题 Any way of defining an AS for a query?? I have tried the following: $data = News::order_by('news.id', 'desc') ->join('categories', 'news.category_id', '=', 'categories.id') ->left_join('users', 'news.user_id', '=', 'users.id') // ['created_by'] ->left_join('users', 'news.modified_by', '=', 'users.id') // ['modified_by'] ->paginate(30, array('news.title', 'categories.name as categories', 'users.name as username')); The problem is that ['name'] from categories will be replaces with the one from

How do I compare 2 eloquent collections and remove whats in one from the other?

白昼怎懂夜的黑 提交于 2020-11-30 00:40:09
问题 I have a collection of all doors, and a collection of doors that the current user has access to. How can I compare the two and remove from the all doors collection the doors the user already has access to? $doors = Door::orderBy('name', 'asc')->get(); $users_doors = $user->doors; Here are the two collections. 回答1: Use Collection::diff(): $doorsWithAccess = $doors->diff($users_doors); 来源: https://stackoverflow.com/questions/53458183/how-do-i-compare-2-eloquent-collections-and-remove-whats-in

Laravel Eloquent - Working with an array of JSON objects

|▌冷眼眸甩不掉的悲伤 提交于 2020-11-29 03:10:32
问题 I have an array of JSON objects within my database (as depicted by the image below) How do I search for a specific value within it? Say I want the record where the "ends_at" field is equal to "2018-11-24 08:00:00" I've tried using ->where('column->starts_at', '2018-11-24 08:00:00') or even tried ->whereJsonContains('column->starts_at', '2018-11-24 08:00:00'). Within the model I've cast the column to an array - am I missing something, or can this not be done? 回答1: Use this: ->whereJsonContains