简体   繁体   English

格式化消息Laravel Collective

[英]Formatting a message Laravel Collective

Laravel 5.2 Laravel 5.2

Hello I have this message in my controller: 您好,我的控制器中显示以下消息:

public function postIndex(SaveProfileRequest $request) {
    $profile = Profile::create([
        'name' => $request->input('name')
        ]);

    if($profile->id) {
        return redirect()->back()->with('message', [
            'type'=> 'success',
            'message' => 'Успешно записан нов профил!'

        ]);
    }

    return redirect()->back()->with('message', [
        'type'=> 'danger',
        'message' => 'Профилът не е записан!'
    ]);
}

I'm displaying it like this: 我正在这样显示它:

@if(Session::has('message'))
    {{ Session::get('message.message')}}
@endif

But it doesn't show in the type i have selected in the controller, how can I format it? 但是它没有以我在控制器中选择的类型显示,我该如何格式化?

Thank you! 谢谢!

You need to change 'message' to 'status' so your code should look like this. 您需要将“消息”更改为“状态”,以便您的代码应如下所示。

public function postIndex(SaveProfileRequest $request) {

    $profile = Profile::create(['name' => $request->input('name')]);

     if($profile->id) {
        return redirect()->back()->with(['message' => 'Успешно записан нов профил!', 'type' => 'success']);
     }

     return redirect()->back()->with(['message', => 'Профилът не е записан!', 'type' => 'danger']);
}

Then you can access your message from the page like this: 然后,您可以从以下页面访问您的消息:

@if (session('status'))
     {{ session('message') . "<br>" }}
     {{ session('type') }}

@endif

Another option for you is to change the "with" with withInput. 您的另一个选择是使用withInput更改“ with”。

public function postIndex(SaveProfileRequest $request) {
    $profile = Profile::create([
        'name' => $request->input('name')
       ]);

   if($profile->id) {
       return redirect()->back()->withInput(['message' => [
           'type'=> 'success',
            'message' => 'Успешно записан нов профил!'
        ]]);
    }

   return redirect()->back()->withInput(['message' => [
       'type'=> 'danger',
       'message' => 'Профилът не е записан!'
   ]]);
}

Then you can access them in your view. 然后,您可以在视图中访问它们。

{{ $message->$message }}
{{ $message->type }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM