PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 13, 2022

[FIXED] How to resolve mass assignment error in laravel?

 April 13, 2022     laravel, laravel-5.8, laravel-models, mass-assignment, migration     No comments   

Issue

I am new to the laravel, i am implementing user registration api while registering the user it's throwing an error like mass_assignment

Add [first_name] to fillable property to allow mass assignment on [App\\Entities\\User].

User.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $table ='user';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name','last_name','phone_number','email'
    ];

    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

 public function userRegister(userRequest $request){
        $data = $this->repository->create($request->all());     
        return $user = new UserTransformer($data);                     
    }
}

please help why this error is happening ..?


Solution

The main problem is laravel5.8 the model paths are different so use following facade

use App\modelname

instead of use App\Entities\User,let me know if this resolves your problem :-)



Answered By - Sai Tarun
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing