chipscros.blogg.se

Laravel model join
Laravel model join











($dialog->messages->count('id') > $dialog->messages->count('viewed_by_user')) ($dialog->messages->count('id') > $dialog->messages->count('viewed_by_client')) OPTION 3: Just use whatever methods are already available instead of writing more logic $dialog = Client::with('messages')->find($id) >join('messages as m', 'm.client_id', 'clients.id')Īnd then just call it like you would any scope Client::dialog()->. >selectRaw('count(m.id) > sum(m.viewed_by_user) as has_new_for_user') >selectRaw('count(m.id) > sum(m.viewed_by_client) as has_new_for_client') >selectRaw('max(m.created_at) as last_message')

laravel model join

>withCount('messages') // the default name will be messages_count Or locally for the query $dialog->setHidden() This can be done globally for the model protected $appends = In the same way, you can hide the attributes you don't want to show. However if you're converting $dialog to an array or json format, accessors will be lost unless you append them. Return $this->messages->count() > $this->messages->sum('viewed_by_user') Īnd then you can access all the properties dynamically $dialog = Client::withCount('messages')->find($id) Public function getHasNewForUserAttribute() Return $this->messages->count() > $this->messages->sum('viewed_by_client') Public function getHasNewForClientAttribute() Return $this->messages->max('created_at') use max() method from collection to get the results If(!$this->relationshipLoaded('messages')) Load relationship only if it hasn't been loaded yet Public function getLastMessageAttribute() Return $this->hasMany(App\Message::class) Since your query has no where clause, a scope is not really necessary but it could also be done with that.

laravel model join

I think you could get all the information you take from that query with accessors in your Client model. I experimented with making a model that would inherit from another model but the boot method didn't work as expected so I dropped it. That said, there's no rule against making more than 1 model per table. You can have a database table without an Eloquent model but not the other way around. So, how can I use JOINs and GROUP BY in Eloquent when I do not have a real table for model entities? Or may be some different solutions for my task? I had an idea to create a VIEW in database from my query and to use this view as a fake table in the model. But I want to use Eloquent relations later with all its benefits. INNER JOIN clients AS c ON m.client_id = c.id Keep in mind here is no table "dialogs", a dialog is a result of joining and grouping.ĬOUNT(m.id) > SUM(m.viewed_by_client) AS has_new_for_client,ĬOUNT(m.id) > SUM(m.viewed_by_user) AS has_new_for_user Now I want to create model called "Dialog".

laravel model join

Here is table "messages" with all messages. Сoncretely I want to make a messager in my application. I want to use sophisticated SELECT query with JOINs and GROUP BY in Laravel model.













Laravel model join