我在Laravel中设置了以下关系:
OrderStatus Model
- hasMany('Order')
Order Model
- 'belongsTo('OrderStatus');
使用orders
表和order_statuses
表设置数据库。 orders
表格包含order_status_id
字段。
当我保存订单时,我通过获取相应的订单状态模型手动设置order_status_id
,如下所示:
$status = OrderStatus::where(['name'=>'sample_status'])->firstOrFail();
$order->order_status_id = $status->id;
$order->save();
我想知道是否有内置功能来执行此操作而不是手动设置order_status_id
。我已经读过"附加相关模型"和"关联模型"在Laravel文档中,但我无法弄清楚这些是否适合我的用例。我认为我遇到的问题是我直接使用子模型(订单),并尝试设置它的父级。这有功能吗?
答案 0 :(得分:21)
当然可以这样做:
$status = OrderStatus::where(['name'=>'sample_status'])->firstOrFail();
$order = new Order;
$order->status()->associate($status);
$order->save();
(status()
是belongsTo关系。您可能需要调整该名称)
答案 1 :(得分:10)
保存新相关模型的关系的正确方法如下:
$status = OrderStatus::where(['name'=>'sample_status'])->firstOrFail();
$order = new Order;
$status->order()->save($order);
文档链接:http://laravel.com/docs/4.2/eloquent#inserting-related-models
答案 2 :(得分:0)
您可以使用自定义解决方案。
我正在解释一个示例,该示例刚刚编码且与您的问题非常相似,希望对您有所帮助。 我有一个问题模型和 AnswerOption模型,如下所示。
问题模型
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Question extends Model
{
protected $table = 'questions';
protected $fillable = [
'title',
'created_at',
'updated_at'
];
/**
* Get the answer options for the question.
*/
public function answerOptions()
{
return $this->hasMany('App\Models\AnswerOption');
}
/**
* @param array $answerOptions
*/
public function syncAnswerOptions(array $answerOptions)
{
$children = $this->answerOptions;
$answerOptions = collect($answerOptions);
$deleted_ids = $children->filter(
function ($child) use ($answerOptions) {
return empty(
$answerOptions->where('id', $child->id)->first()
);
}
)->map(function ($child) {
$id = $child->id;
$child->delete();
return $id;
}
);
$attachments = $answerOptions->filter(
function ($answerOption) {
// Old entry (you can add your custom code here)
return empty($answerOption['id']);
}
)->map(function ($answerOption) use ($deleted_ids) {
// New entry (you can add your custom code here)
$answerOption['id'] = $deleted_ids->pop();
return new AnswerOption($answerOption);
});
$this->answerOptions()->saveMany($attachments);
}
}
AnswerOption模型
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AnswerOption extends Model
{
protected $table = 'answer_options';
protected $fillable = [
'question_id',
'title',
'ord',
'created_at',
'updated_at'
];
/**
* Get the question that owns the answer.
*/
public function question()
{
return $this->belongsTo('App\Models\Question');
}
}
在这里您可以看到单个问题hasMany答案选项,可以看到我在模型中使用了BelongsTo和hasMany Relationip。
现在,在保存和更新问题期间,请在QuestionController中保存答案选项。
为此,我在问题模型中编写了 syncAnswerOptions 方法。
您只需要传递带有ID的选项数组,如果数据库中已经存在ID,则它将进行更新;如果ID为空,它将添加一条新记录;如果ID在其中,但不在您的新数组中,那么该记录将被删除。
/**
* If you are attaching AnswerOption(s) for the first time, then pass
* in just the array of attributes:
* [
* [
* // answer option attributes...
* ],
* [
* // answer option attributes...
* ],
* ]
*//**
* If you are attaching new AnswerOption(s) along with existing
* options, then you need to pass the `id` attribute as well.
* [
* [
* 'id' => 24
* ],
* [
* // new answer option attributes...
* ],
* ]
*/
在问题控制器的存储和更新方法中,在问题添加和更新调用之后立即调用此方法。
$question->syncAnswerOptions($data['answerOptions']);
$ data ['answerOptions']是答案选项数组,就像注释中所述。