首页 >后端开发 >php教程 >Laravel中用@if(Session::has('message'))无效,成功提示消息弹不出来

Laravel中用@if(Session::has('message'))无效,成功提示消息弹不出来

WBOY
WBOY原创
2016-12-05 13:44:081526浏览

控制器

<code><?php namespace App\Http\Controllers;
use App\Student;
use Illuminate\Http\Request;


class StudentController extends Controller
{
    //学生列表页
    public function index()
    {
        //$students = Student::get();
        $student = Student::paginate(7);
        return view('student.index',[
            'students' => $student,
        ]);
    }
//    添加页面
    public function create(Request $request)
    {
        if($request->isMethod('POST')){
            $data = $request->input('Student');
            if(Student::create($data)){
                **return redirect('student/index')->with('success','成功添加');**
            }else{
                return redirect()->back();
            }
        }
        //session暂存数据

        return view('student.create');
    }

}
</code>

View

<code><!-- 成功提示框 -->
@if (Session::has('success'))
<div class="alert alert-success alert-dismissible" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">×</span>
    </button>
    <strong>成功!</strong>{{Session::get('success')}}
</div>
@endif</code>

回复内容:

控制器

<code><?php namespace App\Http\Controllers;
use App\Student;
use Illuminate\Http\Request;


class StudentController extends Controller
{
    //学生列表页
    public function index()
    {
        //$students = Student::get();
        $student = Student::paginate(7);
        return view('student.index',[
            'students' => $student,
        ]);
    }
//    添加页面
    public function create(Request $request)
    {
        if($request->isMethod('POST')){
            $data = $request->input('Student');
            if(Student::create($data)){
                **return redirect('student/index')->with('success','成功添加');**
            }else{
                return redirect()->back();
            }
        }
        //session暂存数据

        return view('student.create');
    }

}
</code>

View

<code><!-- 成功提示框 -->
@if (Session::has('success'))
<div class="alert alert-success alert-dismissible" role="alert">
    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">×</span>
    </button>
    <strong>成功!</strong>{{Session::get('success')}}
</div>
@endif</code>

是不是中间件,没启用session

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn