Posts

Showing posts with the label eloquent

store foreign key as zero instead of it's value

store foreign key as zero instead of it's value I have a table that all the values are foreign keys ,when I store these values it save it as zero not the value that I chose , public function create() { $type=type::query()->pluck('type'); $color=color::query()->pluck('colore'); $region=region::query()->pluck('country'); $size=size::query()->pluck('size'); $brand=brand::query()->pluck('company'); //$price=new_product::query()->pluck('price'); return view('sale',compact('type','color','region','size','brand')); } public function store(Request $request) { new_product::create($request->all()); return redirect()->route('sale.index'); } the model: class new_product extends Model { protected $table = 'enter_new_product'; protected $fillable = ['type_id', 'color_id', 'region_id', 'size_...

Laravel Eloquent how to join on a query rather than a table?

Laravel Eloquent how to join on a query rather than a table? I want to achieve this in Laravel: SELECT * FROM products JOIN (SELECT product_id, MIN(price) AS lowest FROM prices GROUP BY product_id) AS q1 ON products.id = q1.product_id ORDER BY q1.lowest; I wrote this, but clearly there is something wrong: $products = new Product(); $products = $products->join( Price::whereNotNull('price')->select('product_id', DB::raw('min(price) as lowest'))->groupBy('product_id'), 'products.id', '=', 'product_id' )->orderBy('lowest')->get(); The error I got: ErrorException in Grammar.php line 39: Object of class IlluminateDatabaseEloquentBuilder could not be converted to string. I'm currently using join(DB::raw('(SELECT product_id, MIN(price) AS lowest FROM prices WHERE price IS NOT NULL GROUP BY product_id) AS q1'), 'products.id', '=', 'q1.product_id') as a workaround. Just won...