问题
I want to access the Auth Class within my ValidatorService Class.
namespace Services\Validators\User;
use \Services\Validators\Validator;
use \Illuminate\Support\Facades\Auth;
class Edit extends Validator {
public static $rules = [
'email' => 'required|unique:users,email,'.Auth::user()->id
];
}
I tried to use the \Illuminate\Support\Facades\Auth
Namespace, but laravel throws an Exception.
Error: syntax error, unexpected '.', expecting ']'

Laravel only throws the exception, when I try to use Auth::user()->id. If I remove Auth::user()->id and add a number, for example 1, it works.
I also tried to implement the Illuminate\Auth\UserInterface
but it is not working.
How can I use the Auth Class in my ValidatorService Class?
EDIT: Problem solved -> Scroll down.
回答1:
Solution:
You cannot use functions or variables when setting a variable on a class.
Thanks to AndreasLutro on http://laravel.io/irc
So I removed the class variable and added a method. Now everythings works fine.
Code:
class Edit extends Validator{
public static function rules(){
return array(
'email' => 'required|unique:users,email,'.Auth::user()->id
);
}
}
Cheers, Steven.
回答2:
Try to surround the 'required|unique:users,email,'.Auth::user()->id
part with (
and )
so that it looks like this:
public static $rules = [
'email' => ('required|unique:users,email,' . Auth::user()->id)
];
来源:https://stackoverflow.com/questions/21207738/laravel-4-access-auth-class-in-validation-class