2017-11-06 1 views
1

Ich habe ein Problem, konstanten Wert mit übergeordneten Tabelle Wert in Beziehungstabelle zu verwenden.Laravel 5.4 arithmetische Operation in Modellbeziehung

$cost = 5; 
$product = Product::with(["productPrice" => function($q) use($cost) { 
       $q->select("id", "product_id", "price", \DB::Raw("(('"+ $cost + "' * product.weight)/product.pack_size) + price as cost")); 
      }])->select("id", "sku", "pack_size", "image" ,'weight')->get(); 

in Mysql,

select product.id, product.sku, product.pack_size, product.image, product.weight, product_price.id as product_price_id, product_price.price,(($cost * product.weight)/product.pack_size) + product_price.price as cost from product join product_price on product.id = product_price.product_id 

Abfrage arbeiten bereit, aber wie in Laravel Modell Beziehung zu bedienen?

Antwort

1

hatte ich ein ähnliches Problem und ich löste vorübergehend die DB Fassade und unter Verwendung von DB mit :: raw Konstanten für das Einfügen .... Hoffe, es hilft ... So etwas:

$query= DB::table('product')-> 
     ->join ('product_price','product.id','=','product_price.product_id')-> 
     ->select('product.id', 
      'product.sku', 
      ' product.pack_size', 
      'product.image', 
      'product.weight', 
      'product_price.id as product_price_id', 
      DB::raw('($cost * product.weight)/product.pack_size)'), 
      'product_price.price as cost from product', 
      'product_price.price') 
     )->get(); 
+0

Problem gelöst Verwenden des Abfrage-Generators, aber wie Sie Lösungen mithilfe der eloquenten Beziehung erhalten. Irgendeine Idee? –