duanlu1950 2019-01-11 09:49
浏览 45
已采纳

使用laravel单击一个按钮更改两个记录的状态

I have upload form from which Admin can upload different documents. The goal is to have only one active document at the time in the table.

So, admin upload new document which then marks as a Active. After a few days he can upload another one which by default is Inactive. If the admin make the second document to be Active, then the first one should become Inactive.

This is what I have so far.

The route for update

Route::post('/admin/media/{media}', 'Admin\MediaController@completedUpdate')->name('completedUpdate');

The view with the buttons

<td>@if($value->status == 1) 
        <form action="{{ route('completedUpdate', $value->id) }}" method="POST">
            {{ csrf_field() }}                          
            <button type="submit" class="btn btn-success" name="changeStatus" value="0">Active</button>
        </form>                    
    @else
        <form action="{{ route('completedUpdate', $value->id) }}" method="POST">
            {{ csrf_field() }}                              
            <button type="submit" class="btn btn-default" name="changeStatus" value="1">Inactive</button>
        </form>                                                 
    @endif
</td>

And the function in the controller

public function completedUpdate(Request $request, Mediakit $media)
{
    $data = DB::table('media')->get();
    foreach($data as $media) {
        if ( $media->status == 1 ) {
            DB::table('media')->update(['status', 0]);
        }
    }
    $media->status = $request->changeStatus;
    $media->save();
    return redirect()->back()->with('message', 'Status changed!');
}

Current error

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update media set 0 = status, 1 = 0)

I'm not even sure that the function should be like this. What I've tough to do is on button click to select all records, loop them and update all to 0 (inactive) then change only the clicked one to 1(active).

  • 写回答

1条回答 默认 最新

  • douyi7055 2019-01-11 10:15
    关注

    Change

    DB::table('media')->update(['status', 0]);
    

    To

    DB::table('media')->where('status',1)->update(['status' => 0]);
    

    This will update All entries in the media table to have a status of 0 where the status is currently 1.

    You full function will look closer to this:

    public function completedUpdate(Request $request, Mediakit $media)
    {
        // Set ALL records to a status of 0
        DB::table('media')->where('status',1)->update(['status' => 0]);
    
        // Set the passed record to a status of what ever is passed in the Request
        $media->status = $request->changeStatus;
        $media->save();
        return redirect()->back()->with('message', 'Status changed!');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗