我有两个表:products
和requests
,一个 requests_products 轴,用于保存 products_id , requests_id 和其他两个信息。
我还有另一个名为requests_observations
的表,用于保存{em>请求中的requests_products_id
和产品的观察结果。
在我的请求模型中,我有一个belongsToMany
到产品
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function products()
{
return $this->belongsToMany('App\Products', 'requests_products')->withTimestamps();
}
但我需要做的是为 requests_products_id 添加一个观察,我有一个这个表的模型,但我不知道我把 添加到了何处 ,在产品或请求模型中。
谢谢。
更新
产品型号
<?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插入观察,以及如何在以后获取此信息。
谢谢!
答案 0 :(得分:1)
1种模式 这是很多人 //模型请求
Public Sub OpenConnection()
Set conn = New ADODB.Connection
conn.Open GetConnectionString()
End Sub
Function GetConnectionString() As String
Dim ConnectionString$
ConnectionString$ = "DRIVER={MySQL ODBC 5.3 UNICODE Driver}; _
SERVER=localhost;DATABASE=test;USER=root;PASSWORD=google;Option=3"
GetConnectionString = ConnectionString$
End Function
如果你有表requests_observations并且有更多的atrributes你需要做其他模型RequestsObservations并像普通模型一样编辑它
2d模式 默认情况下,只有模型键才会出现在数据透视表对象上。如果数据透视表包含额外属性,则必须在定义关系时指定它们:
public function products()
{
return $this->belongsToMany(Product::class());
}
//model Products
public function request()
{
return $this->belongsToMany(ModelRequest::class());
}
http://laravel.com/docs/5.1/eloquent-relationships#many-to-many
答案 1 :(得分:0)
我通过代码中的一些更改解决了我的问题。
我在我的请求和产品模型中使用了$hidden
,而我隐藏了 pivot 。这使我能够在访问模型属性时调用pivot选项。
我也做了 Marco Feregrino answer,我在Request.php中添加了products()函数,用于指定该关系中的枢轴
public function products()
{
return $this->belongsToMany('App\Products', 'requests_products')->withPivot('id', 'unity_price', 'quantity', 'total_price')->withTimestamps();
}
我的最终 $ hidden 变量为protected $hidden = ['created_at', 'updated_at'];
在我的Products.php中,我不需要改变任何东西。
现在使用数据透视表,我只需要从请求中找到正确的产品。
$productRequest = $request->products()->find($productInformation->id);
// Create the RequestsObservations instance
$requestsObservations = new RequestsObservation();
$requestsObservations->requests_products_id = $productRequest->pivot->id;
$requestsObservations->observation = $productInformation->observation;
$requestsObservations->save();