samedi 12 décembre 2020

update review with Modal get 404 not found

when I click update btn to update a review it works but I can update just one review every time if I click another review to update it show me just the same information for the review I updated(change just one review :one id ) but now my problem is when I click update review with Modal it show me 404 not found and the review not updated like the first time .I don't know whats the problem and how solve it (for the 2 problems) and thank you very much

this is how to edit and update review

reviewcontroller:

 public function edit(Request $request, Review $review)
    {
       
       
        $review=Review::where('user_id',$request->user()->id)->findOrFail($review->id);
   
        return view('review.edit', compact('review'));
        
    }
     /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Product  $product
     * @param  \App\Review  $review
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Review $review, Product $product)
    {

        //$review=Review::where('user_id',$request->user()->id)->findOrFail($request->id);
        $this->validate($request, [
            'description' => 'required|string',
            'rating' => 'required|numeric|min:0|max:5',
        ]);

        $review->rating = $request->rating;
        $review->description= $request->description;
        $product = $review->product;
        $review->save();
        $product->recalculateRating();

        return back()->with(['message' => 'Review Updated', 'review' => $review]);
    }

route:

Route::get('/{id}/edit', 'Site\ReviewController@edit')->name('review.edit');
Route::post('/update/{review}', 'Site\ReviewController@update')->name('review.update');

reviews.blade.php:


@foreach ($reviews as $review)


<hr>
                <div class="row">
                  <div class="col-md-12">
                    @for ($i=1; $i <= 5 ; $i++)
                      <span class="fas fa-star" style="color:orange;"></span>
                    @endfor
                    
                    <a href="" data-toggle="modal" data-target="#myModal" class="btn btn-sm btn-primary"><i class="fa fa-edit"></i></a>
                   
                    @if (auth()->check() && auth()->user()->id == $review->user_id)
                    <a href="" class="btn btn-sm btn-danger"><i class="fa fa-trash"></i></a>
                      @endif           
                     <span class="pull-right"></span> 
                    
                    @for ($i=0; $i < 5 ; $i++)
                   
                   @if($review->rating - $i>= 1)
   
                     <span class="fas fa-star " style="color:orange;" ></span>
                   @elseif($review->rating - $i>0)
                     <span class="fas fa-star-half" style="color:orange;" ></span>
                   @else
                   <span class="far fa-star " ></span>
                   @endif
                   @endfor
                    <p>}</p>
                  </div>
                </div>
               
                                @endforeach
              

product.blade.php:


 @include('site.pages.modal', ['reviews' => $product->reviews, 'product_id' => $product->id])


   

modal.blade.php:

@foreach ($reviews as $review)

<div class="container">
    

      <!-- Modal -->
      <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">

          <!-- Modal content-->
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>

            </div>
            <div class="modal-body">
                <div class="review-edit">
                    <div class="review-edit-block">
                        

                        <form action="" method="POST">
                        @csrf
   
        Your rating:
        <br />
        <select name="rating">
            <option>1</option>
            <option>2</option>
            <option>3</option>
            <option >4</option>
            <option>5</option>
        </select>
        <br /><br />
        <div class="col-sm-12">
                    @if (Session::has('error'))
                        <p class="alert alert-danger"></p>
                    @endif
                </div>
                          <div class="form-group">
                            <textarea style="position:relative;left:10%;width:375px;" name="description" class="form-control"></textarea>
                            @error('description')
                                    <span class="invalid-feedback" role="alert">
                                        <strong></strong>
                                    </span>
                                    @enderror
                          </div>
                          <div>
                            <button type="submit" class="btn btn-primary">Update Review</button>
                          </div>


                        </form>
                    </div> 
                    </div>
                    </div>

            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
          </div>

        </div>
      </div>               
                    @endforeach
     

     


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire