0

I'm trying to make a shopping cart with laravel and am having trouble with one of the methods

this is the error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'pedidos__produtos.pedidos_id' in 'where clause' (SQL: select id_produto, sum(total) as Total, count(1) as qtd from pedidos__produtos where pedidos__produtos.pedidos_id = 1 and pedidos__produtos.pedidos_id is not null group by id_produto order by id_produto desc)

I searched the entire code, but I did not refer this field "pedidos_id" anywhere This error happens when I call "$pedidos[0]->pedido_produtos," in Carrinhocontroller.php

These are the related methods and migrations:

CarrinhoController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Pedidos; use Illuminate\Support\Facades\Auth; class CarrinhoController extends Controller { function __construct(){ $this->middleware('auth'); } public function index(){ $pedidos = Pedidos::where([ 'id_user' => Auth::id() ])->get(); dd([ $pedidos, $pedidos[0]->pedido_produtos, //$pedidos[0]->pedidos_produtos[0]->produto ]); return view('carrinho.index', compact('pedidos')); } } 

Pedidos.php

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Pedidos extends Model { public function pedido_produtos(){ return $this->hasMany('App\Pedidos_Produtos') ->select( \DB::raw('id_produto, sum(total) as Total, count(1) as qtd')) ->groupBy('id_produto') ->orderBy('id_produto', 'desc'); } } 

Pedidos_Produtos.php

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Pedidos_Produtos extends Model { public function produto(){ return $this->belongsTo('App\Produtos', 'id_produto', 'id'); } } 

Migration from Pedidos:

<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePedidosTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('pedidos', function (Blueprint $table) { $table->increments('id'); $table->integer('id_user')->unsigned(); $table->foreign('id_user')->references('id')->on('users'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('pedidos'); } } 

and from pedidos_produtos

<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreatePedidosProdutosTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('pedidos__produtos', function (Blueprint $table) { $table->increments('id'); $table->integer('id_pedido')->unsigned(); $table->integer('id_produto')->unsigned(); $table->decimal('total', 6, 2)->default(0); $table->timestamps(); $table->foreign('id_pedido')->references('id')->on('pedidos'); $table->foreign('id_produto')->references('id')->on('produtos'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('pedidos__produtos'); } } 

Can Anyone help me with that?

2 Answers 2

1

It generates automatically if foreignKey attribute is not defined on hasMany method. it generates field name from classname_id pattern. Also localKey default value is class PK.

public function hasMany($related, $foreignKey = null, $localKey = null) 

You can use like this.

 return $this->hasMany('App\Pedidos_Produtos','id_pedido') 
Sign up to request clarification or add additional context in comments.

Comments

1

The problem you are experiencing is probably to do with this line:

return $this->hasMany('App\Pedidos_Produtos') 

If you do not explicitly tell Laravel what the IDs are called on each table when defining a hasMany relationship, it will assume that the id is {table_name}_id which is where the pedidos_id is coming from.

Try adding the foreign and local keys to the hasMany, something like this:

return $this->hasMany('App\Pedidos_Produtos', 'id_pedido', 'id') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.