dszdiavv474681 2015-10-27 00:55 采纳率: 33.3%
浏览 42
已采纳

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

I have two tables: products and requests, a pivot requests_products that save the products_id, requests_id and other two informations.

I also have another table called requests_observations that save the requests_products_id and an observation for that product in that request.

In my Requests model I have a belongsToMany to Products

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

But what I need to do is to add an observation for a requests_products_id, I have a model for this table, but I don't know where I put the hasMany, in Products or Requests model.

Thank you.

Update

Product model

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

Requests model

<?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 |                |
+----------------------+------------------+------+-----+---------------------+----------------+

I would like to know how do I insert an observation from a requests_products_id and how do I get this information later.

Thanks!

  • 写回答

2条回答 默认 最新

  • dongzhan8001 2015-11-01 18:26
    关注

    I resolved my problem with some changes in my code.

    I was using $hidden in my Request and Product models, and I was hidden the pivot. This was enabling me to call the pivot option when I was accessing the model property.

    I also did what Marco Feregrino answer, I added in my Request.php, products() function the option to specify the pivots from that relationship.

    public function products()
    {
       return $this->belongsToMany('App\Products', 'requests_products')->withPivot('id', 'unity_price', 'quantity', 'total_price')->withTimestamps();
    }
    

    My final $hidden variable is protected $hidden = ['created_at', 'updated_at'];

    In my Products.php I did not need to change anything.

    Now to work with the pivot, I just need to find the correct product, from a request.

    $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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?