Laravel: How to subselect two columns from another table in a query - php

Say I have the following tables:
event_relationships
id
user_id
relationship
primary_event_id
secondary_event_id
events
id
user_id
name
color
In my user model, I have my relationship setup like:
$this->hasMany(EventRelationships::class, 'user_id');
And each event relationship belongs to one user.
How can I use Laravel Eloquent to get a list of all the event relationships, but also get each event's name and color? Matching on the primary_event_id and secondary_event_id.
Is this possible with one query?

In pure eloquent, in the laravel way, that would be :
$eventRelationships = EventRelationships::with('primaryEvent', 'secondaryEvent')->get();
foreach($eventRelationships as $eventRelationship){
$primary_event_name = $eventRelationship->primaryEvent->name;
$secondary_event_name = $eventRelationship->secondaryEvent->name;
}
but it's in 2 queries. If you want only one query, you have to use a join :
$eventRelationships = EventRelationships::query()
->join('events as primary_events', 'events.id', 'event_relationships.primary_event_id')
->join('events as secondary_events', 'events.id', 'event_relationships.secondary_event_id')
->select('event_relationships.*', 'primary_events.name as primary_event_name', 'secondary_events.name as secondary_event_name')
->get();
foreach($eventRelationships as $eventRelationship){
$primary_event_name = $eventRelationship->primary_event_name;
$secondary_event_name = $eventRelationship->secondary_event_name;
}
It might be easier to use the first one as you manipulate eloquent object

Related

Laravel query builder add complex query result

I have three models with the following hierarchy :
User
id
....some other properties
Journey
id
user_id
budget
....some other properties
Confirmation
id
journey_id
user_id
....some other properties
I have a HasMany from User to Journey, a HasMany from Journey to Confirmation.
I want to get the sum for a column of the journeys table by going through the confirmations table but I cannot create an intermediate HasManyThrough relation between User and Journey by using Confirmation.
I have tried to do
public function journeysMade(): HasManyThrough
{
return $this->hasManyThrough(Journey::class, Confirmation::class);
}
// And after,
User::with(...)->withSum('journeysMade','budget')
But it was not possible because the relations are not adapted.
With hindsight, the sql query I want to translate would look like
select coalesce(sum(journeys.budget), 0) as income
from journeys
inner join confirmations c on journeys.id = c.journey_id
where c.user_id = ? and c.status = 'finalized';
How can I implement this query considering how I will use my query builder :
$driversQueryBuilder = User::with(['profile', 'addresses']); // Here
$pageSize = $request->input('pageSize', self::DEFAULT_PAGE_SIZE);
$pageNumber = $request->input('pageNumber', self::DEFAULT_PAGE_NUMBER);
$driversPaginator = (new UserFilterService($driversQueryBuilder))
->withStatus(Profile::STATUS_DRIVER)
->withCountry($request->input('country'))
->withSex($request->input('sex'))
->withActive($request->has('active') ? $request->boolean('active') : null)
->get()
->paginate(perPage: $pageSize, page: $pageNumber);
return response()->json(['data' => $driversPaginator]);
The reason why I want to get a builder is because UserFilterService expects a Illuminate\Database\Eloquent\Builder.
Do you have any idea about how I can solve this problem ?
Not 100% sure what exactly you want to sum, but I think you need the following query
$user->whereHas('journeys', function($query) {
$query->whereHas('confirmations', function($subQuery) {
$subQuery->sum('budget);
}
});
If you the above query isn't summing the budget you need, you just add another layer of abstraction with whereHas methods to get exactly what you need. Hope this helps!
EDIT:
$user->whereHas('confirmations', function($q) {
$q->withSum('journeys', 'budget')->journeys_sum_budget;
}

laravel model relation for nested models

I have three tables in a laravel project, namely:
-center
-payments
-payment_details
center table:
payment table:
payment_details table:
now i want to select all data from payment_details table,i got it it displayed well, but there is a search option for payment_type for the list,But the search filed is not in payment-details table , so i don't know how to retrieve that fro m another table
If you are modeling your data with Eloquent relationships Do somthing like:
$type = 'c'; // For example
PaymentDetail::whereHas('payment', function ($query) use ($type) {
$query->where('type', $type);
})->get();
If not you had to do join statement with payments table

getting all the data from pivot table in laravel

i'm trying to get all the lead_id inside my pivot table but i can't make it work.
controller:
$levels = Level::all();
$levels->lead()->attach('lead_id');
return $levels;
Model Level:
public function lead(){
return $this->belongsToMany(Lead::class, 'level_students')->withPivot('level_id', 'lead_id');
}
Model Lead:
public function level(){
return $this->belongsToMany(Level::class, 'level_students')->withPivot( 'lead_id', 'level_id');
}
If you mean all lead_id of a Level, then you can use pluck().
$level->lead->pluck('lead_id');
I'm not really sure what you are trying to achieve because it seems that you want to retrieve all lead_id associated with any Level. But if that is the case, then you can create a model for the pivot table (e.g. LevelLead) and use distinct() or with Query Builder:
$leadIds = DB::table('level_students')->select('lead_id')->distinct()->get();
If you want to get the referenced table's column (e.g. leads table's name column) then you can use JOIN. Check Laravel's doc for more options. For example, assuming the table name for Lead is leads, then:
$leads = DB::table('level_students')->join('leads', 'level_students.lead_id', '=', 'leads.lead_id')->select('level_students.lead_id', 'leads.name')->distinct()->get();

Including another set of results into many-to-many relationship

Let's say I have three tables:
Article: id, ...
Advert: id, display_on_every_subsite, ...
Article_Advert: advert_id, article_id
I have a simply Eloquent's relationship: belongsToMany between Article and Advert - Article_Advert is the pivot table.
The problem is that I need to fetch all Adverts for specified Article(s) AND all Adverts with display_on_every_subsite = 1.
I'm trying to achieve this using unions, this is what I've at this moment:
$this->belongsToMany('Advert', 'Article_Advert', 'article_id', 'advert_id')->union(Advert::allSubpages()->selectRaw('`advert`.*, `advert`.`id` as `pivot_advert_id`, null as `pivot_article_id`')->getQuery());
The problem is that when pivot_article_id is null, Eloquent does not attach fetched rows to any related model.
It's almost like that, just change the following:
// I assume you have this inside Article Model
$articleId = $this->id;
$this->belongsToMany('Advert', 'Article_Advert', 'article_id', 'advert_id')
->union(Advert::allSubpages()
->selectRaw("`advert`.*, `advert`.`id` as `pivot_advert_id`, '$articleId' as `pivot_article_id`")
->where('display_on_every_subsite','=','1')
->getQuery());

Select a table based on select result from another table

I have a table Registrationrequests where I have course_id and user_id and some other field.
$users_id = Registrationrequest::where('course_id', $query_course_user)->where('registered', 1)->get();
From the above query it gives me an array of result. But I need to take the details of these user_id from another table Users. I'm using Laravel. Table models are Registrationrequest and User
How can I get the user details from the above select result? I'm not that good in Joins. Any advice?
Use Eloquent's whereHas method:
$courseId = Request::get('course_id');
$users = User::whereHas('registrationRequests', function($query) use ($courseId)
{
$query->where('course_id', $courseId)->where('registered', 1);
});
This assumes you have set up the proper relationship in your User model. If not, add this method to your user model:
public function registrationRequests()
{
return $this->hasMany('Registrationrequest');
}

Categories