Reputation: 218
I facing problem in fetching specific data from pivot table (manytomany relation). The scenario is that I want to fetch data from pivot table between two specific dates. When I use this code
$user_availability = $user->dates->where('date','>=' , $start_date)->where('date','<=' , $end_date)->get();
foreach ($user_availability as $date)
{
echo $date->pivot->afternoon;
}
It gives me following error
Type error: Too few arguments to function Illuminate\Support\Collection::get(), 0 passed in C:\xampp\htdocs\codehacking\app\Http\Controllers\UsersController.php on line 210 and at least 1 expected
User Model:
public function dates()
{
return $this->belongsToMany('App\Date')->withPivot('morning', 'afternoon','night','comment');
}
Pivot table
I can provide more information if you need. Any help would be appreciated.
Thanks!
Upvotes: 1
Views: 914
Reputation: 2070
From Laravel documentation.
Querying Relations
Since all types of Eloquent relationships are defined via methods, you may call those methods to obtain an instance of the relationship without actually executing the relationship queries.
Relationship Methods Vs. Dynamic Properties
If you do not need to add additional constraints to an Eloquent relationship query, you may access the relationship as if it were a property.
Change the query to
$user_availability = $user->dates()->where('date','>=' , $start_date)->where('date','<=' , $end_date)->get();
parenthesis
are used in the relation dates
to querying the relation.
Upvotes: 1