Rod*_*igo 56 php mysql many-to-many pivot-table laravel
我有一个phone_models,phone_problems和一个phone_model_phone_problem数据透视表.数据透视表有一个额外的列"价格".
PhoneModel:
class PhoneModel extends \Eloquent
{
public function problems()
{
return $this->belongsToMany('RL\Phones\Entities\PhoneProblem')->withPivot('price');
}
}
Run Code Online (Sandbox Code Playgroud)
PhoneProblem:
class PhoneProblem extends \Eloquent
{
public function models()
{
return $this->belongsToMany('PhoneModel')->withPivot('price');
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是获得具有特定问题的特定手机的价格.
这就是我现在的方式,但我觉得Laravel有一个内置的Eloquent功能,我找不到这样做的方式更简单:
$model = $this->phoneService->getModelFromSlug($model_slug);
$problem = $this->phoneService->getProblemFromSlug($problem_slug);
Run Code Online (Sandbox Code Playgroud)
所有这一切都是从他们的slug中选择特定的模型和问题.
那么我所做的就是凭借这些凭据我得到的价格是这样的:
$row = DB::table('phone_model_phone_problem')
->where('phone_model_id', '=', $model->id)
->where('phone_problem', '=', $problem->id)
->first();
Run Code Online (Sandbox Code Playgroud)
所以现在我可以得到这样的价格,$row->price
但我觉得需要一个更简单,更"Laravel"的方式来做到这一点.
luk*_*ter 116
当使用与Eloquent的多对多关系时,生成的模型会自动获取pivot
分配的属性.通过该属性,您可以访问数据透视表列.虽然默认情况下只有pivot对象中的键.要将列添加到那里,您需要在定义关系时指定它们:
return $this->belongsToMany('Role')->withPivot('foo', 'bar');
Run Code Online (Sandbox Code Playgroud)
如果您需要更多帮助来配置与Eloquent的关系,请告诉我.
编辑
要查询价格,请执行此操作
$model->problems()->where('phone_problem', $problem->id)->first()->pivot->price
Run Code Online (Sandbox Code Playgroud)
Yev*_*yev 12
要从数据透视表中获取数据:
$price = $model->problems()->findOrFail($problem->id, ['phone_problem'])->pivot->price;
Run Code Online (Sandbox Code Playgroud)
或者,如果您有许多不同价格的记录:
$price = $model->problems()->where('phone_problem', $problem->id)->firstOrFail()->pivot->price;
Run Code Online (Sandbox Code Playgroud)
此外.
要更新数据透视表中的数据,您可以采用新的方式:
$model->problems()->sync([$problemId => [ 'price' => $newPrice] ], false);
Run Code Online (Sandbox Code Playgroud)
第二个参数设置为false意味着您不会分离所有其他相关模型.
或者,走老路
$model->problems()->updateExistingPivot($problemId, ['price' => $newPrice]);
Run Code Online (Sandbox Code Playgroud)
并提醒你:
要删除:
$model->problems()->detach($problemId);
Run Code Online (Sandbox Code Playgroud)
要创建新的:
$model->problems()->attach($problemId, ['price' => 22]);
Run Code Online (Sandbox Code Playgroud)
它经过测试并证明在Laravel 5.1中工作了解更多.
拉拉维尔 5.8~
如果你想制作一个自定义的枢轴模型,你可以这样做:
帐户.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Account extends Model
{
public function users()
{
return $this->belongsToMany(User::class)
->using(AccountUserPivot::class)
->withPivot(
'status',
'status_updated_at',
'status_updated_by',
'role'
);
}
}
Run Code Online (Sandbox Code Playgroud)
帐户用户数据透视表.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class AccountUserPivot extends Pivot
{
protected $appends = [
'status_updated_by_nice',
];
public function getStatusUpdatedByNiceAttribute()
{
$user = User::find($this->status_updated_by);
if (!$user) return 'n/a';
return $user->name;
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,Account
是您的普通模型,并且您拥有$account->users
的account_user
连接表具有标准列account_id
和user_id
。
如果您创建自定义数据透视模型,则可以将属性和变更器添加到关系的列上。在上面的示例中,创建 AccountUserPivot 模型后,您可以通过 指示您的 Account 模型使用它->using(AccountUserPivot::class)
。
然后,您可以访问此处其他答案中显示的所有内容,但您也可以通过访问示例属性$account->user[0]->pivot->status_updated_by_nice
(假设这status_updated_by
是用户表中 ID 的外键)。
有关更多文档,请参阅https://laravel.com/docs/5.8/eloquent-relationships(我建议按 CTRL+F 并搜索“pivot”)