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]);
}
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.
– Samuel Carvalho
Add your Model Product to the description so I can understand the relationship better.
– Samuel Carvalho
Okay, I added the model above.
– Herton Vilarim
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
@Lodi added this, but it hasn’t worked yet, I’ll try an Artisan migrate:refresh.
– Herton Vilarim