php - 使用数据透视表访问其他表信息

标签 php mysql laravel pivot relationship

我有两个表:productsrequests,一个数据透视表 requests_products 保存了 products_idrequests_id 等两条信息。

我还有另一个名为 requests_observations 的表,它保存了 requests_products_id 和那个请求中那个产品的观察结果>.

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

/**
 * @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 的观察结果以及稍后如何获取此信息。

谢谢!

最佳答案

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

关于php - 使用数据透视表访问其他表信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33358223/

相关文章:

php - 在一个查询中同时执行 INSERT 和 UPDATE

php - 如何在wordpress中制作可见菜单

使用 jquery post 上传 PHP 文件

php - 随机字符串是不是很好的验证码

php - 将数据输出为 Excel 文件

php - 在 Laravel 中获取查询而不是按名称替换外键

PHP表单使用多维数组提交到数据库

mysql - 仅在自增 id 不等于 6 时才插入(例如)?

ubuntu - Vagrant 1.1.5 中的共享文件夹权限

laravel - $this->attributes 和 $this->original 有什么区别