Reputation:
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.
Upvotes: 7
Views: 1546
Reputation: 2815
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.
Upvotes: 1
Reputation: 1792
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.
Upvotes: 3
Reputation: 1273
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();
?>
Upvotes: 1
Reputation: 411
<?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().
Upvotes: 1