Laravel 5. Translate errors from forms - php

I want to translate form errors to other language.
I created language folder: resources/lang/lt and insinde validation.php file with translated text.
In template show errors like this:
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
But every time I get errors from resources/lang/en/validation.php file, maybe I have somehow use trans function insinde errors loop?
For translation I use mcamara/laravel-localization package. Everything works except errors.

Related

Noob Q: Laravel 5.4 - redirect()->with()

I'm currently learning Laravel 5.4. I'm following a beginner's tutorial (we're on the same Laravel version).
In the video tutorial, the guy uses the following line of code in a controller:
return redirect()->route('posts.index')->with('error','Unauthorised!');
We both have the following in the view:
#if(count($errors))
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{ $error }}
</div>
#endforeach
#endif
The code works perfectly fine for the tutor on screen, however it doesn't for me - it redirects but doesn't pass the errors.
I used the following modified code in my controller and it worked:
return redirect()->route('posts.index')->withErrors(['error'=>'Unauthorised!']);
In order for me to learn, I need to know why the original code works for him - but not me? Like I said previously, we're both using the same version of Laravel.
Can anyone explain why?
I haven't seen the video, so can't say to as why his does work.
1.
return redirect()->route('posts.index')->with('error','Unauthorised!');
You redirect to the posts.index with a variable called error, and you check for a variable called errors so you could do it like this instead
#if(count($error))
<div class="alert alert-danger">
{{ $error }}
</div>
#endif
2.
You could support multiple errors like you other example and you could do it like
return redirect()->route('posts.index')->withErrors(['Unauthorised!', 'error_2', 'error_3', 'etc']);
and then you can loop through them as in your own first example (You don't need the keys in the array, but you can have them if you feel for it)
#if(count($errors))
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{ $error }}
</div>
#endforeach
#endif
3.
A third option would also be to use flash sessions, which is a session that only lives for the next request
than you could do
return redirect()->route('posts.index')->session()->flash('error', 'Unauthorised!');
and in your views
#if(Session::has('error'))
<div class="alert alert-danger">
{{ Session::get('error') }}
</div>
#endif
which I personally prefer as it gives me the option to just include the if statement in my "main layout file", and it will be shown on all the including pages.

Laravel: Place for controller echo in blade view

First Laravel Project.
How to define a place in the view for the echo in controller?
I have sometimes "echoes" in my controller. For example if a MySQL statement has null output it echoes "MySQL select is empty" or after succesfull fileupload: "File upload is succesfull".
Right now it appears outside of the body tag at the top left corner of the screen. I want to place it inside the main section or a modall.
You want to redirect back with errors
return redirect()->back()->withErrors(['mysql' => 'MySQL select is empty']);
And then get it in the view (modal)
#if($errors->any())
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
#endif
You can watch this laracast for more about errors: https://laracasts.com/series/laravel-from-scratch-2017/episodes/12

Laravel 5 echo out session variable containing html in blade

I did a redirect in laravel:
return redirect('admin')->with($returnData);
$returnData is a string that contains a bootstrap info div with the result from the controller. Almost everything is working except when the page loads again it shows the html on the page as if it were text, brackets and everything. If I use this:
#if(!empty(Session::get('error'))) {{ Session::get('error')}} #endif
Then it shows is as pure text. If I change it to
<?php if(!empty(Session::get('error'))) { echo Session::get('error'); } ?>
It works fine. Im ok keeping it like this but I would rather utilize Blade / Laravel as its supposed to be used so I was wondering if there is a way to have the #if statement show the rendered html and not the text version?
I would recommend returning just the error message then in your view create the div. So if you were to change the layout of the view, you would it in one place.
#if(Session::has('error'))
<div class="alert alert-danger">
{{ Session::get('error')}}
</div>
#endif
hope this help.
To show the rendered HTML you should use {!! $variable->coontent !!} in your view, and this gonna convert your HTML text to render
May this example will help you.
Try this
#if (session('Error'))
<div class="alert alert-success">
{{ session('Error') }}
</div>
#endif
If you want to display plain text from error without any HTML entities you can simply use:
{{ Session::get('error') }}
or
{{ session('error') }}
If you have HTML entities in your variable then use:
{!! Session::get('error') !!}
Try changing your blade code to following.
#if(!empty(Session::get('error')))
{!! Session::get('error') !!}
#endif

Showing error "Files should not contain characters before "<?php" in sonarQube for laravel blade template files

I'm developing an application using Laravel framework. When I check coding standard via sonarQube quality analysis tool, it showing error like "Files should not contain characters before "
I'm using code to include header files via "#section('header')". How can we fix the issue? After extends it shows error.
#extends('layouts.user')
#section('content')
<ul class="breadcrumb">
<li>{{trans('user.home')}}</li>
<li class="divider"></li>
<li>{{ trans("user.heading") }}</li>
</ul>

How do I keep my indentation for the blade syntax in PHPStorm?

I am copying the following code from a *.blade.php file:
#section('content')
<h1>All Users</h1>
#if ($users->isEmpty())
<h2>No Users Found</h2>
#else
#foreach ($users as $user)
<li>{{ link_to("/users/{$user->username}", $user->username) }}</li>
#endforeach
#endif
#stop
This is how it appears when it is pasted into the same file:
#section('content')
<h1>All Users</h1>
#if ($users->isEmpty())
<h2>No Users Found</h2>
#else
#foreach ($users as $user)
<li>{{ link_to("/users/{$user->username}", $user->username) }}</li>
#endforeach
#endif
#stop
I am assuming there is a code style somewhere that is removing the indents for the blade syntax, but I can't find which one. What setting do I need to change?
Generally speaking:
Settings (Preferences on Mac) | Editor | Smart Keys | Reformat on Paste
Why "generally" -- because there is no proper Blade support available in PhpStorm yet (but coming soon) and therefore no separate formatter rules for it. Because of that the aforementioned option most likely be the one responsible for such behaviour.

Categories