Reputation: 137
I have a problem when I try to create new User with User::create[]
method.
I read Laravel create method this problem, but it does not help me:
Below is my User model:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password','phone','rating'
];
protected $hidden = [
'password', 'remember_token',
];
}
Here, my controller code:
public function create() {
$input = Request::all();
User::create[$input];
return $input;
}
PhpShtorm shows me this notification:
Constant create not found in ...
How can I fix it, and create new user?
Upvotes: 3
Views: 25937
Reputation: 7772
You are trying to access create
as an array, it is a method, like so: create()
. Change the square brackets in the code below.
Your IDE may not recognize the method because it is called using magic methods
. If you want to properly recognize the method, use laravel-ide-helper.
public function create() {
$input = Request::all();
// $input = request()->all() <-- Using a helper function
User::create($input); // <-- Change this
return $input;
}
Upvotes: 4
Reputation: 11916
You need to call create()
. Assuming you want to return the created user, then do this.
public function create()
{
$user = User::create(request()->all());
return $user;
}
If you really wanted to return the input then you can do this.
public function create()
{
$inputs = request()->all();
User::create($inputs);
return $inputs;
}
Also ignore the warnings on magic methods and fluent syntaxes in IDE. Laravel is not the kindest when it comes to that. You can use this package to help with that.
https://github.com/barryvdh/laravel-ide-helper
Upvotes: 2
Reputation: 21698
This syntax look for a constant with name bar
in class Foo
.
Foo::bar
If bar is a method, ... you need to call it with parenthesys
Foo::bar()
If you write Foo::bar['foo']; you expect to find a constant like
class Foo
{
const bar = ['foo' => 'bar'];
}
I dont think this is the right syntax
You are in App namespace. Exists App\User class? And App\User class have a method 'create'?
Sometimes IDE show errors even if code works perfectly.
Upvotes: 1