lundi 16 janvier 2017

Laravel: Not able to access belongsToMany relation in nested whereHas

I am using whereHas in nested where clause but i am not able to do so. Following is my model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    public function scopePickedUser($query, $pickedTypeID)
    {
        return $query->where(function($query) use ($pickedTypeID)
        {
            $query->whereHas('photos', function($query) use ($pickedTypeID)
            {
                 $query->whereTypeId($pickedTypeID);
            });


            if($condition){
                $query->orWhere('another_column_id',$var);
            }
        });
    }

    public function photos()
    {
        return $this->belongsToMany(Photo::class);
    }
}

If i call, App\Models\User::pickedUser(4)->get() i am getting error BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::photos()'. Why is that? What am i doing wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire