Laravel 5.1更新查询不会改变值

时间:2016-01-21 00:02:27

标签: php mysql forms laravel-5 sql-update

晚上好,

我遇到了查询更新问题,它没有返回任何错误,但也没有更新定义字段用户定义字段中的值。我已尝试删除foreach并在查询中添加特定元素,如下所示,但这也不会影响字段结果。

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

控制器

 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::get('/Display', array(
        'middleware' => 'auth',
        'uses' => 'StockController@Display'
    ));

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

    });

查看:

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

1 个答案:

答案 0 :(得分:1)

您应该使用User模型而不是调用DB::table('users')。然后,如果您将protected $guarded = [];添加到模型中,则应该能够毫无问题地进行更新。

在您的代码中,您可以将DB::table('users')->where('id', '=', $uid)...更改为User::where('id', '=', $uid)...