[英]How to get image from an nested input array Laravel
I am trying to get input value in a nested array like the one showed bellow.But the problem is the image array is missing or if not I don't know how to get the value of the image file. 我试图在一个嵌套的数组中获取输入值,如下面的波纹管所示。但是问题是图像数组丢失了,否则我不知道如何获取图像文件的值。
This is how i want 这就是我想要的
Array
(
[_token] => 7iSeeTphiVbpQw3iQ8eb1lReMRzxBY8Lt1lKqQea
[food] => Array
(
[product[1] => Array
(
[name] => 1
[price] => 10
[image]=>Uploaded image here
)
)
)
But what i am actually getting is this 但是我实际上得到的是
Array
(
[_token] => 7iSeeTphiVbpQw3iQ8eb1lReMRzxBY8Lt1lKqQea
[food] => Array
(
[product[1] => Array
(
[name] => 1
[price] => 10
)
[product[2] => Array
(
[name] => 1
[price] => 10
)
)
)
And my form blade is like this 我的模板刀片就是这样
<form method="POST" action="" enctype="multipart/form-data">
{{csrf_field()}}
<!-----PHP LOOP STARTS HERE ---->
<select class="form-control" name="food[product[$i][name]]">
<option value="1">Fried rice</option>
<option value="2">Demo food</option>
</select>
<input type="text" placeholder="set a price" class="form-control" name="food[product[$i][price]]">
<input type="file" placeholder="set a price" class="form-control" name="food[product[$i][image]]">
<!-----PHP LOOP ENDS HERE ---->
<button type='submit'>Submit</button>
Can anyone help me how can i get the image so i can store in folder and save the url to database?? 谁能帮助我如何获取图像,以便将其存储在文件夹中并将URL保存到数据库? In controller 在控制器中
UPDATE I changed the form 更新我更改了表格
<input type="text" placeholder="set a price" class="form-control" name="product[$i][price]">
<input type="file" placeholder="set a price" class="form-control" name="product[$i][image]">
In reponse i am getting 作为回应,我越来越
[product] => Array
(
[1] => Array
(
[name] => 1
[price] => 10
[image] => Illuminate\Http\UploadedFile Object
(
[test:Symfony\Component\HttpFoundation\File\UploadedFile:private] =>
[originalName:Symfony\Component\HttpFoundation\File\UploadedFile:private] => dell-inspiron-3567-notebook-original-imaetu5ch98vzge5.jpeg
[mimeType:Symfony\Component\HttpFoundation\File\UploadedFile:private] => image/jpeg
[size:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 40705
[error:Symfony\Component\HttpFoundation\File\UploadedFile:private] => 0
[hashName:protected] =>
[pathName:SplFileInfo:private] => C:\xampp\tmp\php17B2.tmp
[fileName:SplFileInfo:private] => php17B2.tmp
)
)
After doing that i can see the image is being uploaded. 这样做之后,我可以看到图像正在上传。 Now how to capture that image ?? 现在如何捕获该图像? If i try foreach loop that image is getting missing. 如果我尝试foreach循环,该图像将丢失。 I am getting undefined index for image. 我正在获取图像的未定义索引。 so how shall i proceed ? 那我该如何进行呢?
It appears as if you don't have a file field in your form. 似乎您的窗体中没有文件字段。 If I understand you correctly you want users to be able to upload images. 如果我对您的理解正确,则希望用户能够上传图像。 You will need to change the type of the last text input to file. 您将需要更改输入到文件的最后一个文本的类型。
<input type="file" placeholder="set a price" class="form-control" name="food[product[$i][image]]">
And then you can get your image by using $_FILES in your php. 然后,您可以通过在PHP中使用$ _FILES获得图像。
Hope this helps ;) 希望这可以帮助 ;)
It seems like it can't be done in normal way we do to get image and verified if exist with Input::hasFile('image') or Input::file('image');
看来我们无法以正常方式来获取图像并使用Input::hasFile('image') or Input::file('image');
来验证它是否存在Input::hasFile('image') or Input::file('image');
So this is how i did it. 所以这就是我的做法。
for ($i = 1; $i<=count(Input::get('product'));$i++){
$data['name']=$request->product[$i]['name'];
if (isset($request->product[$i]['price']) && !empty($request->product[$i]['price']) && isset($request->product[$i]['image'])){
$data['price']=$request->product[$i]['price'];
//upload image
$name= substr(md5(str_random(10)), 0, 8);
$imageName = $name.'.'.$request->product[$i]['image']->getClientOriginalExtension();;
$request->product[$i]['image']->move(public_path('images/food/'), $imageName);
$data['image']= 'images/product/'.$imageName;
if (Product::create($data)){
$msg ='All product has been successfully uploaded';
}
}else{
$msg ='Opps!! Something went wrong.';
}
}
I don't know if there is any better way to do so. 我不知道是否有更好的方法。 if so please do let me know. 如果是这样,请告诉我。
And thank you @qvotaxon for helping me out and everyone too thanks again :)) 谢谢@qvotaxon帮助我,大家也再次感谢:))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.