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.
这里我从表中选择imagefile。它根据单个id获取存储的多个图像,但我只需要从存储的图像数组中获取一个图像。
4 个解决方案
#1
2
Your current database query simply lists all possible filenames because there's no ID restriction added.
您当前的数据库查询仅仅列出所有可能的文件名,因为没有添加ID限制。
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:
自OP州你想查找一个文件名称基于一个ID,您的评论对其他回答表明你不想限制获取记录,您可以扩展您的查询,包括一个ID,然后执行一个搜索在结果集合:
// 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.
当然,这只是解决这个问题的一个例子。您肯定需要调整它以适应代码的其余部分。
#2
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().
在此代码中使用first()而不是get()。
#3
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();
?>
#4
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.
抱歉使用SQL表示法,但我不知道查询-bulder lawrell。这只是构建SQL查询的一个典型问题。
#1
2
Your current database query simply lists all possible filenames because there's no ID restriction added.
您当前的数据库查询仅仅列出所有可能的文件名,因为没有添加ID限制。
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:
自OP州你想查找一个文件名称基于一个ID,您的评论对其他回答表明你不想限制获取记录,您可以扩展您的查询,包括一个ID,然后执行一个搜索在结果集合:
// 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.
当然,这只是解决这个问题的一个例子。您肯定需要调整它以适应代码的其余部分。
#2
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().
在此代码中使用first()而不是get()。
#3
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();
?>
#4
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.
抱歉使用SQL表示法,但我不知道查询-bulder lawrell。这只是构建SQL查询的一个典型问题。