Charles
Charles

Reputation: 276

Laravel 5.1 update query not changing value

good evening,

I have run into a problem with a query update it returns no error but also doesn't update the value in the defined field users defined field. I've tried removing the foreach and adding specific elements into the query like shown below but that didn't effect the field results as well.

$UserUpdate = DB::table('users')->where('id', '=', 3)->update(array('StockAcc' => 6));

Controller

 namespace App\Http\Controllers;
use \View as View;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\HTTP\Request;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class TransController extends BaseController
{
    public function Trans(Request $Return)
    {

        $id = $Return->input('id');
        $uid = $Return->input('uid');
        $Math = $Return->input('Math');
        $Pin = $Return->input('Pin');
        $Buy = $Return->input('Buy');
        $Sell = $Return->input('Sell');
        /*
         * Querys for DB results
         */

        $User = DB::Table('users')->where('id', $uid);
        foreach ($User as $item) {
            $Final = $Buy * $Math;
                    $NewMoney = $Final - $item->StockAcc;
                    $UserUpdate = DB::table('users')
                        ->where('id', '=', $uid)
                        ->update(array('StockAcc' => $NewMoney));

        }
    }
}

Route

 Route::get('/Display', array(
        'middleware' => 'auth',
        'uses' => 'StockController@Display'
    ));

    Route::post('/test', function(){

    });

View:

{!!Form::open(array('action' => 'TransController@Trans', 'url' => ' '))!!}

Upvotes: 4

Views: 1793

Answers (1)

Niraj Shah
Niraj Shah

Reputation: 15457

You should use the User model instead of calling DB::table('users'). Then, if you add protected $guarded = []; to the model, you should be able to update without issue.

In your code, you can then change DB::table('users')->where('id', '=', $uid)... to User::where('id', '=', $uid)...

Upvotes: 1

Related Questions