mardi 1 août 2017

Laravel Query retrieves only one pair of values using relationship 'Join' between two tables

Im trying to query products table that displays the following schema in database ->

Table 1 -> products:

Table:

enter image description here

With a relationship with table products_details in model product.php ->

public function productdetails()
    {
        return $this->hasMany('App\Models\ProductDetails', 'productId', 'id');
    }

Table 2 -> products_details table displays many value pair (name / value ) for each product:

Table:

enter image description here

With a relationship with table products in model productdetails.php ->

public function product()
{
    return $this->hasOne('App\Models\Product', 'productSku', 'productId');
}

Using Tinker to query model I run:

App\Models\Product::join('products_details', 'products.productSku', '=', 'products_details.productId')->take(1)->get();

gives result:

Illuminate\Database\Eloquent\Collection {#1787
     all: [
       App\Models\Product {#1779
         id: 1,
         productName: "Samsung - Galaxy Core Prime 4G",
         productSku: "3953367",
         more info...
         created_at: "2017-04-08 18:28:46",
         updated_at: "2017-04-08 18:28:46",
         deleted_at: null,
         productId: 3953367,
         name: "Operating System",
         value: "Android 4.4 KitKat",
       },
     ],
   }

My question is, how can I query results in order to receive all name/value associated to productId, taking into consideration right now I only receive the first pair of values contained at product_details table, but not all the name/values associated with that productId.

     productId: 3953367,
     name: "Operating System",
     value: "Android 4.4 KitKat",

thank you!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire