Feb 19, 2021
0
Level 1
filter max date on intermediate pivot table
Hi i cannot do this.. I have a relationship many to many as defined
Invoice model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Invoice extends Model
{
public $primaryKey = 'id';
protected $fillable = [
'user_id',
'date_ins',
'tas',
];
public function user()
{
return $this->hasOne(User::class,'id','user_id');
}
/**
* The roles that belong to the invoice.
*/
public function shifts()
{
return $this->belongsToMany(Shift::class, 'invoice_shift')
->withPivot(['invoice_id','shift_id', 'shift_taken_id', 'shift_swapped_date','shift_taken_date', 'tas','msg','status_tas']);
}
}
Then Shift model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Shift extends Model
{
public $primaryKey = 'id';
protected $fillable = [
'code',
'description',
];
/**
* The users that belong to the role.
*/
public function invoices()
{
return $this->belongsToMany(Invoice::class, 'invoice_shift');
}
}
in pivot table i have also a column called shift_swapped_date, I wanted just to retrieve the row that has max value of this date. I tried in the controller
$invoices = Invoice::with('shifts')
->get();
$invoices->max('pivot.shift_swapped_date');
but doesn't work always gives to me all rows...with all dates Solutions? Thx
Please or to participate in this conversation.