mardi 16 août 2016

Laravel 5.1 - Cannot access relationship via ORM

I have two model classes as defined as below:

class Event extends Model
{
        use SoftDeletes;

    protected $dates = ['deleted_at'];


public function user()
{
    return $this->belongsTo('App\User');
}
public function transfers()
{
    return $this->hasMany('App\Transfer');
    }

}




class Transfers extends Model

{
    use SoftDeletes;
/**
 * The attributes that should be mutated to dates.
 *
 * @var array
 */
protected $dates = ['deleted_at'];


public function event()
{
    return $this->belongsTo('App\Event');
    }
}

I am trying access Transfers from an Event object using following code.

 public function filter()
    {
         $id = request()->input('id');
          $event = \App\Event::where([['id', '=', $id],
                                                 ['deleted_at', '=', NULL],
                                                ['user_id', '=', auth()->user()->id]])->first();

         echo $event->transfers->count();
}

However, it resulted in an exception as follows

Fatal error: Class 'App\Transfer' not found in D:\work\HC\hcserver\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 876

Please advice on how to get rid of this exception

Thanks in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire