Problem with JOIN in Laravel

Asked

Viewed 123 times

0

I have a little problem when performing a Join.

Basically I had made this code and it was working, but I had the need to change the primary key of the table 'products' to 'code'.

After that, even making the proper modifications to the code, the query remains giving error, because it calls the old foreign key.

Follows the code:

function getProducts($name){

    $id = request()->route('order');
    $order = Order::find($id);
    $refPrice = $order->price_id;
    $price = Price::find($refPrice);
    $idCoin = $price->coin_id;

    $items = Product::where('products.description', 'like', '%' . $name . '%')
        ->where('products.coin_id', $idCoin)
        ->where('prices.id', $refPrice)
        ->leftJoin('price_items', 'products.code', '=', 'price_items.product_code')
        ->rightJoin('prices', 'price_items.price_id', '=', 'prices.id')
        ->get();

    return view('moduleclient.orders.getproducts', ['items' => $items, 'price_id' => $refPrice]);

}

Error: inserir a descrição da imagem aqui

Model Product:

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use App\Manufacturer;
use App\Composition;
use App\Type;
use App\Line;

class Product extends Model
{
    protected $primaryKey = 'code'; // or null

    protected $fillable = ['code', 'description','manufacturer_id', 'composition_id', 'register','validity', 'type_id', 'line_id', 'coin_id'];

    public function manufacturer()
    {
        return $this->belongsTo('App\Manufacturer');
    }

    public function composition()
    {
        return $this->hasOne('App\Composition');
    }

    public function type()
    {
        return $this->hasOne('App\Type');
    }

    public function line()
    {
        return $this->hasOne('App\Line');
    }
}
  • Check your Product model. Maybe there is a link with another table that is still linked to product_id. The error that is returning does not appear to be from the Inner Join, but from another query.

  • Add your Model Product to the description so I can understand the relationship better.

  • Okay, I added the model above.

  • In fillable you can add a 'code AS product_id', maybe it works... But here already ran the commands of Artisan related to db again?

  • @Lodi added this, but it hasn’t worked yet, I’ll try an Artisan migrate:refresh.

1 answer

0


If you change the primary key to another name has no problem, but, what happens is that the relationship keys you need to configure (exists at the Laravel convention, when you change it, the keys need to be mentioned in the relations).

Example of all relationships has already been answered in this answer, then where it has relationship with products, you need to mention the field code.

Examples:

all these configure the key relations.

If you post all the Models correspondent, I put an integrated response with relationships that there will be no problems, but surely by a name change the code stops working.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.