简体   繁体   中英

How to Update table values with existing method in Laravel?

I am working with Laravel 5.6 with MySql database in My web app.

and I have following function method to add order,

public function addOrder()
{
$input = Input::all();

$subscribed= false;
if(isset($input['subscribed']))
{
$subscribed= true;
}

and In My system I have table name as vehicles as following,

**vehicles table**
id   name   number   adtype
1    car     123       0
2    van     159       0
3    car     258       0
4    lorry   147       0
5    van     298       0
etc..

Now I need update above table adtype values from 0 to 1 when click submit button regarding to above addorder function. then how can write code to update code in side above function????

My url as following....

http://localhost:8000/myads/1/edit/payment

route is,

Route::post('add-order', 'PaymentController@addOrder');

If you are using a model then try the following code:

public function addOrder()
{
    $input = Input::all();
    $subscribed= false;
    if(isset($input['subscribed'])){
        $subscribed= true;
    }
    $vehicle= App\Vehicles::find($input[id]);
    $vehicle->adtype = $subscribed;
    $vehicle->save();
}

If you are using Query Builder ( add use DB; )

public function addOrder()
{
    $input = Input::all();
    $subscribed= false;
    if(isset($input['subscribed'])){
        $subscribed= true;
    }
    DB::table('vehicles')
            ->where('id', $input[id])
            ->update(['adtype' => $subscribed]);
}

You must pass the id of a table row to edit each of them(this is must for any method).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM