我在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文档中阅读了"附加相关模型"和"关联模型",但我无法弄清楚这些是否适合我的用例。我认为我遇到的问题是我直接使用子模型(订单),并尝试设置它的父模型。有这个功能吗?
当然你可以这样做:
$status = OrderStatus::where(['name'=>'sample_status'])->firstOrFail();
$order = new Order;
$order->status()->associate($status);
$order->save();
(status()
是属于关系。您可能需要调整该名称)
保存新相关模型的关系的正确方法如下:
$status = OrderStatus::where(['name'=>'sample_status'])->firstOrFail();
$order = new Order;
$status->order()->save($order);
文档链接 : http://laravel.com/docs/4.2/eloquent#inserting-related-models
您可以使用自定义解决方案。
我正在解释一个例子,刚刚编码并且与您的问题非常相似,希望它会有所帮助。我有一个问题模型和答案选项模型,如下所示。
问题模型
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);
}
}
答案选项模型
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');
}
}
在这里你可以看到一个问题有很多答案选项,你可以看到我用过属于,有很多关系在模型中。
现在,在问题保存和更新期间,在问题控制器中,您还可以保存答案选项。
为此,我在问题模型中编写了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'] 是答案选项数组,就像注释中所述一样。