我有 4 个 MySQL 表,使用 PHP 和 Laravel 7
现在我想连续显示每个会员的单笔付款和所有其他扣除额。 (假设一个人只有一笔付款)
数据库架构如下
这是我要显示的 HTML 表格
这是我正在使用的查询,但它重复了数据。
$payments = Payment::leftJoin('members', 'payments.member_id', '=', 'members.id')
->leftJoin('payment_deductions', 'payments.id', '=', 'payment_deductions.payment_id')
->leftJoin('deductions', 'payment_deductions.deduction_id', '=', 'deductions.id')
->select(
'members.*',
'payment_deductions.*',
)
->orderBy("member_id", "ASC")
->get()->toArray();
结果数组根据每个成员的推导重复每个成员。
有什么办法可以更好地获取这些数据吗?类似于每个成员的嵌套扣除数组?
这是模型
会员
namespace App;
use IlluminateDatabaseEloquentModel;
use CarbonCarbon;
class Member extends Model
{
protected $fillable = [
'full_name',
'email',
'created_by',
];
}
付款
namespace App;
use IlluminateDatabaseEloquentModel;
class Payment extends Model
{
protected $fillable = [
'member_id',
'total_amount',
'payable_amount',
'created_by',
];
public function deductions() {
return $this->belongsToMany(Deduction::class,'payment_deductions')->withTimestamps();
}
}
扣除
namespace App;
use IlluminateDatabaseEloquentModel;
class Deduction extends Model
{
protected $fillable = [
'title',
'priority',
'created_by',
];
}
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号