【问题标题】:Getting the value of another table and summing it up获取另一个表的值并将其汇总
【发布时间】:2021-07-24 08:22:37
【问题描述】:

我有一个表用户和表支付,它们使用一对多关系相互连接, 在我的用户表中,我有一个列名 client_type,其值为 {住宅、商业、医疗和工业),如果此 clientType 中的任何一个付款,他的 user_id 与支付的金额一起存储在付款中。现在,我想将我的 payment_table 中任何此 clientType 支付的所有金额相加。

下面是我的代码 用户模型

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name',
        'last_name',
        'address',
        'phone',
        'email',
        'lga',
        'ogwema_ref',
        'password',
        'role',
        'client_type'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function payments()
    {
        return $this->hasMany(Payment::class);
    }
}

// Payment Model
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Payment extends Model
{
    use HasFactory;
    
    protected $fillable = [
        'user_id',
        'amount',
        'bank_charges',
        'ref',
        'paystack_ref',
        'status',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
This is the code am trying to solve it
Route::get('/chart', function () {
    $residential = DB::select('select id from users where client_type = ?', ['residential']);
    return $residential->payments;
    
    
});

我的输出 错误异常 试图获得非对象的财产“付款”

如何解决这个问题,提前谢谢,

【问题讨论】:

    标签: laravel eloquent-relationship


    【解决方案1】:

    此选择命令 $residential = DB::select('select id from users where client_type = ?', ['residential']); 返回结果数组(不是对象),因此这一行 $residential-&gt;payments 抛出 ErrorException

    select命令的逻辑应该是这样的:

    $totalMoney = DB::select('select sum(amount) from users INNER JOIN payments ON users.id=payments.user_id where users.client_type = ?', ['residential']);
    

    【讨论】:

    • 谢谢..它工作得很好,如果我只想在付款表中添加状态成功的金额怎么办
    • 已经解决了,我这样写 $totalMoney = DB::select('select sum(amount) from users INNER JOIN payment ON users.id=payments.user_id where users.client_type = ? AND payment.status = ?', ['住宅', '成功]);它解决了它..非常感谢我真的很感激..给你的肘部更多的油脂
    • 你能投票给我的答案吗?这是我继续支持他人的动力。谢谢并祝你一切顺利。
    • 我的结果以这种形式出现 [{"sum(amount)":"2000.00"}]..我想将其转换为数组,以便获得 2000.00 值
    【解决方案2】:

    您收到此错误是因为您编写了一个本机查询,因此它没有访问 eloquent 模型,因此您可以通过 eloquent 查询来修复它

    $residential = User::where('client_type', 'residential')->with('payments')->get();
    

    如果您想获得每个用户的总付款,您可以添加withSum 方法

    $residential = User::where('client_type', 'residential')->withSum('payments', 'amount')->get();
    

    【讨论】:

    • $residential = User::where('client_type', 'residential')->with('payments')->get();这工作完美,但是当我在用户模型中编写函数 paymentSum 并编写查询时,这是显示的错误.. 我将 withSum() 函数更改为 sum() 显示的错误是调用成员函数 groupBy()字符串......可能是什么原因造成的......感谢您的解决方案
    • @Abhay 很抱歉这个错误再次检查答案我修复了它
    • 谢谢老板..它用这种方式解决它。真的很感激..给你的肘部更多的油脂
    • 先生,我正在尝试总结状态成功的金额..我该怎么做..提前谢谢
    • 你可以在witSum之后添加一个类似-&gt;where('status, 'success')-&gt;get()的语句
    猜你喜欢
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2016-06-15
    相关资源
    最近更新 更多