Reputation: 578
I want to save a new record, but Laravel gives me this error Class name must be could object or a string. my code;
public function store(Request $request)
{
$question = new Question;
$question->question = $request->question;
$question->question_type_id = $request->question_type;
$question->user_id = $request->Auth::id();
$question->save();
}
Note: My PHP version is latest .i.e., 7.1.9
Upvotes: 1
Views: 5702
Reputation: 916
The correct syntax for dynamic object key is:
$question->user_id = $request->{Auth::id()};
or if you just want the auth ID, then it is:
$question->user_id = Auth::id();
Upvotes: 1
Reputation: 1779
I assume you created an input field that has a name of the user’s id and you are trying to access it with:
$request->Auth::id()
Which for a user with a user id of one, I assume you are asking to pass request item $request->1
.
This won’t work as Auth::id()
is being passed as a literal. Accessing the request object it’s complaining saying "hey, I can’t accept a class as a variable name, please stop!"
If this is in fact what you are trying to do, and Auth::id()
works (I never used that, I typically use Auth::user()->id
), then you would have to pass the function within parentheses like this:
$request->(Auth::id())
Upvotes: 1
Reputation: 578
I have fixed this issue, thanks to aynber for sorting out my issue. I have changed
$question->user_id = $request->Auth::id();
to
$question->user_id = Auth::id();
Upvotes: 0