I'm having problems accessing the Pivot relationship.
#relations: array:1 [
"orders_material" => Collection {#250
#items: []
}
]
I want to get the orders_material that have pivot values null
This is the query :
$material = Material::with(['orders_material' => function($query) use ($begin_date, $end_date, $begin_hour, $hour_final)
{
$query->whereBetween('Orders_has_Material.date_begin',[$begin_date, $end_date])
->whereBetween('Orders_has_Material.date_final',[$begin_date, $end_date])
->where('Orders_has_Material.Material_id', null)
->where('Orders_has_Material.Orders_id', null);
}])
->get();
dd($material);
public function orders_material()
{
return $this->belongsToMany('App\Orders', 'Orders_has_Material', 'Material_id', 'Orders_id');
}
First of all, as I see here u have many columns in ur pivot table like date_begin
and date_final
so here u need to edit ur relationship models like this :
public function orders_material()
{
return $this->belongsToMany('App\Orders', 'Orders_has_Material', 'Material_id', 'Orders_id')->withPivot('date_begin', 'date_final', 'column3');
}
and to access to them just u need to get $material
of orders
or the inverse.
which means, as u get the $material
in ur query, i think to want to access to the orders related to that $material
and show up some information inside ur pivot table. u can access to them like this (if ur relationship models are correct of course):
foreach($material->orders as $order)
{
echo $order->pivot->date_begin;
echo $order->pivot->date_final;
}
for more information read the doc many to many laravel with pivot table