Sunday, March 5, 2017

Laravel use morpMany with where clause

Hi I am implement polymorph relation in laravel framework currently i have 2 models CreditLog and User

Creditlog has property sourceable , which is sourceable to User model

class CreditLog extends Model
{
...


    public function sourceable()
    {
        return $this->morphTo();
    }

...
}

And then in User i have relation like this

class User extends Authenticatable
{

    public function creditLogs()
    {
        return $this->morphMany('App\Models\CreditLog', 'sourceable');
    }

}

And then in some controller i need to get user credit log

$user = User::find($id);
$CreditLogs = $user->creditLogs;

Can i adding parameter in creditLogs method , i mean can laravel morphMany add the parameter like this

$CreditLogs = $user->creditLogs
                        ->where('created_at', '>=', $inputReq['start'])
                        ->where('created_at', '<=', $inputReq['end']);

Thank you for responses the question



via masadi zainul

Advertisement