【发布时间】:2021-08-11 19:32:40
【问题描述】:
I need to create a checkbox, which when selected my user is considered as ACTIVE, and when this checkbox is not selected the user is considered as INACTIVE 我的数据库中有一个名为 (is_active) 的列,它是 tinyInteger,默认为 1,但如果我单击该复选框,它必须变为 0
我需要用一个复选框来做到这一点,我的代码是这样的
<form action="/is_active/save" method="post" class="lg:text-left">
@csrf
<div class="mb-3">
<div class="p-6 card bordered">
<div class="form-control">
<input type="checkbox" name="is_active">
<label>Active</label><br/><br/>
</div>
</div>
</div>
<button type="submit"
class="inline-flex mt-1 justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-blue-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
Save
</button>
</form>
我的表单被定向到
Route::post('/is_active/save', function () {
$userId = auth()->user()->id;
$is_active = request('is_active');
$userId->is_active = $is_active;
$userId->save();
return redirect("people");
})->middleware(['auth']);
今天这是我保存表单后的结果
尝试在 int 上分配属性“is_active”
如何让我的复选框将值 1 更改为 0? 有人可以帮助我吗?谢谢!
【问题讨论】:
-
勾选时没有
value=""的复选框会“打开”到服务器。未选中时,他们什么也不发送。此外,您将复选框命名为terms,所以我不确定request('is_active');应该引用什么。如果你做一个dd($request->is_active),你会得到什么? -
另外,当您分配
$userId = auth()->user()->id;时,$userId是一个整数,您不能这样做$userId->is_active,因为您不能将属性分配给一个整数。您的代码需要大量重构... -
您好,感谢您的回复!已经调整了我的问题
-
好的,你改好了名字,这是很好的第一步。检查我的其余 cmets;你还有一些问题要解决????
-
用 dd,我得到一个空值