eloquent

How to index count from json in Laravel

女生的网名这么多〃 提交于 2020-03-25 12:31:32
问题 I have a messages table with Structure as follows: id sender_id receiver_id pet_id description the description is the text message. I am fetching messages of users related to a specific pet by fetching all messages where the sender is user or receiver is user and then filtering them by petId I want. function messagesByPet($petId,$userId){ $messages = Message::where('sender_id',$userId)->orWhere('receiver_id',$userId)->orderBy('created_at','asc')->get(); $message = $messages->where('pet_id',

Laravel: how to “disable” a global scope in order to include “inactive” objects into query?

帅比萌擦擦* 提交于 2020-03-21 19:03:33
问题 I am having trouble with global scopes, especially the removal of the scope. In my User model, i have a ActivatedUsersTrait, that introduces a global scope to only query for Users with the column "activated" set to true (The User is "activated" after email verification). So far everything works fine, when i query for User::all(), i only get Users with activated=true. My problem now is, how to include the non-activated Users into my query, like SoftDeletingTrait does via withTrashed()? This is

Laravel Eloquent model id as string return wrong value in

折月煮酒 提交于 2020-03-18 04:52:08
问题 There is OauthClient model which $client->id does not return correct value in Laravel 5.2. "lucadegasperi/oauth2-server-laravel": "^5.1" is used that has in migration $table->string('id', 40)->primary(); However when use $client->id in blade template then I get back "0" . Using dd($client) show correct value in id attribute. What could be wrong? Model output from dd() OauthClient {#254 ▼ #connection: null #table: null #primaryKey: "id" #keyType: "int" #perPage: 15 +incrementing: true

Get Laravel Models with All Attributes

你。 提交于 2020-03-17 10:08:34
问题 Is there a way to retrieve a model in Laravel with all the attributes, even if they're null? It seems to only return a model with the attributes that aren't null. The reason for this is that I have a function that will update the model attributes from an array, if the attributes exists in the model. I use the property_exists() function to check the model if it has a particular attribute before setting it. The array key and model attribute are expected to match, so that's how it works. It

Get Laravel Models with All Attributes

落爺英雄遲暮 提交于 2020-03-17 10:07:53
问题 Is there a way to retrieve a model in Laravel with all the attributes, even if they're null? It seems to only return a model with the attributes that aren't null. The reason for this is that I have a function that will update the model attributes from an array, if the attributes exists in the model. I use the property_exists() function to check the model if it has a particular attribute before setting it. The array key and model attribute are expected to match, so that's how it works. It

Call to a member function connection() on a non-object error on Laravel 5

狂风中的少年 提交于 2020-03-17 07:23:18
问题 I want to store user configuration data on database and I'm following this forum thread about it http://forumsarchive.laravel.io/viewtopic.php?id=10406 but when I implemented it laravel throws an error. I ran composer dump-autoload but nothing seems to be workign.What's the problem here? // filename: app/config/settings.php use \App\Models\Setting $list = array(); $format = function(&$list, $keys, $val) use(&$format) { $keys ? $format($list[array_shift($keys)], $keys, $val) : $list = $val; };

Laravel authentication with new model & guard fails: Undefined index: model

送分小仙女□ 提交于 2020-03-15 06:10:08
问题 I'm trying to authenticate my Laravel application (5.8) with an additional model & guard. The problem, I receive a "Undefined index: model" error during the following login approach. Any ideas what i'm doing wrong? I've used this integration in an 5.7 Version of Laravel and it worked there without any problems. auth()->guard('partner')->login($partner); CodeSnippets: Partner Model (additional Settings) class Partner extends Authenticatable { protected $guard = 'partner'; public function

Relationship not being passed to notification?

♀尐吖头ヾ 提交于 2020-03-06 04:38:26
问题 I created a notification that I am passing a model to: class NewMessage extends Notification implements ShouldQueue { use Queueable; protected $message; public function __construct(Message $message) { $this->message = $message; } public function via() { return ['database']; } public function toArray() { Log::info($this->message); return [ 'message_id' => $this->message->id, ]; } } And this is how I call the notification: $message = Message::where('user_id', Auth::user()->id) ->where('message

Return first element from relation with `Lazy Eager Loading` [Laravel 5.2]

假装没事ソ 提交于 2020-03-06 03:08:08
问题 I have relation like this: public function message() { return $this->hasMany('Engine\Message'); } inside my Conversation model. and for each conversation I need to get last message. Here is what I tried, but this will get only one message for first conversation but will not get message from other conversations... $con = Conversation::all(); $con->load(['message' => function ($q) use ( &$mess ) { $mess = $q->first(); }]); return $con; I don't wana query for each record... Anyone know how to

Laravel eloquent insert data with multiple relationship

那年仲夏 提交于 2020-03-05 04:14:09
问题 I have 3 tables: User - id - email UserAccount - id - user_id - account_id Account - id - user_id Verification - id - user_id - guid I am trying to achieve a post whenever I try to add a user , it will automatically add an account with empty fields but with user_id in it, Verification table with user_id also, at the same time once the Account has been created it should also record UserAccount user_id and account_id but I ended up this error using many to many relationship belongsToMany and