Laravel 5 Pass and get a flash data not working

送分小仙女□ 提交于 2019-12-12 03:55:17

问题


i want to pass a flash data from store function to add function but i cant get it to work, i always get null;

here is my controller

public function add()
{
    return view('cars.add');
}

public function store(CarFormRequest $request)
{
    $car = new Cars(array(
        'name' => $request->get('name'),
        'color_id' => $request->get('color')
    ));

    $car->save();
    $car->position_id = $car->id;
    $car->save();

    return redirect('/cars/add')->with('status', 'A Car has been added');
}

here is my view:

@if (session('status'))
    <div class="alert alert-success">
        {{ session('status') }}
    </div>
@endif

回答1:


Add in your controller

session()->flash('status', 'Task was successful!');

and in View:

@if (session->has('status'))
    <div class="alert alert-success">
        {{ session('status') }}
    </div>
@endif


来源:https://stackoverflow.com/questions/35567608/laravel-5-pass-and-get-a-flash-data-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!