【问题标题】:Working with pivot table to access other table information使用数据透视表访问其他表信息
【发布时间】:2016-01-26 06:55:45
【问题描述】:

我有两个表:productsrequests,一个保存 products_idrequests_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


    【解决方案1】:

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

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

    如果你有表 requests_observations 并且有更多属性,你需要做其他模型 RequestsObservations 并像普通模型一样编辑它

    二维模式 默认情况下,只有模型键会出现在数据透视对象上。如果您的数据透视表包含额外的属性,您必须在定义关系时指定它们:

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

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

    【讨论】:

    • 我已经更新了我的问题,我添加了我的模型和数据库,我相信你回答的我已经做了,只需要知道如何在这个架构中实现 requests_observations。谢谢!
    【解决方案2】:

    我通过对代码进行一些更改解决了我的问题。

    我在我的请求和产品模型中使用了$hidden,并且隐藏了 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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-20
      • 1970-01-01
      • 1970-01-01
      • 2021-08-06
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      相关资源
      最近更新 更多