简体   繁体   English

在字符串上调用成员函数 addEagerConstraints() - Laravel 8

[英]Call to a member function addEagerConstraints() on string - Laravel 8

I want to get response with type_name property, without adding new field in the table我想使用type_name属性获得响应,而不在表中添加新字段

{
  "status": 200,
  "message": "OK",
  "data": {
    "id": 23,
    "uuid": "9b1d33f9-0e44-4161-9936-ec41309697a5",
    "sender_id": null,
    "receiver_id": 2,
    "type": 0,
    "coin": 200,
    "balance": 27000,
    "description": "Topup 200 coin",
    "type_name": "Topup"
}

Therefore I tried to create a method named typeName() inside the CoinTransaction model, in the hope that the method can be called via with() method like that:因此我尝试在 CoinTransaction 模型中创建一个名为 typeName() 的方法,希望可以通过 with() 方法调用该方法,如下所示:

$transaction = CoinTransaction::create([
     'receiver_id'   => auth()->user()->id,
     'coin'          => $request->coin,
     'balance'       => $predefineCoin->balance ?? 1000,
     'type'          => 0,
     'description'   => $request->description
]);

$transaction = CoinTransaction::with(['typeName'])->find($transaction->id);

But it's return an error:但它返回一个错误:

Error: Call to a member function addEagerConstraints() on string...

In my CoinTransaction model在我的 CoinTransaction 模型中

class CoinTransaction extends Model
{
    use HasFactory;

    protected $guarded  = ['id'];


    public function sender() {
        return $this->belongsTo(User::class, 'sender_id');
    }


    public function receiver() {
        return $this->belongsTo(User::class, 'receiver_id');
    }


    public function typeName() {
        $typeName = null;

        switch($this->type){
            case 0: $typeName = 'Topup'; break;
            default: $typeName = 'Unknown';
        }

        return $typeName;
    }
}

typeName is not relationship method so you have typeName() like below typeName不是relationship method所以你有typeName()像下面

$coin=CoinTransaction::find($transaction->id); 
dd($coin->typeName());

or或者

To add type_name attribute to existing response ,you can use Mutators要将type_name属性添加到现有响应,您可以使用 Mutators

Ref : https://laravel.com/docs/8.x/eloquent-mutators#accessors-and-mutators参考: https : //laravel.com/docs/8.x/eloquent-mutators#accessors-and-mutators

So add below property and method in CoinTransaction所以在CoinTransaction添加以下属性和方法

protected $appends = ['type_name'];

and method和方法

public function getTypeNameAttribute()
{
    $typeName = null;

        switch($this->type){
            case 0: $typeName = 'Topup'; break;
            default: $typeName = 'Unknown';
        }

        return $typeName;    
}

so in controller所以在控制器中

$transaction = CoinTransaction::find($transaction->id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel-在字符串上调用成员函数addEagerConstraints() - Laravel - Call to a member function addEagerConstraints() on string 在字符串上调用成员函数 addEagerConstraints() - Call to a member function addEagerConstraints() on string 在float LARAVEL上调用成员函数addEagerConstraints() - Call to a member function addEagerConstraints() on float LARAVEL Laravel在布尔值上调用成员函数addEagerConstraints() - Laravel call to a member function addEagerConstraints() on boolean Laravel 调用成员 function addEagerConstraints() on float - Laravel call to a member function addEagerConstraints() on float 在 null 上调用成员 function addEagerConstraints() - Call to a member function addEagerConstraints() on null Laravel 5 查询关系导致“调用成员函数 addEagerConstraints() on null”错误 - Laravel 5 Querying with relations causes "Call to a member function addEagerConstraints() on null" error 在 null 上调用成员函数 addEagerConstraints() 在 laravel 6 上出现此错误 - Call to a member function addEagerConstraints() on null getting this error on laravel 6 我在 integer 上收到对成员 function addEagerConstraints() 的错误调用 - I get The Error Call to a member function addEagerConstraints() on integer 由于条件关系,在 null 上调用成员函数 addEagerConstraints() - Call to a member function addEagerConstraints() on null because of a conditional relationship
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM