获取额外的pivot表列laravel的值

时间:2022-09-15 20:55:13

I have a phone_models, phone_problems, and a phone_model_phone_problem pivot table. The pivot table has an extra column 'price'.

我有一个phone_models、phone_problems和一个phone_model_phone_problem pivot表。pivot表有一个额外的列“price”。

PhoneModel:

PhoneModel:

class PhoneModel extends \Eloquent
{
    public function problems()
    {
        return $this->belongsToMany('RL\Phones\Entities\PhoneProblem')->withPivot('price');
    }
}

PhoneProblem:

PhoneProblem:

class PhoneProblem extends \Eloquent
{
    public function models()
    {
        return $this->belongsToMany('PhoneModel')->withPivot('price');
    }
}

What I'm trying to do is get the price of a specific phone with a specific problem.

我想做的是得到一个有特定问题的手机的价格。

This is how I have it now but I feel like Laravel has a built in Eloquent feature I can't find to do this in a much simpler way:

这就是我现在的样子,但我觉得Laravel有一个很有说服力的特点,我找不到一种更简单的方式:

$model = $this->phoneService->getModelFromSlug($model_slug);
$problem = $this->phoneService->getProblemFromSlug($problem_slug);

all this does is select the specific model and problem from their slug.

所有这些都是从它们的蛞蝓选择特定的模型和问题。

then what I do is with those credentials I get the price like so:

然后我做的就是用这些凭证我得到的价格是这样的:

$row = DB::table('phone_model_phone_problem')
->where('phone_model_id', '=', $model->id)
->where('phone_problem', '=', $problem->id)
->first();

so now I can get the price like so $row->price but I feel like there needs to be a much easier and more 'Laravel' way to do this.

所以现在我可以得到这样的价格,比如$row->的价格,但我觉得需要有一个更简单,更“Laravel”的方式。

2 个解决方案

#1


79  

When using Many to Many relationships with Eloquent, the resulting model automatically gets a pivot attribute assigned. Through that attribute you're able to access pivot table columns. Although by default there are only the keys in the pivot object. To get your columns in there too, you need to specify them when defining the relationship:

当使用许多到许多有说服力的关系时,结果模型会自动地获得一个被分配的轴心属性。通过该属性,您可以访问pivot表列。虽然默认情况下,pivot对象中只有键。要将列也包含在其中,您需要在定义关系时指定它们:

return $this->belongsToMany('Role')->withPivot('foo', 'bar');

Official Docs

官方文档

If you need more help the task of configuring the relationships with Eloquent, let me know.

如果您需要更多的帮助来配置有说服力的关系,请让我知道。

Edit

编辑

To query the price do this

要查询价格,请执行以下操作

$model->problems()->where('phone_problem', $problem->id)->first()->pivot->price

#2


8  

To get data from pivot table:

从pivot表获取数据:

$price = $model->problems()->findOrFail($problem->id, ['phone_problem'])->pivot->price;

Or if you have many records with different price:

或者如果你有很多不同价格的记录:

$price = $model->problems()->where('phone_problem', $problem->id)->firstOrFail()->pivot->price;

In addition.

此外。

To update data in the pivot you can go NEW WAY:

要更新pivot的数据,可以采用新的方法:

$model->problems()->sync([$problemId => [ 'price' => $newPrice] ], false); 

Where the 2nd param is set to false meaning that you don't detach all the other related models.

如果第2个参数设置为false,则意味着您不会将所有其他相关模型分离。

Or, go old way

或者,走旧路

$model->problems()->updateExistingPivot($problemId, ['price' => $newPrice]);

And remind you:

并提醒你:

To delete:

删除:

$model->problems()->detach($problemId);

To create new:

创建新的:

$model->problems()->attach($problemId, ['price' => 22]);

It has been tested and proved working in Laravel 5.1 Read more.

已经在Laravel 5.1中测试并证明了它的有效性。

#1


79  

When using Many to Many relationships with Eloquent, the resulting model automatically gets a pivot attribute assigned. Through that attribute you're able to access pivot table columns. Although by default there are only the keys in the pivot object. To get your columns in there too, you need to specify them when defining the relationship:

当使用许多到许多有说服力的关系时,结果模型会自动地获得一个被分配的轴心属性。通过该属性,您可以访问pivot表列。虽然默认情况下,pivot对象中只有键。要将列也包含在其中,您需要在定义关系时指定它们:

return $this->belongsToMany('Role')->withPivot('foo', 'bar');

Official Docs

官方文档

If you need more help the task of configuring the relationships with Eloquent, let me know.

如果您需要更多的帮助来配置有说服力的关系,请让我知道。

Edit

编辑

To query the price do this

要查询价格,请执行以下操作

$model->problems()->where('phone_problem', $problem->id)->first()->pivot->price

#2


8  

To get data from pivot table:

从pivot表获取数据:

$price = $model->problems()->findOrFail($problem->id, ['phone_problem'])->pivot->price;

Or if you have many records with different price:

或者如果你有很多不同价格的记录:

$price = $model->problems()->where('phone_problem', $problem->id)->firstOrFail()->pivot->price;

In addition.

此外。

To update data in the pivot you can go NEW WAY:

要更新pivot的数据,可以采用新的方法:

$model->problems()->sync([$problemId => [ 'price' => $newPrice] ], false); 

Where the 2nd param is set to false meaning that you don't detach all the other related models.

如果第2个参数设置为false,则意味着您不会将所有其他相关模型分离。

Or, go old way

或者,走旧路

$model->problems()->updateExistingPivot($problemId, ['price' => $newPrice]);

And remind you:

并提醒你:

To delete:

删除:

$model->problems()->detach($problemId);

To create new:

创建新的:

$model->problems()->attach($problemId, ['price' => 22]);

It has been tested and proved working in Laravel 5.1 Read more.

已经在Laravel 5.1中测试并证明了它的有效性。