Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
提交表单Laravel后重定向到其他路由_Laravel_Laravel 5_Vue.js_Vue Component_Laravel Routing - Fatal编程技术网

提交表单Laravel后重定向到其他路由

提交表单Laravel后重定向到其他路由,laravel,laravel-5,vue.js,vue-component,laravel-routing,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()

在提交表单后,我正在尝试将我的帖子路由重定向到另一个路由。我一直在查找Laravel的文档,但我尝试的所有内容都将我重定向到同一页面

这是控制器

ProductReviewController

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

    return redirect()->route('welcome.show', compact('product'));
}
public function showOne(Product $product, ProductReview $productReview)
{
    return view('welcome.show', ['product' => $product]);
}
"Missing required parameters for [Route: welcome.show] [URI: products/{product}]."
这是我试图改变方向的路线

Web.php

Route::get('/products/{product}', [ 
'uses' => 'ProductController@showOne',
'as' => 'welcome.show'
]);
这是这条路线的控制者

ProductController

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

    return redirect()->route('welcome.show', compact('product'));
}
public function showOne(Product $product, ProductReview $productReview)
{
    return view('welcome.show', ['product' => $product]);
}
"Missing required parameters for [Route: welcome.show] [URI: products/{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));
}
错误

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

    return redirect()->route('welcome.show', compact('product'));
}
public function showOne(Product $product, ProductReview $productReview)
{
    return view('welcome.show', ['product' => $product]);
}
"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>

您如何评价它?


查看标题 说明 发送评论 导出默认值{ 道具:['product','url','user'], 数据(){ 返回{ formData:{} } }, 方法:{ 事后审查(){ this.formData.product\u id=this.product.id; this.formData.user\u name=this.user.first\u name; post(this.url,this.formData) 。然后(数据=>{ location.reload(); }) .catch(错误=>{ console.log(error.response); }) } }, }
刀片视图

    @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
@extends('layouts.app'))
@章节(“标题”)
复习
@端部
@节(“内容”)
imageUrl}“alt=”第一张幻灯片“>
{{$product->title}
@端部

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

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

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

如果route具有参数,则可以将其作为第二个参数传递给route方法:

// 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]);
只需返回路由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>

导出默认值{
道具:['product','url','user'],
数据(){
返回{
formData:{}
}
},
方法:{
事后审查(){
this.formData.product\u id=this.product.id;
this.formData.user\u name=this.user.first\u name;
post(this.url,this.formData)
.然后(功能(响应){
location.href=response;//老实说,我不是vue专家,但我想它应该可以像这样工作。
})
.catch(错误=>{
console.log(error.response);
})
}
},
}

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

return route(“welcome.show”, [“product” => $product->id]);
只需返回路由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>

导出默认值{
道具:['product','url','user'],
数据(){
返回{
formData:{}
}
},
方法:{
事后审查(){
this.formData.product\u id=this.product.id;
this.formData.user\u name=this.user.first\u name;
post(this.url,this.formData)
.然后(功能(响应){
location.href=response;//老实说,我不是vue专家,但我想它应该可以像这样工作。
})
.catch(错误=>{
console.log(error.response);
})
}
},
}

您正在显示相同的路由名称,并在相同的路由名称上重定向。请尝试以下操作:

return redirect('route name');

您正在显示相同的路由名称,并在相同的路由名称上重定向。请尝试以下操作:

return redirect('route name');
试试这个

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

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>

您如何评价它?


查看标题 说明 发送评论 导出默认值{ 道具:['product','url','user'], 数据(){ 返回{ formData:{} } }, 方法:{ 事后审查(){ this.formData.product\u id=this.product.id; this.formData.user\u name=this.user.first\u name; post(this.url,this.formData) 。然后(数据=>{ window.location.href=“/products/”+this.formData.product_id; }) .catch(错误=>{ console.log(error.response); }) } }, }
我刚刚通过添加以下内容解决了我的问题:

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>

您如何评价它?


查看标题 说明 发送评论 导出默认值{ 道具:['product','url','user'], 数据(){ 返回{ formData:{} } }, 方法:{ 事后审查(){ this.formData.product\u id=this.product.id;