I thought for sure I figured it out but I still get an error.
I changed user to this...
public function scopeUser()
{
return $this->belongsTo('User');
}
And tried this in the controller...
$cars = Cars::User()->BySortOrder()->get();
The error is:
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::BySortOrder()
Thanks!
When you call Car::user(), you're now returning an instance of a User object, no longer working with the QueryBuilder for your Car model. You'll be able to implement this a few ways, as long as you define the relationship to the cars model in your user class:
$user = /* retrieve the user from somewhere */;
$cars = $user->cars()->bySortOrder()->get();
$userId = /* retrieve the user id from somewhere */;
$cars = $cars->whereHas('user', function($query) use($userId) {
$query->where('id', $userId);
})->bySortOrder->get();
A third way would be to implement this as a query scope in your Cars model:
function scopeForUser($query, $id)
{
$query->whereHas('user', function($query) use($id) {
$query->where('id', $id);
});
}
And in your controller:
$userId = /* retrieve the user id from somewhere */
Cars::forUser($userId)->bySortOrder()->get();
I like your first example as I don't want to put a lot of user logic in the Cars class.
So in my controller I put this:
$user = Sentry::getUser();
$cars = $user->cars()->BySortOrder()->get();
And in my user class I have the relationship:
public function Cars()
{
return $this->hasMany('Car');
}
I get the error:
BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::cars()
I also tried double colon after $user
$cars = $user::cars()->BySortOrder()->get();
And tried adding scope in my relationship function
public function scopeCars()
{
return $this->hasMany('Car');
}
But no combination works.
Have you tried changing the function name in your user model to lowercase? IE "cars()" and not "Cars()"?
Sentry has its own User model. Does your User model that, and if so, have you ensured that your Sentry config file points to the right model?
Ah, no, I don't model that. I bet that's it.
I think it's this here
\vendor\cartalyst\sentry\src\Cartalyst\Sentry\Users\Eloquent\User.php
So in Laravel how do I extend or inherit this?
It should be a simple as changing your user class to extend from Cartalyst\Sentry\Users\Eloquent\User instead of Eloquent.
Actually, it was anything but simple. It's taken me hours to get this right.
For the User model I had to remove everything except this:
use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel;
class User extends SentryUserModel {
protected $table = 'users';
public function cars()
{
return $this->hasMany('Car');
}
}
And then I had to call it from my controller like this:
$user = User::find(Sentry::getUser()->id);
$cars = $user->cars()->bySortOrder()->get();
But, it's finally done.
Thank you both very much for sticking in there with me. I really appreciate it.
Mark
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community