Explorar el Código

Link authors from book index table, eager load authors

axkuhta hace 1 año
padre
commit
6ed1b48487
Se han modificado 2 ficheros con 3 adiciones y 1 borrados
  1. 1 1
      app/Http/Controllers/BookController.php
  2. 2 0
      resources/views/books.blade.php

+ 1 - 1
app/Http/Controllers/BookController.php

@@ -13,7 +13,7 @@ use App\Models\Author;
 
 class BookController extends Controller {
 	function index() {
-		$books = Book::all(); // Достать все книги
+		$books = Book::all()->load("author"); // Достать все книги
 
 		return view("books", ["rows" => $books]);
 	}

+ 2 - 0
resources/views/books.blade.php

@@ -9,6 +9,7 @@
 	<th>ISBN</th>
 	<th>Год</th>
 	<th>Страниц</th>
+	<th>Автор</th>
 </tr>
 @foreach ($rows as $row)
 	<tr>
@@ -17,6 +18,7 @@
 		<td>{{$row->isbn}}</td>
 		<td>{{$row->year}}</td>
 		<td>{{$row->pagecount}}</td>
+		<td><a href="/author/{{$row->author->id}}">{{$row->author->name}}</a></td>
 	</tr>
 @endforeach
 </table>