Level 53
This will make book_id and reader_id fields required and will validate that both ids exists in the database:
$request->validate([
'book_id' => 'required|exists:books,id',
'reader_id' => 'required|exists:readers,id'
]);
I am creating a library application and I have a function, where the user can rent out books. The user can choose the book and the person who rents the book out. However, I want to validate it so, that no empty values can go through, so I want to make it required, but I am not sure how to write the code.
My Controller:
public function store(Request $request)
{
$reader = Reader::find($request->reader_id);
$reader
->books()
->attach(
$request->book_id,
["maxreturndate" => Carbon::now()->addDays(14)]
);
return redirect('checkedouts')->with('success', 'Buch wurde erfolgreich verliehen!');
}
This will make book_id and reader_id fields required and will validate that both ids exists in the database:
$request->validate([
'book_id' => 'required|exists:books,id',
'reader_id' => 'required|exists:readers,id'
]);
Please or to participate in this conversation.