【发布时间】:2016-01-26 06:55:45
【问题描述】:
我有两个表:products 和 requests,一个保存 products_id、requests_id 和其他两个信息的枢轴 requests_products .
我还有另一个名为 requests_observations 的表,它保存了 requests_products_id 和对该 request 中该 product 的观察。
在我的请求模型中,我有一个 belongsToMany 到产品
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function products()
{
return $this->belongsToMany('App\Products', 'requests_products')->withTimestamps();
}
但是我需要做的是为 requests_products_id 添加一个观察,我有这个表的模型,但我不知道我把 hasMany 放在哪里,在产品或请求模型中。
谢谢。
更新
产品型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Products extends Model
{
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function category()
{
return $this->belongsTo('App\Categories', 'categories_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function fileUpload()
{
return $this->belongsTo('App\FileUpload', 'file_upload_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function ingredients()
{
return $this->belongsToMany('App\Ingredients', 'products_ingredients')->withTimestamps();
}
}
请求模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Requests extends Model
{
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function board()
{
return $this->belongsTo('App\Boards', 'boards_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function status()
{
return $this->belongsTo('App\Status', 'status_id');
}
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function products()
{
return $this->belongsToMany('App\Products', 'requests_products')->withTimestamps();
}
}
requests_products
mysql> SHOW COLUMNS FROM requests_products;
+-------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| requests_id | int(10) unsigned | NO | MUL | NULL | |
| products_id | int(10) unsigned | NO | MUL | NULL | |
| unity_price | decimal(10,2) | NO | | NULL | |
| quantity | int(11) | NO | | NULL | |
| total_price | decimal(10,2) | NO | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
+-------------+------------------+------+-----+---------------------+----------------+
requests_observations
mysql> SHOW COLUMNS FROM requests_observations;
+----------------------+------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| requests_products_id | int(10) unsigned | NO | MUL | NULL | |
| observation | text | NO | | NULL | |
| created_at | timestamp | NO | | 0000-00-00 00:00:00 | |
| updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
+----------------------+------------------+------+-----+---------------------+----------------+
我想知道如何从 requests_products_id 插入观察结果以及以后如何获取此信息。
谢谢!
【问题讨论】:
标签: php mysql laravel pivot relationship