Displaying a Table, Undefined Offset Error - laravel

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>

Related

Undefined variable: categories in user.blade.php

I was trying to extend my user.blade.php to my views menu.blade.php. Everything works fine with my other views that use the same user.blade.php too. But not with my menu.blade.php, I get an error saying "Undefined variable: categories (View: D:\xampp\htdocs\mieaceh\resources\views\layouts\user.blade.php)" with "Possible typo $categories
Did you mean $errors?"
Here are the codes to my user.blade.php
#foreach($categories as $category)
<a href="{{ route('menu.index', ['category' => $category->slug]) }}">
<div class="card-category" style="width: 10rem; height: 4rem;">
{{ $category->name }}
</div>
</a>
#endforeach
How do I solve it?
If you want to make a piece of view that appears in multiple places, you can use blade components https://laravel.com/docs/8.x/blade#components.
This will help encapsulating this partials behavior and required data.

modulus with remainder laravel

I am trying to setup columns of 4 from a laravel array which works correctly. The problem is how do you handle remainders? With my loop I end up with 2 extra divs that are empty at the end of my output. I have 10 items that i'm looping over which would give me a remainder of 2.5 Here is my code.
<div class="serviceListingColumn">
<ul>
#foreach($serviceListings as $serviceListing)
#if ($serviceListing->service_category == $services->title)
<li class="serviceListingItem">{{$serviceListing->service_name}}</li>
#endif
#if($loop->iteration % 4 === 0)
</ul>
</div>
<div class="serviceListingColumn">
<ul>
#endif
#endforeach
</ul>
I would suggest chunking the original array into groups, then just looping those. Easier for the template logic.
// Use the Collection's group() functionality in your controller.
// Use collect() if it isn't a Collection already.
$array = collect($array)->chunk(4);
// Your template then doesn't need to worry about modulus,
// and can focus on displaying the chunked groups.
#foreach ($array as $group)
<div class="serviceListingColumn">
<ul>
#foreach ($group as $serviceListing)
<li class="serviceListingItem">{{ $serviceListing->service_name }}</li>
#endforeach
</ul>
</div>
#endforeach

Laravel show all links in paginator

Since Laravel 5.7, the paginator links() method shows only a reduced number of pages when called. The number of links can be customized using the onEachSide($count) method.
‹‹ ‹ 1 2 ... 47 48 49 50 51 52 53 ... 102 103 › ››
I want to display every page in order to make a page selector dropdown, using a html <select>.
Calling onEachSide(9999) does work, but it looks flimsy, and may break on high page counts that the app i'm writing could reach.
What would be the clean way to get all the page links ?
EDIT: I am using a custom view, which looks like the following :
#if ($paginator->hasPages())
<ul class="pagination">
{{-- Previous Page Link --}}
#if ($paginator->onFirstPage())
<li class="disabled">‹‹</li>
<li class="disabled">‹</li>
#else
<li>‹‹</li>
<li>‹</li>
#endif
{{--Pagination Elements --}}
#foreach ($elements as $element)
#if (is_string($element))
<li class="disabled"><span>{{ $element }}</span></li>
#endif
#if (is_array($element))
#foreach ($element as $page => $url)
#if ($page == $paginator->currentPage())
<li class="active"><span>{{ $page }}</span></li>
#else
<li>{{ $page }}</li>
#endif
#endforeach
#endif
#endforeach
{{-- Next Page Link --}}
#if ($paginator->hasMorePages())
<li>›</li>
<li>››</li>
#else
<li class="disabled">›</li>
<li class="disabled"><span>››</li>
#endif
</ul>
#endif
The $elements variable seems to already have the extra pages removed, however.
The paginator uses the bootstrap-4 blade view to render the page. This view always implements the tripple dots.
You could create a custom view that renders all pagination pages without any tripple dot items and pass this view as the first parameter to the links function.
Update: The view you are using is exactly the same as the one build in to Laravel. You shouldn't use the $element variable as this gets windowed with tripple dot elements.
You should use the pagination lastpage variable and loop over all pages yourself. For example, like this:
{{-- Pagination Elements --}}
#for ($page = 1; $page <= $paginator->lastPage(); $page++)
#if ($page == $paginator->currentPage())
<li class="active"><span>{{ $page }}</span></li>
#else
<li>{{ $page }}</li>
#endif
#endforeach

Can't access data from named error bag in Laravel view

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

How to show comments and replycomments in laravel

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>

Resources