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!');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?