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
set0
= 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)
.