Good afternoon, I'm using Laravel 5.7 to create an ordering system. So I have 3 tables: customers, products and orders.
For the time being my migrations look like this:
Migrations:
migration clients:
Schema::create('clientes', function (Blueprint $table) {
$table->increments('id');
$table->text('nome_completo');
$table->text('rua');
$table->text('numero');
$table->text('bairro');
$table->text('email');
$table->text('tel');
$table->text('empresa');
$table->timestamps();
});
migration products:
Schema::create('produtos', function (Blueprint $table) {
$table->increments('id');
$table->text('nome');
$table->float('valor');
$table->timestamps();
});
migration requests:
Schema::create('pedidos', function (Blueprint $table) {
$table->increments('id');
$table->integer('cliente_id')->unsigned();
$table->foreign('cliente_id')
->references('id')->on('clientes');
$table->integer('produto_id')->unsigned();
$table->foreign('produto_id')
->references('id')->on('produtos');
$table->integer('quantidade');
$table->float('total', 8, 2);
$table->boolean('pago');
$table->timestamps();
});
Models:
App \ Product
public function pedidos()
{
return $this->belongsTo(Pedidos::class);
}
App \ Client
public function pedidos()
{
return $this->belongsTo(Pedidos::class);
}
App \ Orders
protected $table = 'pedidos';
protected $primaryKey = 'id';
public function clientes()
{
return $this->belongsTo(Cliente::class);
}
public function produtos()
{
return $this->hasMany(Produto::class);
}
Now what would the controllers look like? I would like to recover ALL requests! I've tried
$pedidos = Pedido::all()->produtos;
And nothing!
The table that will be displayed in the Orders view should contain the customer name, product name, quantity of each product, and total value.