1

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?? 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. I am getting undefined index for image. so how shall i proceed ?

2
  • Where's the controller can you show Commented Jan 18, 2018 at 13:39
  • as of now controller is nothing but print_r(Input::all()); Commented Jan 18, 2018 at 14:31

2 Answers 2

2

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'); 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 :))

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Hope this helps ;)

7 Comments

I'm not an expert on Laravel, but could you please try using Input::file and Input::hasFile. As explained here: link. Using Input::all, as you already used, should probably work. I'm just curious about whether the file is actually being uploaded.
yes me to wondering if files are actually been uploaded or not. Since its a nested array the way i think i can try to getting image is through looping. But in loop cant use File or hasFile, and if i try to doInput::hasFile(); or Input::file(); it returns nothing blank
Have you looked through your apache and php error log? Maybe something is going wrong with the file upload. E.g file is to large or something like that.
Yes, nothing is there. Btw i changed the input form little bit and now i can see in the response that the image is being uploaded. Now the only problem remaining is how to capture that image
I'm not sure if this is actually the problem, but the documentation states that you need to run the command Input::file('photo')->move($destinationPath); or Input::file('photo')->move($destinationPath, $fileName); to move the actual file to a directory (saving the file). In your case I think it should be (if in loop): Input::file($nameOfArray['product'][$i]['image'])->move($destinationPath); Or Input::file($nameOfArray['product'][1]['image'])->move($destinationPath); If not in a loop. On a side note: Actually it's kinda strange the array starts at index 1 :P
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.