7

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 for storing array of images.

Function To fetch single image from an array of images:

<?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 am selecting the imagefile from the table.It fetches the multiple images stored based on single id.But I need only one images from the array of images stored.

Pankaj Makwana
  • 3,030
  • 6
  • 31
  • 47

4 Answers4

3

Your current database query simply lists all possible filenames because there's no ID restriction added.

Since the OP states that you want to look up a file name based on a single ID and your comments on other answers indicate you don't want to limit the fetched records, you could extend your query to include an ID and then perform a search in the resulting Collection:

// 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.

Of course this is just an example of how to tackle this. You'll definitely need to adjust this to fit in the rest of your code.

Propaganistas
  • 1,674
  • 1
  • 17
  • 40
1

Use the below code the limited records

<?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();
    ?>

check the below updated code: Here you will group the images by products and will get the single product image

<?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();
?>
pravindot17
  • 1,199
  • 1
  • 15
  • 32
  • Sorry,I don't want to limit the records.I just Only want limit the images I'm fetching –  Oct 12 '17 at 08:03
  • `SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'GemSeller.products_photos.filename' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by` –  Oct 21 '17 at 10:02
  • I guess your mysql version is above 5.6, which do support only full group by default, use the following answer to solve this https://stackoverflow.com/a/23921234/6582942 – pravindot17 Oct 23 '17 at 10:33
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();
?>

In this code use first() instead of get().

Parth Shah
  • 311
  • 5
  • 13
1

If you understand well, then you have a lot of product photos. If you have a photo in the database that is a main photo, then you can build a query like:

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

Otherwise use sub-query

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

Sorry for using SQL notation, but I do not know the query-bulder lawrell. And this is just a typical problem with building SQL queries.

bato3
  • 2,695
  • 1
  • 18
  • 26