Laravel Eloquent : 统计两个表的数据
Laravel Eloquent : Count data from two tables
我有两个不同的 table,名为 artists
和 artworks
。我想从 table 中检索数据并在 Summary
table 中显示它们。
这里的条件是:从artists
table得到name
(这个table还有其他列)和从 artworks
table 得到 number of total artworks
。在 Summary
table.
中显示它们
artists
|-------------------|
| id | name |
|-------------------|
| 1 | A |
|-------------------|
| 2 | B |
|-------------------|
| 3 | C |
|-------------------|
artworks
|-----------------------------------------------------|
| id | artist_id | title | medium |
|-----------------------------------------------------|
| 1 | 1 | ABC | Oil |
|-----------------------------------------------------|
| 2 | 1 | DEF | Water |
|-----------------------------------------------------|
| 3 | 1 | GHI | Water |
|-----------------------------------------------------|
| 1 | 2 | JKL | Oil |
|-----------------------------------------------------|
| 2 | 2 | MNO | Water |
|-----------------------------------------------------|
| 3 | 3 | PQR | Oil |
|-----------------------------------------------------|
这就是我想要的:
Summary
|-------------------------------------------|
| No | Artist Name | Total Artwork |
|-------------------------------------------|
| 1 | A | 3 |
|-------------------------------------------|
| 2 | B | 2 |
|-------------------------------------------|
| 3 | C | 1 |
|-------------------------------------------|
我们将不胜感激任何帮助。感谢您的宝贵时间。
您可以对您的关系使用withCount()方法:
$artists = Artist::withCount('artworks')->get();
foreach($artists as $artist) {
echo $artist->artworks_count;
}
因为你想使用数据库查询,所以你需要添加连接。
\DB::table('artists')
->join('artworks', 'artists.id', '=', 'artworks.artist_id')
->select('artists.id as id', 'artists.name as name', \DB::raw("count(artworks.artist_id) as count"))
->groupBy('artists.id')
->get();
如果您想使用 Relation,则在 Artist 模型中使用 hasMany
关系。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Artist extends Model
{
protected $table = 'artists';
public function artworks()
{
return $this->hasMany('App\Artwork','artist_id','id');
}
}
在控制器中
$artists = Artist::withCount('artworks')->get();
foreach($artists as $artist) {
echo $artist->artworks_count;
}
我有两个不同的 table,名为 artists
和 artworks
。我想从 table 中检索数据并在 Summary
table 中显示它们。
这里的条件是:从artists
table得到name
(这个table还有其他列)和从 artworks
table 得到 number of total artworks
。在 Summary
table.
artists
|-------------------|
| id | name |
|-------------------|
| 1 | A |
|-------------------|
| 2 | B |
|-------------------|
| 3 | C |
|-------------------|
artworks
|-----------------------------------------------------|
| id | artist_id | title | medium |
|-----------------------------------------------------|
| 1 | 1 | ABC | Oil |
|-----------------------------------------------------|
| 2 | 1 | DEF | Water |
|-----------------------------------------------------|
| 3 | 1 | GHI | Water |
|-----------------------------------------------------|
| 1 | 2 | JKL | Oil |
|-----------------------------------------------------|
| 2 | 2 | MNO | Water |
|-----------------------------------------------------|
| 3 | 3 | PQR | Oil |
|-----------------------------------------------------|
这就是我想要的:
Summary
|-------------------------------------------|
| No | Artist Name | Total Artwork |
|-------------------------------------------|
| 1 | A | 3 |
|-------------------------------------------|
| 2 | B | 2 |
|-------------------------------------------|
| 3 | C | 1 |
|-------------------------------------------|
我们将不胜感激任何帮助。感谢您的宝贵时间。
您可以对您的关系使用withCount()方法:
$artists = Artist::withCount('artworks')->get();
foreach($artists as $artist) {
echo $artist->artworks_count;
}
因为你想使用数据库查询,所以你需要添加连接。
\DB::table('artists')
->join('artworks', 'artists.id', '=', 'artworks.artist_id')
->select('artists.id as id', 'artists.name as name', \DB::raw("count(artworks.artist_id) as count"))
->groupBy('artists.id')
->get();
如果您想使用 Relation,则在 Artist 模型中使用 hasMany
关系。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Artist extends Model
{
protected $table = 'artists';
public function artworks()
{
return $this->hasMany('App\Artwork','artist_id','id');
}
}
在控制器中
$artists = Artist::withCount('artworks')->get();
foreach($artists as $artist) {
echo $artist->artworks_count;
}