Ad

Redirecting To A Different Route After Submit Form Laravel

I'm trying to redirect my post route to another route after submitting a form. I've been looking in the documentation of Laravel, but everything that I try redirects me to the same page.

This is the controller

ProductReviewController

public function store(Request $request, Product $product)
{   
    auth()->user()->reviews()->create($request->all());

    return redirect()->route('welcome.show', compact('product'));
}

This is the route that I'm trying to redirect.

Web.php

Route::get('/products/{product}', [ 
'uses' => '[email protected]',
'as' => 'welcome.show'
]);

And this is the controller of this route

ProductController

public function showOne(Product $product, ProductReview $productReview)
{
    return view('welcome.show', ['product' => $product]);
}

What am I doing wrong? Because everytime that I try to redirect to this route it's throwing me this error.

"Symfony\Component\HttpKernel\Exception\NotFoundHttpException"

Throwing this new error when I change the controller code for:

New Controller code

public function store(Request $request, Product $product)
{   
auth()->user()->reviews()->create($request->all());

return redirect()->route('welcome.show', $product->id));
}

Error

"Missing required parameters for [Route: welcome.show] [URI: products/{product}]."

These are the views:

Vue Component

    <template>
    <form @submit.prevent="postReview">
            <div class="form-group">
                <p class="mt-4 text-center"><strong>How would you rate it?</strong></p>
                <h4 class=" offset-md-3"><star-rating v-model="formData.rating"></star-rating></h4> 
            </div>
            <hr>
            <div class="form-group">
                <label for="headline"><strong>Review title</strong></label>
                <input type="text" class="form-control" v-model="formData.headline" id="headline" placeholder="Add a title for your review">
            </div>
            <div class="form-group">
                <label for="description"><strong>Description</strong></label>
                <textarea v-model="formData.description" class="w-100 rounded pl-2 pt-2 border border-muted" style="height:100px" id="description" cols="30" rows="10" placeholder="Tell us about your experiences with this product"></textarea>
            </div>

            <button class="btn btn-primary rounded offset-md-4" type="submit">Send review</button>
    </form>
</template>

<script>
 export default {
    props:['product','url', 'user'],
    data(){
        return {
            formData:{}
        }
    },
    methods:{
        postReview(){
            this.formData.product_id=this.product.id;
            this.formData.user_name=this.user.first_name;

            axios.post(this.url,this.formData)
                .then(data=>{
                    location.reload();
                })
                .catch(error=>{
                    console.log(error.response);
                })
        }
    },
}
</script>

Blade view

    @extends('layouts.app')

@section('title')
Review
@endsection

@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-6">
            <div class="col-md-12 product-img">
                <div class="card">
                    <div class="card-image">
                        <div class="carousel-item active">
                        <img class="d-block mx-auto" src="{{ $product->imageUrl }}" alt="First slide">
                        </div>
                    </div>
                </div>
            </div>
            <h5 class="text-center">{{ $product->title }}</h5>
        </div>
        <div class="col-md-6" id="app">
            <review-form
            :user="{{Auth::user()}}"
            :product="{{$product}}"
            url="{{route('review.store')}}">
            </review-form>
        </div>
    </div>
</div>
@endsection
Ad

Answer

I just solved my problem by adding this:

window.location.href = "/products/" + this.formData.product_id

This is my new Vuejs file:

    <template>
    <form @submit.prevent="postReview">
            <div class="form-group">
                <p class="mt-4 text-center"><strong>How would you rate it?</strong></p>
                <h4 class=" offset-md-3"><star-rating v-model="formData.rating"></star-rating></h4> 
            </div>
            <hr>
            <div class="form-group">
                <label for="headline"><strong>Review title</strong></label>
                <input type="text" class="form-control" v-model="formData.headline" id="headline" placeholder="Add a title for your review">
            </div>
            <div class="form-group">
                <label for="description"><strong>Description</strong></label>
                <textarea v-model="formData.description" class="w-100 rounded pl-2 pt-2 border border-muted" style="height:100px" id="description" cols="30" rows="10" placeholder="Tell us about your experiences with this product"></textarea>
            </div>

            <button class="btn btn-primary rounded offset-md-4" type="submit">Send review</button>
    </form>
</template>

<script>
 export default {
    props:['product','url', 'user'],
    data(){
        return {
            formData:{}
        }
    },
    methods:{
        postReview(){
            this.formData.product_id=this.product.id;
            this.formData.user_name=this.user.first_name;

            axios.post(this.url,this.formData)
                .then(data=>{
                    window.location.href = "/products/" + this.formData.product_id;
                })
                .catch(error=>{
                    console.log(error.response);
                })
        }
    },
}
</script>
Ad
source: stackoverflow.com
Ad