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__produtoswherepedidos__produtos.pedidos_id= 1 andpedidos__produtos.pedidos_idis not null group byid_produtoorder byid_produtodesc)
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?