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

报告相同问题?

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因