Deekep Thapa
Deekep Thapa

Reputation: 45

Laravel : Calculate sum of two fields and store in table column

I have two input field a and b. Create table with column a, b and sum. I want to input value of a and b from form and store the sum of a and b in sum column.

Upvotes: 0

Views: 453

Answers (2)

Cappsy
Cappsy

Reputation: 61

public function store(Request $request){
    $number_one = $request->a;
    $number_two = $request->b;
    $sum = $number_one + $number_two;
    DB::table('yourtable')->insert(
        ['one' => $number_one, 'two' => $number_two,'sum' => $sum]
    );
    
    //OR
    $table = new table; //modelname

    $table->one = $number_one;
    $table->two = $number_two;
    $table->sum = $sum;

    $table->save();
    if(!$table)
    return 0;   
    else return 1;
        
}

Upvotes: 1

Anup Shrestha
Anup Shrestha

Reputation: 129

You can do this way to:

 public function store(Request $request){
       $request["sum"] = $request->a + $request->b;
       SumTable::store($request->all());
    }

Upvotes: 0

Related Questions