2

User.php code, here, whether I use fillable or gaurded, I get the same error.

<?php

namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     *@var array
     */
    // protected $fillable = [
    //     'name',
    //     'email', 
    //     'password',
    // ];
    
    protected $guarded = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

UserController.php code, here, I have tried the mass assignment

<?php

namespace App\Http\Controllers;


use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\User;
use Illuminate\Database\Eloquent\Model;




class UserController extends Controller
{
    public function index()
    {
        $data = [
            'name' => 'elon',
            'email' => 'elon@gmail.com',
            'password' => 'password',
        ];

        User::create($data);

        $user = User::all();
        return $user;
    }
}
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
sunny
  • 21
  • 1
  • 1
  • 2

7 Answers7

8

You seem to not be importing the user class from the right namespace in your UserController.php

You are using

use Illuminate\Foundation\Auth\User;

Use

use App\Models\User;

instead.

Edit:

$fillable is not the problem in this case as $guarded is set to an empty array which allows for all fields to be mass assignable through the create method. Eloquent mass assignment

Remy
  • 777
  • 2
  • 8
  • 15
3

There are two problems in the code provided:

  1. As commented by @sta, you should allow the Model attributes to be mass assignable by using the $fillable property in the User class:
<?php

namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     *@var array
     */
     protected $fillable = [
         'name',
         'email', 
         'password',
     ];
    
    protected $guarded = [];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}
  1. As commented by @Remy, we should make sure to use the correct class:
<?php

namespace App\Http\Controllers;

use App\Models\User; // <-- corrected line

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;

class UserController extends Controller
{
    public function index()
    {
        $data = [
            'name' => 'elon',
            'email' => 'elon@gmail.com',
            'password' => 'password',
        ];

        User::create($data);

        $user = User::all();
        return $user;
    }
}
Stefano Amorelli
  • 4,553
  • 3
  • 14
  • 30
1

I found this helpful for me in laravel 8, this worked fine in all versions because many times if we import class and it auto import another one so please check that you import this class or another one.

use App\Models\User;
mightyteja
  • 825
  • 1
  • 14
  • 38
aakash rajput
  • 81
  • 1
  • 3
0

in the UserController.php try to use

use App\Models\User;
0

in laravel 7 this work for me :

use App\User;
0

For me, I had to stop the server in terminal with ctrl + c and the restart the server with php artisan serve It worked for me.

Godana Emiru
  • 48
  • 1
  • 9
0

by adding this in my model

protected $guarded = [];

it save me from my misery thanks!

Wilfred
  • 69
  • 9
  • 1
    Please don't add _thanks_ as answers. They don't actually provide an answer to the question, and can be perceived as noise by its future visitors. Instead, [upvote answers](/help/privileges/vote-up) you like. This way future visitors of the question will see a higher vote count on that answer, and the answerer will also be rewarded with reputation points. See [Why is voting important](/help/why-vote). – Yunnosch Jun 04 '22 at 18:53