LoginSignup
0
1

More than 5 years have passed since last update.

flashメッセージの色分けをする

Posted at

下記のようなフラッシュメッセージを色分けしたい。

Userscontroller.php

public function destroy($id)
{
  $user = User::find($id);
  $user->delete();
  return redirect('/users')->with('status' => '削除しました');
}

->with()は配列も処理できるようになっているみたいなので

RedirectResponse.php
public function with($key, $value = null)
{
  $key = is_array($key) ? $key : [$key => $value];

  foreach ($key as $k => $v) {
    $this->session->flash($k, $v);
  }

  return $this;
}

配列にして渡してあげる

Userscontroller.php
public function destroy($id)
{
  $user = User::find($id);
  $user->delete();
  return redirect('/users')->with([
    'status' => '削除しました',
    'color' => 'danger'
  ]);
}

表示

index.blade.php
@if (session('status'))
  <div class="alert alert-{{ session('color') }}">
    {{ session('status') }}
  </div>
@endif
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1