drbouzlxb92333332 2017-09-19 05:12
浏览 24
已采纳

如何通过Laravel 5.2一次更新特定的列数据?

I have a users table where i have a column named rank_id. Now i want to update all users rank_id into 2 where rank_id = 6 at a time(click by one button). How can i do that? I have tried so far:

Routes:

Route::post('/test/rankup2', array('as' => 'user.rank2.post', 'uses' => 'TestController@RankUpgradeTwo'));

TestController.php:

function RankUpgradeTwo(Request $request)
{
    $memberAll = User::where('rank_id','=',6)->firstOrFail();
    //dd($memberAll);

    $memberAll->rank_id = '2';

    $memberAll->save();
    return Redirect::to('user/test')->with('message', 'Successfully Ugraded into Rank 2');

}

testScript.blade.php:

    <form method="POST"
        action="{{ route('user.rank2.post') }}"
        class="form-horizontal stdform"
        autocomplete="off"
        enctype="multipart/form-data"
        id="edit-form"
        role="form"
        {{--v-on:submit="submitChange" --}}
        novalidate />
    {{ csrf_field() }}


<div class="control-group action">


    <button type="submit" name="submit" value="submit" id="submit" class="btn btn-blue btn-left">
       Rank Up 2
    </button>       

</div>

{{ Form::close() }}

This shows me successfull message but i don't see any update in database. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongxixia6399 2017-09-19 05:14
    关注

    You can try Eloquent Update

    User::where('rank_id','=',6)->update(['rank_id' => 2]);
    

    OR

    Since firstOrFail will just fetch the first record from the db so you won't be able to update all the records but there is another work around for that e.x

    $memberAll = User::where('rank_id','=',6)->get();
    foreach($memberAll as $member) {
        $member->rank_id = 2;
        $member->save();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题