Reputation: 45
My application takes payment from users but sub accounts must not be allowed to see the payment screen.
I have a Route::group
which checks if the user is allowed to pay through Middleware. The handle
function look like this
if(!\Auth::user()->isTeacher)
{
\Auth::logout();
return redirect('/login')->withErrors([$error = 'Sorry there was a problem. Please notify your School']);
}
return $next($request);
and the isTeacher()
function
if($this->school_id) {
$teachers = $this->school->find($this->id)->teachers;
$isTeacher = false;
foreach ($teachers as $teacher) {
if ($teacher->id == \Auth::user()->id) {$teacher = true;}
}
return $isTeacher;
}
Finally the School relationship looks like the following
return $this->hasOne('App\School', 'id', 'school_id');
The error I keep receiving is
LogicException in Model.php line 2667: Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
In part of the error tree?? it shows this which is from the Middleware
at Model->__get('isTeacher') in MustBeTeacherToMakePayment.php line 19
which is the if statement on the first line above.
Is anyone able to tell me what I'm doing wrong? This is driving me mad
Upvotes: 4
Views: 9319
Reputation: 40909
Instead of calling the isTeacher() function you are accessing isTeacher attribute. Eloquent sees a method of that name and identifies it as a method that should return relation definition. And then you get the error because relation definition methods should return Relation object.
You have to replace
if(!\Auth::user()->isTeacher)
with
if(!\Auth::user()->isTeacher())
and the error will be gone.
Upvotes: 10