使用 pivot table 访问其他 table 信息

Working with pivot table to access other table information

我有两个 table:productsrequests,一个枢轴 requests_products 保存 products_idrequests_id等两条信息。

我还有另一个名为 requests_observations 的 table 保存了 requests_products_idproduct 的观察 请求.

在我的 Requests 模型中,我有一个 belongsToMany 到 Products

/**
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 */
public function products()
{
    return $this->belongsToMany('App\Products', 'requests_products')->withTimestamps();
}

但我需要做的是为 requests_products_id 添加一个观察值,我有这个 table 的模型,但我不知道我不知道我把 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 的观察结果以及以后如何获取此信息。

谢谢!

1 模式 这是多对多 //模型请求

public function products()
{
    return $this->belongsToMany(Product::class());
}
//model Products
public function request()
{
    return $this->belongsToMany(ModelRequest::class());
}

如果您有 table requests_observations 并且有更多属性,您需要执行其他模型 RequestsObservations 并像普通模型一样编辑它

二维模式 默认情况下,只有模型键会出现在枢轴对象上。如果你的主元 table 包含额外的属性,你必须在定义关系时指定它们:

return $this->belongsToMany('App\Role')->withPivot('column1', 'column2');

http://laravel.com/docs/5.1/eloquent-relationships#many-to-many

我通过更改代码解决了我的问题。

我在请求和产品模型中使用了 $hidden,我隐藏了 pivot。这使我能够在访问模型 属性.

时调用数据透视选项

我也做了 Marco Feregrino ,我在 Request.php 中添加了产品()函数选项指定该关系的枢轴。

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();