3

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?

4
  • 2
    User::create($input);create is not an array it is a method. Commented May 29, 2017 at 12:37
  • i.imgur.com/b7cGcKo.png Commented May 29, 2017 at 12:39
  • @HakanSONMEZ please add what you mention in answer, because it right answer . Commented May 29, 2017 at 12:39
  • 2
    @ДимаСвободин Your problem is your IDE is searching for magic methods. Commented May 29, 2017 at 12:45

3 Answers 3

4

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

@ДимаСвободин The IDE cannot find it because it is called using magic methods but it works.
2

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

Comments

1

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.

1 Comment

Is an IDE issue: check if code works on your browser. Sometimes IDE do not recognize code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.