Laravel newbie here! Below function retrieves all chapters of a book from a country specified as a slug (in Laravel 5.8):
$country = Country::where('slug', $slug)->first();
$results = $country->with('libraries.books.chapters')->get();
How can I retrieve all chapters from this output in Blade?
[{
"id": 1,
"created_at": "2019-09-13 22:41:18",
"name": "USA",
"libraries": [{
"id": 626,
"created_at": "2019-09-13 22:41:20",
"name": "Stephen A. Schwarzman Library",
"books": [{
"id": 125,
"title": "Manhattan Beach",
"author": "Jennifer Egan",
"created_at": "2019-09-13 22:41:20",
"chapters": [{
"id": 9413,
"created_at": "2019-09-13 22:41:26",
"nr": 1
}, {
"id": 9414,
"created_at": "2019-09-13 22:41:26",
"nr": 2
}, {
"id": 9415,
"created_at": "2019-09-13 22:41:26",
"nr": 3
}, {
"id": 9416,
"created_at": "2019-09-13 22:41:26",
"nr": 4
}]
}]
}]
}]
This output is JSON (which I outputted using Blade , but I can't seem to find the right foreach syntax to retrieve all chapters. Below gives a syntax error on line 2 unexpected ')', expecting '['
:
@foreach ($results->libraries as library)
@foreach ($library->books as book)
@foreach ($book->chapters as chapter)
@endforeach
@endforeach
@endforeach
Treating as an array results in the same syntax error:
@foreach ($results['libraries'] as library)
@foreach ($library['books'] as book)
@foreach ($book['chapters'] as chapter)
@endforeach
@endforeach
@endforeach
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire