I'm trying to use Eloquent with a relationship and alwys have th same result:
Call to undefined method Illuminate\Database\Query\Builder::xxxx()
where xxxx could be "sons" defined in my model or save() if I use another code.
First of all I have this two models (simplified):
<?php
use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;
class User extends SentryUserModel {
public static $rules = array( 'first_name' => 'required',
'last_name' => 'required',
);
protected $hidden = array('password', 'remember_token');
public function sons()
{
return $this->hasMany('Son');
}
}
and
class Son extends \Eloquent {
protected $guarded =['id'];
public $timestamps = false;
public static $rules = array( 'first_name_son' => 'required',
);
public function user()
{
return $this->belongsTo('User');
}
}
and PhpStrorm tolds me tha Eloquent is an undefined class. Even so, I can use some methods.
My problem now. I try tu use this to save a "son" of a "user":
$user = Sentry::getUser();
$data = Input::all();
$rules_son = Son::$rules;
$son = new Son(array(
'first_name_son' => $data['first_name_son'],
));
$user->sons()->save($son);
here breaks with
Call to undefined method Illuminate\Database\Query\Builder::sons()
and if I use
$user->with('sons')->save($son);
Call to undefined method Illuminate\Database\Query\Builder::save()
and this works fine
Son::create(array(
'user_id' => $user->id,
'first_name_son' => $data['first_name_son'],
));
All this code is in my UsersController
What's the matter using mnethod "sons()", and why in this proyect Eloquent could be "not defined"? (it's defined becouse I can search or save using Eloquent ORM)
Thanks guys
While I was waiting for an answer ;), I still having problems with Eloquent methods. I cant use something as $user->books()->get() or ->attach() in a relation many to many users and books, also with User::with('books') I have the same problem
Call to undefined method Illuminate\Database\Query\Builder::xxxx()
My problem is becouse of Sentry. I get the User with:
$user = Sentry::getUser();
And, I don't know why, this $user is not a User model too, so some methods fail.
My solution, I don't know if it's the best one: - create a static function to call the user still using Sentry
//In User.php model
public static function eloquentUser(){
$sentry_user = Sentry::getUser();
return $user = User::findOrFail($sentry_user->id);
}
Then I call it with:
$user = User::eloquentUser();
Stiil using Sentry and works with Eloquent.