Laravel重定向在整个项目中根本不起作用

时间:2019-07-19 06:39:36

标签: laravel laravel-blade

在将记录保存到DB后尝试返回重定向,但失败。通常,只有登录重定向和主页有效。返回视图也可以正常工作。

这是我尝试过的:

return redirect()->back()->with('success',"Product updated successfully");

return redirect('main/products');

return Redirect::to('main/products');

但是,如果我在响应下的浏览器上进行检查,则可以看到返回了视图的HTML代码,只是未显示该视图。 This is the response I get on inspect...no errors

是的,我知道,这看起来很简单,但这让我感到沮丧。由于它影响了我的整个系统,因此我猜测有些东西缺少其语法。

我的完整方法是:

public function updateProducts(){
       request()->validate([
            'name'=>'required',
            'description'=>'required'
        ]);
        $product=Product::findOrFail(\request('id'));
        $product->name=\request('name');
        $product->description=\request('description');
        $product->save();

        return redirect()->back()->with('success',"Product updated successfully");  
    }

我的刀片:

@extends('layouts.dashboard')

@section('page_heading','Products ')

@section('section')
<div class="col-lg-12">
    <hr class="sidebar-divider">
    <div class="col-sm-12">
    <ol class="breadcrumb">
        <div class="col-lg-12">
            <div class="row">
                <div class="col-lg-3">
                    <li class="breadcrumb-item">
                        <a href="{{url('/main/system')}}" class="btn btn-success btn-icon-split">
                            <span class="icon text-white-50">
                                <i class="fas fa-arrow-left"></i>
                            </span>
                            <span class="text">Back</span>
                        </a>
                    </li>
                </div>
                <div class="col-lg-4    "><h4 class="card-title">African Guarantee Fund Products</h4></div>
                <div class="col-lg-5">

                    <a href="#add_product" data-toggle="modal" class="btn btn-success btn-icon-split">
                        <span class="icon text-white-50">
                            <i class="fas fa-plus"></i>
                        </span> <span class="text">New Guarantee Product</span>
                    </a>

                </div>
            </div>
        </div>
    </ol>
    </div>
    <div class="card border-left-success shadow h-100 py-2">
    <div class="card-body">
        @include('common.bootstrap_table_ajax',[
        'table_headers'=>["name","description","action"],
        'data_url'=>'/main/system/products/list',
        'base_tbl'=>'products'
        ])
    </div>
  </div>
  <hr class="sidebar-divider">
  <div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="favoritesModalLabel" id="add_product">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title"
                id="add_product">New Guarantee Product</h4>
                <button type="button" class="btn btn-success btn-icon-split" class="close"
                data-dismiss="modal"
                aria-label="Close">
                <span aria-hidden="true">&times;</span></button>
            </div>
             <div class="modal-body">
                    {!! Form::autoForm(['product_name','description'],"main/system/products/add") !!}
            </div>
        </div>
    </div>
</div>

<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="favoritesModalLabel" id="product_modal">
        <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title"
                id="product_modal">New Guarantee Product</h4>
                <button type="button" class="btn btn-success btn-icon-split" class="close"
                data-dismiss="modal"
                aria-label="Close">
                <span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body">
                 {!!Form::autoForm(\App\Models\Main\Product::class,"main/system/products/update")!!}

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


    @endsection

3 个答案:

答案 0 :(得分:1)

只需使用以下代码进行检查

return redirect()->to('main/products');

             OR

return redirect()->route('main_products'); // where main_products is the route name mentioned in the web.php

答案 1 :(得分:0)

您可以使用此选项

public function updateProducts(){
        request()->validate([
            'name'=>'required',
            'description'=>'required'
        ]);
        $product=Product::findOrFail(\request('id'));
        $product->name=\request('name');
        $product->description=\request('description');
        $product->save();
        Session::flash('success',"Product updated successfully");
        return redirect()->back();  
    }

并使用Blade文件中的变量作为

@if(Session::has('success'))
    <p>{{ Session::get('success') }}</p>
@endif

别忘了使用此

use Session;

答案 2 :(得分:0)

好消息:我已解决问题。坏消息:这是一个愚蠢的错误。

我在资源刀片的末尾放置了HTML代码行,以测试是否通过控制台记录值来加载所有资源。这是我添加的行:

    <input type="hidden" name="material_page_loaded" value="1">
</body>
</html>

对不起,我为此花了时间。感谢您的帮助。