在Laravel 10中,在Blade中添加成功消息。

huangapple go评论91阅读模式
英文:

Add successful message in blade with Laravel 10

问题

我在控制器中添加了一个成功消息,但在将数据插入数据库后,成功消息未显示。希望专家能帮助。

这是我的HTML代码:

<form action="{{url('/')}}/register" method="post">
  @csrf
  <div class="fields">
    <div class="input-field">
      <label for="fn" id="Fn">名字</label>
      <div class="inpt">
        <input type="text" id="fn" name="First_Name" placeholder="输入您的名字" />
      </div>
    </div>
  </div>
  <div class="input-field">
    <label for="ln" id="Ln">姓氏</label>
    <div class="inpt">
      <input type="text" id="ln" name="Last_Name" placeholder="输入您的姓氏" />
    </div>
  </div>
  <div class="input-field">
    <label for="email">电子邮件</label>
    <div class="inpt">
      <input type="text" id="email" name="email" placeholder="输入您的电子邮件" required />
    </div>
  </div>
</form>

这是我的控制器:

Registrationform::create($request->all());

return redirect()->route('register')->withSuccess('您已成功注册!');

这是我的路由:

Route::get('/register', [Registrationcontroller::class,'register'])->name('register'); 
Route::post('/register', [Registrationcontroller::class,'store']);
英文:

I add a with-success message in the controller but after inserting data in the database, the successful message not showing. Hoping an expert help.

This is my HTML code:

<form action="{{url('/')}}/register" method="post">
  @csrf
  <div class="fields">
    <div class="input-field">
      <label for="fn" id="Fn">First Name</label>
      <div class="inpt">
        <input type="text" id="fn" name="First_Name" placeholder="Enter Your First Name" />
      </div>
    </div>
  </div>
  <div class="input-field">
    <label for="ln" id="Ln">Last Name</label>
    <div class="inpt">
      <input type="text" id="ln" name="Last_Name" placeholder="Enter Your Last Name" />
    </div>
  </div>
  <div class="input-field">
    <label for="email">E-mail</label>
    <div class="inpt">
      <input type="text" id="email" name="email" placeholder="Enter Your Email" required />
    </div>
  </div>
</form>

This is my controller:

Registrationform::create($request->all());

return redirect()->route('register')->withSuccess('You have registered successfully!');

This is my route:

Route::get('/register', [Registrationcontroller::class,'register'])->name('register'); 
Route::post('/register', [Registrationcontroller::class,'store']);

答案1

得分: 3

You're doing Laravel's flash messaging correctly, but you missed the display part. In the end of the form add this:

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

Check this example

Edit 01

For Laravel 10:

@if ($message = Session::get('success'))

@endif
英文:

You're doing Laravel's flash messaging correctly, but you missed the display part. In end of the form add this

@if (session(&#39;success&#39;))
    &lt;div class=&quot;alert alert-success&quot;&gt;
        {{ session(&#39;success&#39;) }}
    &lt;/div&gt;
@endif

Check this example


Edit 01

For laravel 10

@if ($message = Session::get(&#39;success&#39;))

@endif

huangapple
  • 本文由 发表于 2023年7月13日 15:51:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677082.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定