gpt4 book ai didi

php - Laravel/Eloquent 具有模型特征的碰撞

转载 作者:行者123 更新时间:2023-12-04 03:44:28 25 4
gpt4 key购买 nike

我正在为我的项目使用 laravel 5.8,我收到错误 Bad call Call to undefined method Illuminate\Database\Eloquent\Builder::withSum() 因为我删除了 LaravelSubQueryTrait 在我的特质中。当我重新添加 LaravelSubQueryTrait 时,我的项目没有运行,我得到这个错误 Trait method newBaseQueryBuilder has not been applied, because there are collisions with other trait methods on App\Traits\TestModelTrait 。在这一点上升级到 laravel 8.x 不是一个选项,我不太明白我做错了什么。非常感谢阅读任何提示/建议/资源,以帮助解决我的问题。谢谢

我的 TestmodelTrait 如下:

use Alexmg86\LaravelSubQuery\Traits\LaravelSubQueryTrait;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Watson\Rememberable\Rememberable;

trait MedyqModelTrait
{
protected static $skipUuid = false;

use MedyqTrait, SoftDeletes, Rememberable, LaravelSubQueryTrait;

protected static function boot()
{
parent::boot();

self::creating(function ($record) {

if (!self::$skipUuid) {
$record->{'uuid'} = (string)Str::orderedUuid();
}

if (auth()->check()) {
$record->{'created_by'} = auth()->id();
$record->{'updated_by'} = auth()->id();
}
});

self::updating(function ($record) {

if (auth()->check()) {
$record->{'updated_by'} = auth()->id();
}

});

self::updated(function ($record) {

$record->flushCache();

});

self::deleted(function ($record) {

if (auth()->check()) {

$record->{'deleted_by'} = auth()->id();

$record->save();
}

});

self::restored(function ($record) {

if (auth()->check()) {

$record->{'restored_by'} = auth()->id();

$record->{'restored_at'} = Carbon::now();

$record->save();
}

});
}

/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
return 'uuid';
}

public function scopeAlive($query)
{
$query->where("status", config("constants.status.alive"));
}

}

我的 TestTrait 看起来像这样:

use App\Entities\User;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

trait MedyqTrait
{
public static function getTableName()
{
return with(new static)->getTable();
}

public function scopeActive($query)
{
$query->where("status", config("constants.status.active"));
}

public function scopeInActive($query)
{
$query->where("status", config("constants.status.inactive"));
}

/**
* Mass (bulk) insert for Postgres Laravel 5
*
* insert([
* ['id'=>1,'value'=>10],
* ['id'=>2,'value'=>60]
* ]);
*
*
* @param array $rows
* @param null $table
* @param bool $skipUUid
* @param bool $timestamps
* @param bool $authorstamp
* @return bool
* @throws Exception
*/
public function bulkInsert(array $rows, $table = null, $skipUUid = false, $timestamps = true, $authorstamp = true)
{
$return_values = array_filter($rows, 'is_array');
if (!count($return_values) > 0) {
throw new Exception("This function expects a multidimensional associative array");
}

if (is_null($table)) {
$table = DB::getTablePrefix() . $this->getTable();
}

$rows = array_map(function ($item) use ($skipUUid, $timestamps, $authorstamp) {
$authenticated = auth()->check();
$authorId = auth()->id();
$time = Carbon::now();
if (!$skipUUid) {
$item['uuid'] = Str::orderedUuid();
}
if ($timestamps && !key_exists('created_by', $item) && $authenticated && $authorstamp) {
$item['created_by'] = $authorId;
}
if ($timestamps && !key_exists('updated_by', $item) && $authenticated && $authorstamp) {
$item['updated_by'] = $authorId;
}
if ($timestamps && !key_exists('created_at', $item)) {
$item['created_at'] = $time;
}
if ($timestamps && !key_exists('updated_at', $item)) {
$item['updated_at'] = $time;
}
return $item;
}, $rows);

$first = reset($rows);

$columns = implode(',',
array_map(function ($value) {
return "$value";
}, array_keys($first))
);

$values = implode(',', array_map(function ($row) {
return '(' . implode(',',
array_map(function ($value) {
return "'" . str_replace("'", "''", $value) . "'";
}, $row)
) . ')';
}, $rows)
);


$sql = "INSERT INTO {$table}({$columns}) VALUES {$values}";

return DB::statement($sql);
}

public function deletedBy()
{
return $this->belongsTo(User::class, "deleted_by", "id");
}
}

最佳答案

我猜你的语法有问题。

$invoice = Invoice::where('id', $invoice->id)
->with('items')
->withSum('items', 'total_amount')
->withSum('items', 'amount_paid')
->first();

withSum 可能仅从 Laravel 8.x 版本开始可用,在此之前我认为只有 withCount 可用。但是如果你仍然想在早期版本中使用 withSum 你可以定义一个宏'

关于php - Laravel/Eloquent 具有模型特征的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65398066/

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