Matthew
Matthew

Reputation: 1655

Add a default value to form submission in Laravel

In my controller I've got the destination columns and the origin fields for specific data entered in my create function. However, I've got a few default values in here, like below:

    $shipment->shipment_billing_status = 1;
    $shipment->created_by = $user_id;
    $shipment->cn_billtoName = request('cn_billtoName');

As you can see $shipment->shipment_billing_status default value should always be 1. However, in extreme cases, I would like a user to be able to choose (if they need to) between two checkboxes, titled "No Settle" and "No Charge", or their values being 2 and 3.

So in those extreme cases, I want a record to absolutely go to 1 if the "No Settle" or "No Charge" checkboxes aren't selected.

Upvotes: 1

Views: 1509

Answers (4)

Olasunkanmi
Olasunkanmi

Reputation: 333

Go to your shipment model and add this line

$table->integer('Billing_status')->default(1);

Refresh your migration.

Add this to your view

<div class="form-group">
        {!! Form::label('Billing_status','Status:') !!}
        {!! Form::select('Billing_status',[1=>'shipment_billing_status',2=>'No Settle', 3=>No chare],null,['class'=>'form-control']) !!}

    </div>

This will default shipment_billing_status to 1, if none is selected. I hope this answers your question. @Matthew did you try this?

Upvotes: 0

Omar Tarek
Omar Tarek

Reputation: 744

Let's say that the name of the check boxes input is status(single case since you're only accepting one status or none), I believe you can do something like this:

// Inside the store method.

$status = request('status') ?: 1;

$shipment->shipment_billing_status = $status;

Hopefully this should do the trick.

Upvotes: 0

Rutvij Kothari
Rutvij Kothari

Reputation: 1285

As you mentioned shipment_billing_status is a checkbox and if any of the checkboxes isn't selected form won't have shipment_billing_status field.

A workaround would be:

$shipment->shipment_billing_status = (isset($request->shipment_billing_status) && !empty($request->shipment_billing_status)) ? $request->shipment_billing_status : 1;

Explanation: Condition is if $request has shipment_billing_status field and it's not empty then shipment_billing_status is used, else default value is assigned.

Upvotes: 0

Tim Lewis
Tim Lewis

Reputation: 29278

You can set the default value of an input if it's null, like so:

$shipment->shipment_billing_status = request("shipment_billing_status", 1);

If request("shipment_billing_status") is null, it'll set $shipment->shipment_billing_status to 1. I don't think this works for "" (input being sent but set to an empty value), so you may have to handle as such:

$billingStatus = request("shipment_billing_status");
if(!$billingStatus || $billingStatus == ""){
   $shipment->shipment_billing_status = 1;
} else {
   $shipment->shipment_billing_status = $billingStatus;
}

This should handle cases of sending the value in the POST data or omitting it completely.

Upvotes: 1

Related Questions