Felipe Umpierre
Felipe Umpierre

Reputation: 188

Working with pivot table to access other table information

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!

Upvotes: 2

Views: 94

Answers (2)

Felipe Umpierre
Felipe Umpierre

Reputation: 188

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

Upvotes: 0

Marco Feregrino
Marco Feregrino

Reputation: 663

1 mode This is many to many //Model Requests

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

if you have the table requests_observations and has more atrributes you need to do other model RequestsObservations and edit it like normal model

2d mode By default, only the model keys will be present on the pivot object. If your pivot table contains extra attributes, you must specify them when defining the relationship:

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

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

Upvotes: 1

Related Questions