Explorar o código

Author selector when adding a book

axkuhta hai 1 ano
pai
achega
b11db23779

+ 7 - 4
app/Http/Controllers/BookController.php

@@ -9,6 +9,7 @@ use Illuminate\Http\Request;
 
 // Фасад DB здесь не используется; здесь используется модель
 use App\Models\Book;
+use App\Models\Author;
 
 class BookController extends Controller {
 	function index() {
@@ -18,7 +19,7 @@ class BookController extends Controller {
 	}
 
 	function add() {
-		return view("add_book_form");
+		return view("add_book_form", ["authors" => Author::all()]);
 	}
 
 	function view(Book $book) {
@@ -36,9 +37,11 @@ class BookController extends Controller {
 			"annotation" => "nullable",
 			"pagecount" => "nullable|numeric",
 			"year" => "nullable|numeric",
-			"isbn" => "nullable"
+			"isbn" => "nullable",
+			"author" => "required|exists:authors,id"
 		], [
-			"name" => "Книга должна иметь название."
+			"name" => "Книга должна иметь название.",
+			"author" => "Книга должна иметь автора."
 		]);
 
 		$arr = $request;
@@ -49,7 +52,7 @@ class BookController extends Controller {
 		$book->pagecount = $arr->pagecount;
 		$book->year = $arr->year;
 		$book->isbn = $arr->isbn;
-		$book->authors = 0;
+		$book->author_id = $arr->author;
 		$book->save();
 
 		return view("success");

+ 10 - 0
resources/views/add_book_form.blade.php

@@ -55,6 +55,16 @@
 		</label>
 	</div>
 
+	<div>
+		<label>
+			<div>Автор:</div>
+			@include("include.author_selector")
+			@error("author")
+				<span class="alert">{{ $message }}</span>
+			@enderror
+		</label>
+	</div>
+
 	<input type="submit">
 </form>
 @endsection

+ 8 - 0
resources/views/include/author_selector.blade.php

@@ -0,0 +1,8 @@
+<div class="author-selector">
+@foreach ($authors as $author)
+	<label>
+		<input type="radio" name="author" value="{{ $author->id }}">
+		<span>{{ $author->name }}</span>
+	</label>
+@endforeach
+</div>

+ 11 - 2
resources/views/layouts/app.blade.php

@@ -61,17 +61,26 @@
 				}
 			}
 
-			label {
+			form > div > label {
 				border-left: 2px solid #D0D0D0;
 				display: block;
 				padding-left: 0.3rem;
 				margin: .3rem 0;
 			}
 
-			label:focus-within {
+			form > div > label:focus-within {
 				border-color: #00FF60;
 			}
 
+			.author-selector {
+				font-size: 22px;
+			}
+
+			.author-selector label {
+				display: block;
+				padding: .2rem 0rem;
+			}
+
 			input, textarea {
 				font-size: inherit;
 				border: 1px solid #D0D0D0;