Querying only one row from a one to many relationship laravel



PHP Snippet 1:

public function pictures()
{
    return $this->hasMany('App\ProductPicture', 'product_id', 'id');
}

PHP Snippet 2:

public function product()
{
    return $this->belongsTo('App\Product', 'id', 'product_id');
}

PHP Snippet 3:

$products = Product::with(['pictures' => function($q) {
    $q->take(1);
])->get();

PHP Snippet 4:

public function singlePicture()
{
    return $this->hasMany('App\ProductPicture', 'product_id', 'id')->limit(1);
}

PHP Snippet 5:

$products = Product::with(['singlePicture'])->get();

PHP Snippet 6:

DB::raw("GROUP_CONCAT(`product_picture_id` SEPARATOR ',') as product_picture_ids")

PHP Snippet 7:

$products = Product::join('product_pictures', 'products.product_id', '=', 'product_pictures.product_id')
        ->select('product_name', 'quantity', 'products.product_id', DB::raw(" GROUP_CONCAT(`product_picture_id` SEPARATOR ',') as product_picture_ids"))
        ->groupBy('products.product_id')
        ->get();

PHP Snippet 8:

$products = Product::join('product_pictures as pp', 'products.product_id', '=', 'pp.product_id')
    ->select('products.product_name', 'products.quantity', 'products.product_id', 'pp.product_picture_url')
    ->groupBy('products.id')
    ->get();