Search code examples
phplaravellaravel-5

Laravel ::create method


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?


Solution

  • 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;
    }