lundi 2 mai 2016

How to update multiple and single images at once in many to many relationship from one controller?

I have managed to store data from one controller in many to many relationship. But when i set down to update it is giving me lots of headache.I have multiple image fields and single image field.This is the reason i am having from problem.

My view form is :

                <div class="col-sm-4">
                    @if(Request::segment(4)==='edit')
                        @foreach($food->foodImages as $image)
                        <div style="margin-top: 10px;">

                            @if(!empty($image->featured_image))

                                <?php
                                $food_featured_image = $image->featured_image;
                                ?>

                                

                            @endif
                        </div>
                        @endforeach

                        <div style="margin-top: 10px;">
                            <input onchange="document.getElementById('foodimage').src = window.URL.createObjectURL(this.files[0])"
                                   name="featured_image"  type="file" placeholder="" value="">
                        </div>

                @else
                        <div style="margin-top: 10px;">
                            <img  width="150px;" height="150px;"  id="foodimage" />
                        </div>
                        <div style="margin-top: 10px;">
                            <input onchange="document.getElementById('foodimage').src = window.URL.createObjectURL(this.files[0])"
                                   name="featured_image" type="file" placeholder="">
                        </div>
                @endif
                        {!! $errors->first('featured_image', '<span class="text-danger">:message</span>') !!}

            </div>


                <label for="image_path" class="col-sm-2 control-label">Upload Image:<span class=help-block" style="color: #b30000">&nbsp;* </span></label>
                <div class="col-sm-4">
                    @if(Request::segment(4)==='edit')

                        @foreach($food->foodImages as $image)
                            @if(!empty($image->image_path))

                                <?php
                                $food_multiImage_path = $image->image_path  ;
                                ?>

                                

                            @endif
                        @endforeach
                            <hr />
                            <div id="dvPreview">
                            </div>
                            <input name="image_path[]" value="" type="file" multiple="multiple" />

                    @else
                        <input name="image_path[]"  type="file" multiple="multiple" />
                        <hr />
                        <div id="dvPreview">
                        </div>
                    @endif

                </div>

            </div>




    In my controller i have completed store like as follows:

      $input = $request->all();

            $destinationPath = 'uploads/foodimages';
            if($input) {

                $input['restaurant_id']=Auth::user()->userRestaurantOwner->restaurant->id;

                $food=Food::create($input);

                $featured_image = str_random(6) . '_' . time() . "-" .
                    $request->file('featured_image')->getClientOriginalName();

                $featured_image_path = $request->file('featured_image')->move($destinationPath,$featured_image);


                FoodImage::create([
                    'food_id'=>$food->id,
                    'featured_image'=>$featured_image_path
                ]);

                if ($files = Input::file('image_path') ) {
                    foreach ($files as $file) {
                        $destinationPath = 'uploads/foodimages';

                        $multiimage = str_random(6) . '_' . time() . "-" .
                                      $file->getClientOriginalName();
                        $imagenames = $file->move($destinationPath, $multiimage);
                        $image = new FoodImage();
                        $image->food_id = $food->id;
                        $image->image_path = $imagenames;


                        $image->save();
                    }

I havenot shown complete form only image form since there are lots of attributes and i am having problem on multiple image and single image update at once.

Can anyone helps me with update code as i have tried lots to solve.
Thanks for help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire