How to get a single image from an array of inserted images in laravel?

Controller function:

<?php

public function addImages(Request $request, $imagesProductId) {

    $product = Product::create($request->all());
    $filenames = array();

    if (empty($request->images)) {
        $message = "error";
        return Redirect::back()->with('message', $message);
    }


    $rules = [
        'images' => 'mimes:jpeg,jpg,png'                 // allowed MIMEs
// size in pixels
    ];



    $validator = Validator::make($request->all(), $rules);
    $result = $validator->fails() ? 'QCVerified' : 'QCFailed';



    foreach ($request->images as $photo) {

//  echo($result);
        $filename = $photo->store('public/uploadedImages');
        $filename = substr($filename, 22);
        $filenames[] = asset('storage/uploadedImages/' . $filename);
        ProductsPhoto::create([
            'nonliveStatus' => $result,
            'product_id' => $product->id,
            'productId' => $imagesProductId,
            'filename' => $filename
        ]);
    }
    return response()->json($filenames);
}
?>

This is my storage function to store an array of images.

Function To get a single image from an image array:

<?php
$liveValues = priceInfo::join('productDescription', 'productDescription.productId', '=', 'productPriceDetails.productId')
        ->join('productAdditionalInformation', 'productAdditionalInformation.productId', '=', 'productPriceDetails.productId')
        ->join('products_photos', 'products_photos.productId', '=', 'productAdditionalInformation.productId')
        ->select('products_phots.filename')
        ->where('productPriceDetails.nonliveStatus', '=', "QCVerified")
        ->get();
?>

Here I select the image file from the table. It retrieves multiple images stored based on a single identifier. But I need only one image from an array of saved images.

+6
source share
4 answers

In the current database query, all possible file names are simply listed, since an ID restriction has been added.

OP , , , , , , :

// I've included the "productId" column here, but feel free to include any other column as required.
$liveValues = priceInfo::join('productDescription',     'productDescription.productId', '=', 'productPriceDetails.productId')
                       ->join('productAdditionalInformation',     'productAdditionalInformation.productId', '=', 'productPriceDetails.productId')
                       ->join('products_photos', 'products_photos.productId', '=',     'productAdditionalInformation.productId')
                       ->select('products_photos.productId', 'products_photos.filename')
                       ->where('productPriceDetails.nonliveStatus', '=', "QCVerified")
                       ->get();

// Get the image filename for a given productId.
// ID calculation logic here.
$productId = 512; // Sample ID

// $liveValues is a Collection.
$filename = $liveValues->where('productId', $productId)
                        ->pluck('filename')
                        ->first(); // Get first filename if there are multiple.

, , . , .

+2

<?php
$liveValues = priceInfo::join('productDescription', 'productDescription.productId', '=', 'productPriceDetails.productId')
        ->join('productAdditionalInformation', 'productAdditionalInformation.productId', '=', 'productPriceDetails.productId')
        ->join('products_photos', 'products_photos.productId', '=', 'productAdditionalInformation.productId')
        ->select('products_phots.filename')
        ->where('productPriceDetails.nonliveStatus', '=', "QCVerified")
        ->limit(1)
        ->get();
    ?>

: .

<?php
$liveValues = priceInfo::join('productDescription', 'productDescription.productId', '=', 'productPriceDetails.productId')
    ->join('productAdditionalInformation', 'productAdditionalInformation.productId', '=', 'productPriceDetails.productId')
    ->join('products_photos', 'products_photos.productId', '=', 'productAdditionalInformation.productId')
    ->select('products_phots.filename')
    ->where('productPriceDetails.nonliveStatus', '=', "QCVerified")
    ->limit(1)
    ->groupBy('products_photos.productId')
    ->get();
?>
+1
<?php
$liveValues = priceInfo::join('productDescription', 'productDescription.productId', '=', 'productPriceDetails.productId')
        ->join('productAdditionalInformation', 'productAdditionalInformation.productId', '=', 'productPriceDetails.productId')
        ->join('products_photos', 'products_photos.productId', '=', 'productAdditionalInformation.productId')
        ->select('products_phots.filename')
        ->where('productPriceDetails.nonliveStatus', '=', "QCVerified")
        ->get();
?>

first() get().

+1

, . , , , :

SELECT ... FROM ... 
    [LEFT] JOIN products_photos 
        ON products_photos.productId = productAdditionalInformation.productId 
            AND products_photos.is_main = 1

SELECT ... , (SELECT filename FROM products_photos WHERE productId = productAdditionalInformation.productId [ORDER BY productId  DESC]) 
    FROM ...

Sorry to use SQL notation, but I don't know what the query name is. And this is a typical problem with building SQL queries.

+1
source

All Articles