如何从 MySql table 中输出排名靠前的读者?
How do I output top readers from MySql table?
我想从图书馆管理系统中输出排名靠前的读者,看看今年谁阅读最多,但我被查询构建卡住了。
我有 3 个 table,
书籍
id
name
1
name of the book1
2
name of the book2
3
name of the book3
4
name of the book4
book_issues
id
student_id
book_id
issue_date
1
1
1
2022-05-09
2
2
3
2022-05-01
3
3
5
2022-05-06
2
2
2
2022-05-08
3
2
1
2022-05-03
和学生
id
name
1
name of the student1
2
name of the student2
3
name of the student3
4
name of the student4
这是我的控制器
public function topmonth_wise()
{
return view('report.topmonthWise', ['books' => '']);
}
public function generate_topmonth_wise_report(Request $request)
{
$request->validate(['month' => "required|date"]);
return view('report.topmonthWise', [
'books' => book_issue::where('issue_date', 'LIKE', '%' . $request->month . '%')
// ->select(student::raw('count(book_id) as top'))
//->groupBy('top')
//->orderBy('top', 'DESC')
->limit(10)
//->latest()
->get(),
]);
}
这是我的观点:
<table class="content-table">
<thead>
<th>Nr. Crt</th>
<th>Nume elev</th>
<th>Total carti citite</th>
</thead>
<tbody>
@forelse ($books as $book)
<tr>
<td>{{ $book->id }}</td>
<td>{{ $book->student->name }}</td>
<td>{{ $book->count() }}</td>
</tr>
@empty
<tr>
<td colspan="10">Nu s-au găsit înregistrări!</td>
</tr>
@endforelse
</tbody>
</table>
这是我的模型:
public function student(): BelongsTo
{
return $this->belongsTo(student::class, 'student_id', 'id');
}
/**
* Get the book that owns the book_issue
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function book(): BelongsTo
{
return $this->belongsTo(book::class, 'book_id', 'id');
}
我想在我的视图中显示本月的热门读者,但如果我使用来自控制器的代码,我会收到错误消息
我想要的只是显示这样的 table 的视图:
id
student name
total books read
1
student name 2
3
2
student name 1
1
3
student name 3
1
如果你想看其他文件我给你问问
谁能帮帮我?
您需要按 student_id
分组,然后您需要计算该特定 id 内的项目总数。
//here we loaded students as well as we need to know the name
//and then group by `student_id`
$book_issues = book_issue::with('student')
->where('issue_date', 'LIKE', '%' . $request->month . '%')
->get()
->groupBy('student_id');
$row = 1;
@foreach($book_issues as $k => $v)
<tr>
<td>{{$row}}</td>
<td>{{$v->first()->student->name}}</td>
<td>{{$$v->count()}}</td>
</tr>
<?php
$row++;
?>
@endforeach
我想从图书馆管理系统中输出排名靠前的读者,看看今年谁阅读最多,但我被查询构建卡住了。
我有 3 个 table,
书籍
id | name |
---|---|
1 | name of the book1 |
2 | name of the book2 |
3 | name of the book3 |
4 | name of the book4 |
book_issues
id | student_id | book_id | issue_date |
---|---|---|---|
1 | 1 | 1 | 2022-05-09 |
2 | 2 | 3 | 2022-05-01 |
3 | 3 | 5 | 2022-05-06 |
2 | 2 | 2 | 2022-05-08 |
3 | 2 | 1 | 2022-05-03 |
和学生
id | name |
---|---|
1 | name of the student1 |
2 | name of the student2 |
3 | name of the student3 |
4 | name of the student4 |
这是我的控制器
public function topmonth_wise()
{
return view('report.topmonthWise', ['books' => '']);
}
public function generate_topmonth_wise_report(Request $request)
{
$request->validate(['month' => "required|date"]);
return view('report.topmonthWise', [
'books' => book_issue::where('issue_date', 'LIKE', '%' . $request->month . '%')
// ->select(student::raw('count(book_id) as top'))
//->groupBy('top')
//->orderBy('top', 'DESC')
->limit(10)
//->latest()
->get(),
]);
}
这是我的观点:
<table class="content-table">
<thead>
<th>Nr. Crt</th>
<th>Nume elev</th>
<th>Total carti citite</th>
</thead>
<tbody>
@forelse ($books as $book)
<tr>
<td>{{ $book->id }}</td>
<td>{{ $book->student->name }}</td>
<td>{{ $book->count() }}</td>
</tr>
@empty
<tr>
<td colspan="10">Nu s-au găsit înregistrări!</td>
</tr>
@endforelse
</tbody>
</table>
这是我的模型:
public function student(): BelongsTo
{
return $this->belongsTo(student::class, 'student_id', 'id');
}
/**
* Get the book that owns the book_issue
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function book(): BelongsTo
{
return $this->belongsTo(book::class, 'book_id', 'id');
}
我想在我的视图中显示本月的热门读者,但如果我使用来自控制器的代码,我会收到错误消息 我想要的只是显示这样的 table 的视图:
id | student name | total books read |
---|---|---|
1 | student name 2 | 3 |
2 | student name 1 | 1 |
3 | student name 3 | 1 |
如果你想看其他文件我给你问问 谁能帮帮我?
您需要按 student_id
分组,然后您需要计算该特定 id 内的项目总数。
//here we loaded students as well as we need to know the name
//and then group by `student_id`
$book_issues = book_issue::with('student')
->where('issue_date', 'LIKE', '%' . $request->month . '%')
->get()
->groupBy('student_id');
$row = 1;
@foreach($book_issues as $k => $v)
<tr>
<td>{{$row}}</td>
<td>{{$v->first()->student->name}}</td>
<td>{{$$v->count()}}</td>
</tr>
<?php
$row++;
?>
@endforeach