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!