提交表格Laravel后重定向到不同的路线

时间:2018-05-30 01:27:13

标签: laravel laravel-5 vue.js vue-component laravel-routing

我正在尝试在提交表单后将我的邮寄路线重定向到另一条路线。我一直在查看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'
]);

这是这条路线的控制者

ProductController的

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>

刀片视图

    @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

6 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

我不确定,但我猜$ product是一个模型对象或其他类型的对象我猜。在这种情况下,如果您的路线需要$ product-&gt; slug或$ product-&gt; Id,那么

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

只需返回路径网址,然后更改您的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>

答案 2 :(得分:0)

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

return redirect('route name');

答案 3 :(得分:0)

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

答案 4 :(得分:0)

试试这个

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

答案 5 :(得分:0)

我刚刚解决了我的问题:

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>