Laravel collection with pivot data - laravel

i am trying to query a collection of many to many relations with pivot data
this is my route:
Route::get('/test/{group}', function(Group $group){
return $group->applicants->pivot->alert_end_date;
})->middleware('auth');
Models:
public function applicants()
{
return $this->belongsToMany(User::class,'applicant_group_relationships', 'group', 'applicant')
->withPivot('alert_end_date');
}
public function applied_groups()
{
return $this->belongsToMany(Group::class, 'applicant_group_relationships', 'applicant', 'group')
->withPivot('alert_end_date');
}
getting an error "Property [pivot] does not exist on this collection instance."
*edit: i am trying to get the applicants with the alert_end_date from the intermediate table as a collection as a whole.
Any assistance is appreciated!

Just use return $group->applicants;. This already contains the pivot data.

Related

Laravel merge Relationship

I have 2 tables bookings and eventDates.
bookings table has 2 fields checkIndate and checkOutDate which points to eventDates table.
I setup following relationships
public function eventCheckInDate() {
return $this->belongsTo( 'EventDate', 'checkInDate' );
}
public function eventCheckOutDate() {
return $this->belongsTo( 'EventDate', 'checkOutDate' );
}
How can I combine those relationship so that eloquent only runs single query and get the data?
What happens now is it run 2 queries even if checkInDate and checkOutDate are still the same.
Thank you
As I understand you need something like that. create another method for merging two relations.
public function getEventsInOutDateAttribute($value)
{
$eventCheckInDate = $this->eventCheckInDate;
$eventCheckOutDate = $this->eventCheckOutDate;
// Merge collections and return single collection.
return $eventCheckInDate->merge($eventCheckOutDate);
}

Laravel Nova Metrics Partition belongsToMany relationship

I have the following data model:
(Publication) <-[belongsToMany]-> (Subscriber)
I want to create a Nova Partition Metric to display the number of Subscribers for each Publication.
The calculate method of my Partition class looks like this:
public function calculate(Request $request)
{
return $this->count($request, Subscriber::with('publications'), 'publication.id');
}
But I am getting an "unknown column" error. Anyone know how to make this work?
You could do something like this:
public function calculate(Request $request)
{
$subscribers = Subscriber::withCount('publications')->get();
return $this->result(
$subscribers->flatMap(function ($subscriber) {
return [
$subscriber->name => $subscriber->publications_count
];
})->toArray()
);
}
The count helper only allows to group by a column on model's table. It also don't allow to join tables.
If you want a more complex query, with a join and a group by column in another table, you can build your own array of results and return it with the results helper.
You can see the results helper docs here: https://nova.laravel.com/docs/1.0/metrics/defining-metrics.html#customizing-partition-colors
You should create your array (you can use eloquent or query builder here) inside the calculate function, then return that array with the results helper.
Hope this helps!
You can make the groupBy on publication_foreign_key in Subscriber table and edit the publication_foreign_key to publication_name using ->label() method
Like this
public function calculate(Request $request)
{
return $this->count($request, Subscriber::class, 'publication_id')
->label(function($publicationId)
{
switch($publicationId)
{
case publication_foreign_key_1 : return publication_name_1;
break;
case publication_foreign_key_2 : return publication_name_2;
break;
default: return 'Others';
}
});
}

Laravel 5.7 query with 3 tables

I'm pretty new to Laravel and i got stuck with building a more complex query:
I've 3 tables with their models:
Codes:
id
description
factor
public function purposes() {
return $this->hasMany('App\Purpose', 'code_purposes', 'code_id', 'purpose_id');
//I could be wrong here
}
Purpose:
id
name
description
Code_purposes:
code_id
purpose_id
public function codes() {
$this->belongsToMany('App\Code'); //I could be wrong here
}
public function purposes() {
$this->belongsToMany('App\Purpose'); //I could be wrong here
}
What I want is to fetch all the codes with the condition where the purposes name = 'some_name'
I thought this would be easy with the relationships, but I can't figure it out.
So how do I do this in Laravel?
In Code model:
public function purposes() {
return $this->belongsToMany('App\Purpose');
}
In Purpose model:
public function codes() {
return $this->belongsToMany('App\Code');
}
Now you can get data like:
$codes = Purpose::where('name', 'some_name')->first()->codes;
Relation table name must be code_purpose. And no need any model for this table.
Source: Laravel Many To Many Relationships

Nested With() in Laravel Model Collection Return

I have a collection that is returned as such:
$scheduleLoads = Load::with('shipment','driver','tractor');
Now, my question is related to the with issue - is there a way to add the relationships of these relationships into my returned collection?
For example:
In the Load model I have the following relationship to shipment:
public function shipment(){
return $this->belongsTo(shipment::class, 'shipmentID');
}
In the shipment model I have the following relationship:
public function shiptoAccount(){
return $this->belongsTo('App\Customer', 'ship_to');
}
Is there a way to include the shiptoAccount return of the shipment associated with the Loads collections?
Use the "dot" syntax (documentation):
$scheduleLoads = Load::with('shipment.shiptoAccount', 'driver', 'tractor');

latest() helper function in laravel

I'm building a small application where I'm having many to many relationship between two models something like this:
class Contact extends Model
{
public function company()
{
return $this
->belongsToMany('App\Company', 'company_contact', 'company_id', 'contact_id')->withTimestamps();
}
}
Now while retrieving this I want only the latest model through the pivot table or you may say relational table, for this I'm trying to implement:
public function getData()
{
$allData = Contact::all();
foreach($allData as $data)
{
$getCompany = $data->company()->latest()->first();
$data->company = $getCompany;
}
return response()->json(['model' => $allData], 200);
}
But I'm unable to retrieve the latest table it is showing the same old value or the first value.
Guide me how can I achieve this.
You can try this :
latest() is a function defined in Illuminate\Database\Query\Builder Class.
public function latest($column = 'created_at')
{
return $this->orderBy($column, 'desc');
}
So, It will just orderBy with the column you provide in descending order with the default column will be created_at
OR
public function getData()
{
$allData = Contact::all();
foreach($allData as $data)
{
$getCompany = $data->company()->orderBy('created_at', 'desc')->first();
$data->company = $getCompany;
}
return response()->json(['model' => $allData], 200);
}
So basically idea is if you are finding the relational data from many to many relation like $data->company() in this question and try to sort this with latest it will sort the company table with created_at sorting desc in order to get relational latest data you need to sort through pivot tables i.e.
$getCompany = $data->company()->withPivot('created_at')->orderBy('pivot_cr‌​eated_at', 'desc')->first();
This is how I achieved the latest relational table.
Note: You must have pivot table in your relation, in this answer created_at is the pivot field I'm using.

Resources