gpt4 book ai didi

php - Laravel 5.1 - 表格的拆分内容

转载 作者:行者123 更新时间:2023-11-30 22:34:19 25 4
gpt4 key购买 nike

我有这个 mysql 表:

+----+-------------------------------+---------+--------------+-------------+
| id | title | chapter | date_release | author |
+----+-------------------------------+---------+--------------+-------------+
| 1 | This is should be title One | 1 | 10/08/2015 | John Robert |
| 2 | This is should be title Two | 1 | 11/08/2015 | John Robert |
| 3 | This is should be title Three | 1 | 12/08/2015 | John Robert |
| 4 | This is should be title Four | 2 | 10/09/2015 | Sir Arthur |
| 5 | This is should be title Five | 2 | 11/09/2015 | Sir Arthur |
| 6 | This is should be title Six | 1 | 13/08/2015 | John Robert |
| 7 | This is should be title Seven | 2 | 12/08/2015 | Sir Arthur |
+----+-------------------------------+---------+--------------+-------------+

在 Laravel 5.1 中,我想从基于表的 chapter 和结果 view 中拆分,如下所示:

<div class="chapter-1">
<span>
...
All content from table of chapter 1 only
...
</span>
</div>
<div class="chapter-2">
<span>
...
All content from table of chapter 2 only
...
</span>
</div>

我不知道我应该在 controller 中执行什么查询功能以及如何在 laravel view 中显示它?

已编辑:

@aldrin27 问我关于我的 Controller ,就在这里。

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Book;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class BookController extends Controller {

...
...


public function show($id) {
//
$book = Book::find($id);
return view('books.show', compact('book'));
}

...
...

}

最佳答案

执行 Book::find($id) 只会返回一个模型。您想要的是获得要使用的模型的 Collection(例如:拆分、分组等)。请改用 all()get()

例如,在您的 Controller 中执行以下操作:

$books = Book::all();

$groupedBooks = $books->groupBy('chapter');

$groupedBooks 传递给您的 View ,并在您的 View 文件中执行如下操作:

@foreach($groupedBooks as $chapter => $books)
<div class="chapter-{{ $chapter }}">
@foreach($books as $book)
<p>{{ $book->title }}</p>
@endforeach
</div>
@endforeach

注意:我认为您建立人际关系的方式存在问题。例如,Book 包含(零个或多个?)Chapter,因此它们将是两个相关模型,而不是只使用一个模型 - Book .但那只是我...

关于php - Laravel 5.1 - 表格的拆分内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33027371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com