For some reason, I can't access data from an error bag.
I've assigned them to variable in view but I can't display them.
#php
$register_errors = $errors->getBag('register');
echo($register_errors);
#endphp
#foreach($register_errors as $error)
<li>{{ $error }}</li>
#endforeach
echo prints this: {"email":["Email mus\u00ed ma\u0165 spr\u00e1vny form\u00e1t!"],"password":["Heslo mus\u00ed ma\u0165 aspo\u0148 8 charakterov!"],"password_confirmation":["Heslo mus\u00ed ma\u0165 aspo\u0148 8 charakterov!","Hesl\u00e1 sa musia zhodova\u0165!"],"psc":["Pole psc mus\u00ed by\u0165 \u010d\u00edslo."]}
foreach displays nothing,
if I try to access them like this: $register_errors->email / $register_errors->email[0]. I get no results
I'm sorry for such a newbie(stupid) question, please bear with me.
Laravel has a special format to display them. Try :
{{ $errors->first('email', '<div class="some_name">:message</div>') }}
FOR RENAMED BAG
If you renamed the bag like :
return redirect('register')
->withErrors($validator, 'register_errors');
You may use :
{{ $errors->register_errors->first ('email') }}
TO GET ALL ERROR MESSAGES
#foreach ($errors->all() as $message) {
<div>{{ $message }}</div>
#endforeach
Related
I have a problem with my view of laravel. I have a field that executes a loop, and if it has data in the field it list, but if it does not it only shows me nothing.
I used #forelse but it does not work, any help?
with $prices I can check whether or not it has the value, but in that fieldinfos_home does not.
I used #forelse but it does not work, any help?
<em>{{ $prices->price ?? ' - ' }}</em> <br>
#forelse($prices->infos_home as $info)
<em>{{ $info }}</em> <br>
#empty
<em> - </em>
#endforelse
When I use #forelse I have the following error message.
message: "Method Illuminate\View\View::__toString() must not throw an exception, caught ErrorException: Trying to get property 'infos_home' of non-object (View: C:\wamp64\www\suzuki-cms-backoffice\resources\views\brand\motorcycle\variations\index\column-price.blade.php)"
KISS and just use #if / #else:
<em>{{ $prices->price ?? ' - ' }}</em> <br>
#if($prices && $prices->infos_home)
#foreach($prices->infos_home as $info)
<em>{{ $info }}</em> <br>
#endforeach
#else
<em> - </em>
#endif
#forelse is good when you are sure that variable exists.
I don't know how to show comments and replycomments in my post page. My comments table contains id, sender_id, replyer_id, reply_id, comment_text.
CommentController returns a comment object. In Post.blade.php, how do I write a foreach loop or loops?
Your Controller code must be similar to this:
public function index()
{
$comments = Comment::with(['sender', 'other-relation'])->get();
return view('comments.index', compact('comments'));
}
and your blade code must be similar to this:
<ul>
#foreach($comments as $comment)
<li>{{ $comment->comment_text }}</li>
#if ($comment->sender) // or other relation
<a> {{$comment->sender->name}}<a> // relation name and column name must be fix yourself
#endif
#endforeach
</ul>
Inside blade file:
<ul>
#foreach($comments as $comment)
<li>{{ $comment->comment_text }}</li>
#endforeach
</ul>
I want display tables in a blade view that should be like this:
Table Image
dynamically ...
When i add another User and attach a category it should display more tables.
I have 3 Tables in my Database:
User, user_category and category
I defined the realtionship in my model files.
A User can have many Categories.
I tried it with PHP variables and a #while blade expression.
#php
$i=0;
#endphp
#foreach($users as $user)
<!-- Display Table Header -->
#while($Category[$i]->user_id == $user->id)
<!-- Display Table Rows -->
#php
$i++;
#endphp
#endwhile
#endforeach
This gave me an error: Undefined Offset: 6.
I have currently 6 rows in my Table. I can display the content when i 'hardcode' the index, like this for example: $Category[5].
The Join Query in my Controller works. I sorted it ascendent by User ID
How do i solve this problem? I'm a beginner and currently learning laravel.
Yes, i googled my problem before. Sorry for my english
If I understand your question correctly, maybe this could help you. It's just an example but you get the point.
<ul>
#foreach ($users as $user)
#foreach ($user->categories as $category)
<li>{{ $category->property_1 }}</li>
<li>{{ $category->property_2 }}</li>
<li>{{ $category->property_3 }}</li>
#endforeach
#endforeach
</ul>
I am passing the array $cats to my laravel template view. It is a multidimensional array from a database transaction, containing category data. So it would contain data like:
$cat[0]['id'] = 1;
$cat[0]['name'] = 'First Category';
And so on. In my blade template I have the following code:
{{ $i=0 }}
#foreach($cats as $cat)
{{ $cat['name'] }}<br />
{{ $i++ }}
#endforeach
Which outputs:
0 First Category
1 Second Category
2 Third Category
Notice the numbers preceding the category name. Where are they coming from? Is this some clever Laravel trick? It seems that when you include a counter variable, they are automatically added. I can't find any mention of it anywhere, and I don't want them! How do I get rid of them?
Thanks.
You just need to use the plain php translation:
#foreach ($collection as $index => $element)
{{$index}} - {{$element['name']}}
#endforeach
EDIT:
Note the $index will start from 0, So it should be {{ $index+1 }}
The {{ }} syntax in blade essentially means echo. You are echoing out $i++ in each iteration of your loop. if you dont want this value to echo you should instead wrap in php tags. e.g.:
<?php $i=0 ?>
#foreach($cats as $cat)
{{ $cat['name'] }}<br />
<?php $i++ ?>
#endforeach
As an additional note, if you choose to work in arrays then thats your call but unless you have a specific reason to do so I would encourage you to work with object syntax, eloquent collection objects in laravel can be iterated over just like arrays but give you a whole lot of extra sugar once you get used to it.
#foreach($cats as $cat)
{{ (isset($i))?$i++:($i = 0) }} - {{$cat['name']}}
#endforeach
<? php $i = 0 ?>
#foreach ( $variable_name as $value )
{{ $ value }}<br />
< ? php $i++ ?>
#endforeach
if your $k is integer you can use {{ $k+1 }} or isn't integer you can use $loop->iteration
// for laravel version 4 and after
#foreach ($posts as $k => $post)
{{ $loop->iteration }}. {{ $post->name }}
#endforeach
You can actually use a built in helper for this: {{ $cat->incrementing }}.
I'm trying to get my flash message to display.
This is in my routing file
Route::post('users/groups/save', function(){
return Redirect::to('users/groups')->withInput()->with('success', 'Group Created Successfully.');
});
This is in my view
{{ $success = Session::get('success') }}
#if($success)
<div class="alert-box success">
<h2>{{ $success }}</h2>
</div>
#endif
But nothing is working.
When I try this, I get an error Variable $success is undefined. But it actually shows the flash message too.
{{ Session::get('success') }}
#if($success)
<div class="alert-box success">
<h2>{{ $success }}</h2>
</div>
#endif
This works for me
#if(Session::has('success'))
<div class="alert-box success">
<h2>{{ Session::get('success') }}</h2>
</div>
#endif
if you are using bootstrap-3 try the script below for Alert Style
#if(Session::has('success'))
<div class="alert alert-success">
<h2>{{ Session::get('success') }}</h2>
</div>
#endif
when you set variable or message using ->with() it doesn't set the variable/message in the session flash, rather it creates an variable which is available in your view, so in your case just use $success instead of Session::get('success')
And in case you want to set the message in the session flash the use this Session::flash('key', 'value'); but remember with session flash the data is available only for next request.
Otherwise you can use Session::put('key', 'value'); to store in session
for more info read here
two methods:
Method 1 - if you're using
return Redirect::to('users/groups')->withInput()->with('success', 'Group Created Successfully.');
under your controller create(), add in
$success = Session::get('success');
return View::make('viewfile')->with('success', $success);
then on view page,
#if (isset($success))
{{$success }}
#endif
What's happening in method 1 is that you're creating a variable $success that's passed into your create(), but it has no way of display $success. isset will always fail unless you set a variable to get the message and return it.
Method 2 - use return Redirect withFlashMessage
return Redirect::route('users/groups')->withFlashMessage('Group Created Successfully.');
then on your view page,
#if (Session::has('flash_message'))
{{ Session::get('flash_message') }}
#endif
Method 2 is much cleaner and does not require additional code under create().
{{ Session::get('success') }}
This just echos the session variable 'success'. So when you use
{{ Session::get('success') }}
#if($success)
<div class="alert-box success">
<h2>{{ $success }}</h2>
</div>
#endif
you are seeing it's output along with the error of the next statement. Because with() function only sets the value in Session and will not set as a variable. Hence #if($success) will result in undefined variable error.
As #Andreyco said,
#if(Session::has('success'))
<div class="alert-box success">
<h2>{{ Session::get('success') }}</h2>
</div>
#endif
This should work.
The reason you are not seeing it might be because the action you are performing is not success. And this does not require you to either reinstall xampp or modify php.ini.
Laravel 4.2
Personally i use
Session::flash('key', 'value');
return Redirect::to('some/url');
then in the view id first check if there is a session of that key in the view
#if(Session::has('key'))
{{Session::get('key')}} //this prints out the message or your 'value' in the session::flash method
#endif
it works for me most of the time and i usually have that blade template integrated into my view just so i can push success messages to the view from my codes.
please do note that it is stated in the documentation that "Sometimes you may wish to store items in the session only for the next request. You may do so using the Session::flash method" so yes it expires after the next page.
hope this helps
i just realized in using the Redirect::to(), when you use the withInput() method, chaining a with() function to pass variables will not work. the only way is either you flash your inputs separately using Input::flash(), and use the with() to pass your variables or you pass your variable via session using Session::flash('key','val') and retrieve in the view via session::get('key').
This link describes how to do this http://vegibit.com/flash-messages-in-laravel/
Just tried with laravel 5 - works to me.
Inside of the routes.php file try to create your routes within the
Route::group(['middleware' => ['web']], function () {
//routes here
}
then use
#if(Session::has('success'))
<div class="alert-box success">
<h2>{{ Session::get('success') }}</h2>
</div>
#endif
I fixed mine by changing the session driver in config/session.php from array to file !