0
votes

*Asking for your help again please. Im getting the above error on registration module. This is working before. I tried to make a my profile edit page and then this happens. Hoping for your help please.. Thank you in advance *

User Controller:

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Model\user\user;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;

class UserController extends Controller
{

    public function __construct()
    {
        $this->middleware('auth');
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = Auth::user();
        return view('user.user.show',compact('user'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate(request(), [
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required|confirmed'
            ]);

        $user = User::create([
            'name' => request('name'),
            'email' => request('email'),
            ]);
        $user->password = bcrypt(request('password'));
        auth()->login($user);

        return redirect(route('index'));;
    }


    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = Auth::user();
        return view('user.user.edit', compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate(request(), [
            'email' => ['required','email',Rule::unique('users')->ignore($id)],
            'password' => ['required','password',Rule::unique('users')->ignore($id)]
             ]);

        //$user = Auth::user();       
        $user = Auth::user();

        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = bcrypt($request->password);
        $user->firstname = $request->firstname;
        $user->middlename = $request->middlename;
        $user->lastname = $request->lastname;
        $user->nationality = $request->nationality;
        $user->gender = $request->gender;
        $user->civilstatus = $request->name;
        $user->mobilenum = $request->mobilenum;
        $user->worknum = $request->worknun;
        $user->workadd = $request->workadd;
        $user->homeadd = $request->homeadd;
        $user->email = $request->email;
        $user->birthday = $request->birthday;

        $user->save();

        return redirect(route('user.user.show'))->with('message','Announcement Updated Succesfully');;

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

Blade file:

                    <form method="POST" action="{{ route('register') }}">
                        @csrf
                    <div class="text-center"><button class="btn btn-primary text-left" style="width: 100%;" type="button"><i class="fa fa-facebook"></i>&nbsp; Continue with Facebook</button></div>
                    <div class="text-center mt-2"><button class="btn btn-light text-left border-dark" style="width: 100%;" type="button"><i class="fa fa-google"></i>&nbsp; Continue with Google</button></div>
                    <form class="mt-4">
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-prepend"><span class="text-primary input-group-text"><i class="fa fa-user-o"></i></span></div><input class="form-control @error('name') is-invalid @enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus placeholder="Full Name">
                                @error('name')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                                <div class="input-group-append"></div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-prepend"><span class="text-primary input-group-text"><i class="fa fa-envelope-o"></i></span></div><input class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" placeholder="Email">
                                @error('email')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                                <div class="input-group-append"></div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-prepend"><span class="text-primary input-group-text"><i class="fa fa-lock"></i></span></div><input class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password" placeholder="Password" type="password">
                                @error('password')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                                <div class="input-group-append"></div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-prepend"><span class="text-primary input-group-text"><i class="fa fa-lock"></i></span></div><input class="form-control" name="password_confirmation" required autocomplete="new-password" placeholder="Confirm Password" type="password">
                                <div class="input-group-append"></div>
                            </div>
                        </div>

Model/users/user.php

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

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'firstname', 'middlename', 'lastname', 'birthday', 'nationality', 
        'gender', 'civilstatus', 'mobilenum', 'worknum', 'workadd',
        'homeadd', 
    ];

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

Please help me. Thank you in advance

2
What version of Laravel are you using?Rwd

2 Answers

2
votes

The reason you're getting this issue is because password isn't mass assignable i.e. it isn't in your $fillable array.

Methods like create() or fill() are protected from mass assignment. To get around this you can either add password to your $fillable array or set it explicitly e.g.

public function store(Request $request)
{
    $this->validate(request(), [
        'name'     => 'required',
        'email'    => 'required|email',
        'password' => 'required|confirmed',
    ]);

    $user = new User([
        'name'  => request('name'),
        'email' => request('email'),
    ]);
    $user->password = bcrypt(request('password'));
    $user->save();

    auth()->login($user);

    return redirect(route('index'));;
}
1
votes

You can do two things:

  1. Make your table definition so the password can be nullable
  2. Use the new User([...]) syntax over User::create([...]). The first one does not insert directly, the latter will.

Table definition example:

Schema::create('users', function ($table) {
    // other user table fields ...
    $table->string('password')->nullable();
});

Example without ::create (as given by @Rwd):

// NOT User::create([...]) but new User([...]);
$user = new User([
  'name'  => request('name'),
  'email' => request('email'),
]);
$user->password = bcrypt(request('password'));
$user->save(); // dont forget to save when not using ::create([...]) syntax