【发布时间】:2022-07-08 02:24:58
【问题描述】:
Laravel 文档:
有时您可能希望将项目存储在会话中以供下一个请求使用。您可以使用 flash 方法进行操作。
$request->session()->flash('status', 'Task was successful!');
我的代码:
public function store(StorePost $request)
{
$validated = $request->validate();
$post = new Posts();
$post->title = $validated['title'];
$post->content = $validated['content'];
$post->save();
$request->session()->flash('status', 'Task was successful!');
return redirect()->route('posts.show', [$post->id]);
}
我的 IDE vscode 抛出错误如下所示: error in flash
对这个错误有什么帮助?
【问题讨论】:
-
别忘了添加使用Session;到您的控制器。或使用 Illuminate\Support\Facades\Session;
-
我添加了它'但仍然无法正常工作
-
不要认为它使用 Session 类
-
你可以使用会话助手:
session()->flash('status', 'Task was successful!')或者你可以return redirect()->route('posts.show', $post)->with('status', 'Task was successful!');