m4martie's avatar

How to set default value when storing a data

Hello, I'm new to laravel, can I ask if how to set a default value when storing a data?

0 likes
6 replies
m4martie's avatar
public function store(Request $request)
                {

                    $user = User::create([
                    'firstname' = $request->firstname,
                    'lastname' = $request->lastname,
                    'username' = $request->username,
                    'email' = $request->email,
                    'password' = bcrypt($request->password),
                    'row_status' => 1,
                    ]);

                    return redirect('/home')

                }

here's my RegisterController

Cronix's avatar
Cronix
Best Answer
Level 67

And what do you want set by default, the row_status?

That's best done in your migration when you create the field.

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('firstname');
    $table->string('lastname');
    // other fields...
    $table->tinyint('row_status')->default(1); // give default value of 1
});

Then you can do

$user = User::create([
       'firstname' = $request->firstname,
       'lastname' = $request->lastname,
       'username' = $request->username,
       'email' = $request->email,
       'password' = bcrypt($request->password),
]);

and if you didn't specify row_status, it will insert a 1 for it.

m4martie's avatar

ohhh tinyint it is? because I put

$table->string('row_status')->default(1);

to my migration

Please or to participate in this conversation.