Using @if(Session::has('message')) in Laravel is invalid, and the success message does not pop up.

WBOY
Release: 2023-03-03 08:16:01
Original
1498 people have browsed it

Controller

<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>
Copy after login
Copy after login

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">&times;</span>
    </button>
    <strong>成功!</strong>{{Session::get('success')}}
</div>
@endif</code>
Copy after login
Copy after login

Reply content:

Controller

<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>
Copy after login
Copy after login

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">&times;</span>
    </button>
    <strong>成功!</strong>{{Session::get('success')}}
</div>
@endif</code>
Copy after login
Copy after login

Is it the middleware that has not enabled session?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template