tzcoding
tzcoding

Reputation: 67

Laravel mass assigning guarded fields

So basically, I'm using mass assignment for convenience. However I have a guarded field called "userid". I've made the separate code to insert into this field but for some reason I still get the error: SQLSTATE[HY000]: General error: 1364 Field 'userid' doesn't have a default value

Code:

$apply = Apply::create(
        $request->all()
    );
    $apply->userid = Auth::user()->id;
    $apply->save();
    return redirect()->route('apply');

Model:

protected $guarded = ['userid'];

Thanks.

EDIT: Still not fixed, any other solutions?

Upvotes: -2

Views: 129

Answers (1)

Qchmqs
Qchmqs

Reputation: 1805

pass the userid field to the create function,

 $apply = Apply::create(array_merge(
         $request->all(),
         [
            "userid" => Auth::user()->id
         ])
         );

     $apply->save();

return redirect()->route('apply');

Upvotes: 0

Related Questions