Reputation: 1587
I have a relation to get the friends created on specific date which is working with a static date parameter
public function friends()
{
return $this->hasMany(Friend::class)->where('created_at','2018-01-31');
}
But i need to get it for a dynamic date where the $request variable is unavailable inside a relation. I tried like this which is not working
public function friends($request)
{
return $this->hasMany(Friend::class)->where('created_at',$request->date);
}
How can i solve this issue?
Upvotes: 3
Views: 1844
Reputation: 419
You can do something like this in controller
public function data(){
Mdalname::friend($value)
}
And in modal
public function friends($value )
{
return $this->hasMany(Friend::class)->where('created_at', $value);
}
Upvotes: 0
Reputation: 242
Model will remain same as you posted:
public function friends($date)
{
return $this->hasMany(Friend::class)->where('created_at', $date);
}
In your controller(example):
$fooFriends = $foo->friends('2018-01-30')->get();
//Directly from $request or you can use different values/vars each time
$fooFriends = $foo->friends($request->date)->get();
Upvotes: 1
Reputation: 2588
You can use Laravel helper function request()
The request function returns the current request instance or obtains an input item
public function friends()
{
$date = request('date') ? : '2018-01-31'; // You can choose a default date, here '2018-01-31'
return $this->hasMany(Friend::class)->where('created_at', $date);
}
Upvotes: 0