提交表单后重定向到不同的路径 Laravel

Redirecting to a different route after submit form Laravel

我试图在提交表单后将我的 post 路由重定向到另一条路由。我一直在查看 Laravel 的文档,但我尝试的所有操作都将我重定向到同一页面。

这是控制器

ProductReviewController

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

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

这是我要重定向的路线。

Web.php

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

这是这条路线的管制员

产品控制器

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

我做错了什么?因为每次我尝试重定向到这条路线时都会抛出这个错误。

"Symfony\Component\HttpKernel\Exception\NotFoundHttpException"

当我更改控制器代码时抛出这个新错误:

新控制器代码

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

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

错误

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

这些是观点:

Vue 组件

    <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 查看

    @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

如果你的路由有参数,你可以将它们作为第二个参数传递给路由方法:

// For a route with the following URI: /products/{product}

    return redirect()->route('welcome.show', ['product' => $product]);

我不确定,但我猜 $product 是模型对象或其他类型的对象。在这种情况下,如果您的路线期望 $product->slug 或 $product->Id 那么

return route(“welcome.show”, [“product” => $product->id]);

只是 return 路由 url 然后像下面这样更改你的 Vue 组件脚本:

<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(function(response){
                   location.href = response;// Honestly i am not expert in vue but i would guess it should work something like this.

            })
            .catch(error=>{
                console.log(error.response);
            })
        }
    },
}
</script>

您正在显示相同的路由名称,并且还在相同的路由名称上重定向。像这样尝试:

return redirect('route name');
In your js section you have added the code :
 axios.post(this.url,this.formData)
                .then(data=>{
                    location.reload();
                })
                .catch(error=>{
                    console.log(error.response);
                })

here you are reloading the page..
write the redirection code at place of location.reload except in controller.
I hope it will work

试试这个

return redirect()->action('ProductController@showOne', ['product' => $product]);

我刚刚通过添加这个解决了我的问题:

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

这是我的新 Vuejs 文件:

    <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>