是否可以使用isEmpty()
方法驗證空字段的輸入a?我有一個validate($request)
它有一個多表格填補。我有其他形式,它不是完全必要的。
控制器:
$this->validate($request,
[
'title' => 'required|regex:/(^[A-Za-z0-9 ]+$)+/|max:255',
'content' => 'required',
'category' => 'required',
'approver' => 'required',
'recipient' => 'required',
]);
$document = new Document();
$user = Auth::user();
//Request in the form
$document->title = $request->title;
$document->content = $request->content;
$document->category_id = $request->category;
$document->save();
foreach($request->recipient as $recipientId)
{
$document->sentToUsers()->sync([ $recipientId => ['sender_id' => $user->id]],false);
}
我有不需要填寫表格名稱department
。我試圖檢查它是否不是空的,它會將其插入到數據庫中。如果空無一物,將會發生。
if(!$request->department->isEmpty())
{
foreach($request->department as $departmentId)
{
foreach(Department::find($departmentId)->users()->get() as $user1) //find the users belonging to the current department
{
$document->sentToUsers()->sync([ $user1->id => ['sender_id' => $user->id]],false);
}
}
}
else
{
}
試試看吧? – Farkie