Start retrieved records at 0 MySQL Laravel - php

I have a forum I'm building and replies are always incrementing with their unique id's.
This means that I can have a thread and have the reply number at 550 when the thread starts, not displaying the correct reply number in relation to the thread.
Is there anyway that when I retrieve these records, I can reset them to 1 and have them increment respective to their threads?
Thanks for any help.
Here is what I have so far:
#for($i = 0; $i < $thread->replies->count(); $i++)
Post {{ $i }} of {{ $thread->replies->count() }}
#endfor
It seems to work except this is stuck in my $replies as $reply loop and it proliferates all records.

I would use a #foreach loop and the $loop variable to do the counting, See the $loop doc
#foreach($thread->replies as $post)
Post {{ $loop->iteration }} of {{ $loop->count }}
#endfor
In the case of spanning multiple pages, you should implement Pagination instead See the Pagination doc

{{ ($replies->currentPage()-1) * $replies->perPage() + $loop->index + 1 }} of {{ $replies->total() }}
Found here: Laravel - Use $loop->iteration with pagination

Related

Increase variable in column name Laravel

I have those columns in my database table :
value_day_1 | value_day_2| value_day_3 |......|value_day_36
I'm trying to display each value in a view using a for loop
#for ($n=1;$n<37;n++)
{{ $day->value_day_? }}
#endfor
How can i replace the ? by $n ?
One solution would be
#foreach(range(1,37) as $n)
#php($column = 'value_day_' . $n;)
{{ $day->$column }}
#endforeach
I prefer to use range instead of the for syntax but it is not neccessary for your problem
#for ($n=1;$n<37;n++)
$d='value_day_'.$n;
{{ $day->$d }}
#endfor
Just assign to a new variable before
You can do this easily inline:
$day->{'value_day_'. $n}

How to comma separate properties from a Laravel returned object

First, my apologies if the issue has been resolved. I read a lot of similar posts, but not quite the right one for my case.
I am setting up a simple project in Laravel 5.8. I want to create a simple bookstore and I need a book to have multiple authors so each title is followed by the author, or authors - if many - separated by commas and the last one by the word 'and'.
I have set up two Models, 'Author' and 'Book' and their respective tables, as well as a pivot table since they are in a relation belongsToMany. Everything works like a charm, I get my results as expected. However, I cannot get to format the results as I need. In the case of multiple authors, I always get an extra comma in the end. Since I cannot get the commas right I haven't yet tried to add the last 'and' in the case of the last author.
In the case of a single author the solution is easy, I just use a conditional.
However in the case of multiple authors, thing get complicated.
The most popular method to similar problems was the use of implode() in various similar ways. The problem with this method is since Eloquent is using its internal logic to perform the query, when I loop through 'books', there is no author column, just a reference to the pivot table. In addition, the authors' include first name and last name in different columns. So, when I try to manually create an 'implodable()' array by fetching the respective data otherwise, I get double the item size, since each name consists of the first name and theist name. And on top of that, the whole thing runs inside a double loop, making things even more complicated for me.
I am sure there is a simple way around this.
This is a sample of my blade code as of now. Of course the conditionals should be rearranged accordingly when the problem will be solved, to implement the 'and' case:
<ul>
#foreach ($books as $book)
<li>{{ $book->title }} by
#if (count($book->author) == 1)
#foreach ($book->author as $name)
{{ $name->last_name }}
{{ $name->first_name }}
#endforeach
#else
#foreach ($book->author as $name)
{{ $name->last_name }}
{{ $name->first_name }}
{{-- print a comma here if there are other names, or an 'and' if it the last one. Problem seems that it needs to be solved outside the conditional, but how? --}}
#endforeach
#endif
</li>
#endforeach
</ul>
My DB structure:
'authors': 'id', 'last_name', 'first_name','created_at', 'updated_at'
'books': 'id', 'title', 'created_at', 'updated_at'
'author_book': 'id', 'author_id', 'book_id','created_at', 'updated_at'
Expected result:
Title 1, by Author 1
Title 2, by Author 1 and 2
Title 3, by Author 1, 2 and 3
Actual Result:
Title 1, by Author 1
Title 2, by Author 1 and 2 and
Title 3, by Author 1 and 2 and 3 and
If you didn't need the "and" mechanism, then implode with comma would do the job. For "and" mechanism use below code:
<ul>
#foreach ($books as $book)
<li>{{ $book->title }} by
#for ($i = 0; $i < count($book->author); $i++)
{{ $book->author[$i]->last_name }}
{{ $book->author[$i]->first_name }}
#if ($i == count($book->author) - 2)
and
#endif
#if ($i < count($book->author) - 2)
,
#endif
#endfor
</li>
#endforeach
</ul>
added non-breaking-space &nbsp wherever necessary to not get them linked to each other
Delete #if (count($book->author) == 1), if you use foreach inside.
You can't use #else, when before you close condition with #endif.
This #else has wrong construction, use #elseif instead.
If you want check authors count and show extra value, do it inside foreach with $book->author()->count().
What relation u have for book -> author? One book can have many authors or not?
Just display implode(', ', $book->author), based on your actual result you don't need to do complicated checking if/else anymore.
#foreach ($books as $book)
<li>{{ $book->title }} by {{print implode(', ', $book->author)}}</li>
#endforeach

Laravel - Foreach loop - Show a specific message for types of result

So I have a messages table with all messages. I want pinned messages to be at top and normal ones to follow after. I have been able to do this normally with the orderBy() method in my query.
However, I want to display some specific messages for all my pinned messages. I would like to have a header at the top of the pinned messages and a header at the top of the normal messages to let users know that pinned and normal messages are there.
Example:
My query:
$rows = Messages::where('active', true)->orderBy('pinned', 'desc')->get();
My view
#foreach ($rows as $row)
{{ $row->message }}
#endforeach
What I see
Message text 3
Message text 1
Message text 2
I have a few messages with "pinned" in the column in database. So I want the pinned ones to show at the top WITH DESCRIPTIONS. Something like this:
Pinned
----------
Message text 3
----------
Normal
----------
Message text 1
Message text 2
I have tried orderBy() and it's working pretty good, in terms of ordering it from pinned to normal, but I can't get it to show the "Pinned" and "Normal" message. How can I do this?
Try something like this (change 1/0 to true/false or whatever you use):
In a controller:
$pinned = $rows->where('pinned', 1);
$normal = $rows->where('pinned', 0);
In a view:
#if(count($pinned) > 0)
Pinned
#foreach ($pinned as $row)
{{ $row->message }}
#endforeach
#endif
#if(count($normal) > 0)
Normal
#foreach ($normal as $row)
{{ $row->message }}
#endforeach
#endif
If real #foreach part is big, use partial and #each instead of #foreach to avoid code duplication.
Alternative
#foreach ($rows as $row)
#if ($row->pinned === 1 && !isset($pinnedShown))
Pinned
{{ $pinnedShown = true }}
#endif
#if ($row->pinned === 0 && !isset($normalShown))
Normal
{{ $normalShown = true }}
#endif
{{ $row->message }}
#endforeach
Short alternative
Not very readable, but if you just need short code, use something like this:
#foreach ($rows as $row)
<?php !($row->pinned == 1 && !isset($pin)) ? : $pin = call_user_func(function(){ echo 'Pinned'; return 1; });
!($row->pinned == 0 && !isset($nor)) ? : $nor = call_user_func(function(){ echo 'Normal'; return 1; }); ?>
{{ $row->message }}
#endforeach

Laravel 5.1 pagination count

I am using the paginator of Laravel to display 100 results per page. In the front of each row I'm currently displaying the ID of the database field. However, I want to display the count.
I've found some threads on stackoverflow solving this issue for Laravel 4
Where they say, you should solve it like this
<?php $count = $players->getFrom(); ?>
#foreach ($players as $player)
<tr>
<td>{{ $count++ }}. </td>
</tr>
#endforeach
or like this
<?php $count = $players->getFrom() + 1; ?>
#foreach ($players as $player)
...
The problem here, is that Laravel 5.1 doesn't have the getForm method anymore. The upgrade guide says to replace getFrom by firstItem, which I did. Unfortunetely, I'm not getting the correct numbers.
I tried it like this
<?php $count = $pages->firstItem() + 1 ?>
#foreach ($pages as $page)
#include('pages.singlepagebasic')
#endforeach
and like this
{{ $count = $pages->firstItem() }}
#foreach ($pages as $page)
#include('pages.singlepagebasic')
#endforeach
//pages.singlebasic.blade.php
{{ $count ++ }}
but my site always displays only the number "2" instead of counting up. How do I achieve that?
Just use this to get pages count
$players->lastPage()
or this to get items count
$players->total()
If you want every entry count DONT DO {{ $count++ }} because its echoing data. Instead do like that
<?php $count++; ?>
{{ $count }}
First of all get 100 players with paginate(100) method then you can get the index or count in the following way:
<?php $count = 1; ?>
#foreach ($players as $player)
<tr>
<td>{{$players ->perPage()*($players->currentPage()-1)+$count}}</td>
</tr>
<?php $count++; ?>
#endforeach
Here {{$players ->perPage()*($players->currentPage()-1)+$count}} will print the index or count like 1 to 100 on first page and 101 to 200 on next page and so on.
If you want to use laravel 5 loop itration
`#foreach ($players as $player)
<tr>
<td>{{$players->perPage()*($players->currentPage()-1)+$loop->iteration}}</td>
</tr>
#endforeach`
Hope this helps.
What i used to do in order to avoid php tags in blade templates in 4.2 was something like that
#foreach ($players as $key => $player)
<tr>
<td>{{ (Input::get('page', 1) - 1) * $players->getPerPage() + $key + 1 }}. </td>
</tr>
#endforeach
It's kinda strange but it will do the trick.

Outputting Multidimensional Array in View

I am currently working on a Laravel project and run the following query to build this multidimensional array:
Basically what I am trying to do is see the 5 different statuses and get the count of how many of those applications with a certain status are assigned to a certain admin.
The problem I am running into is outputting this data on my view. This is what I have so far:
I am getting an error saying undefined index first_name.
Any help would be much appreciated!
EDIT: Here is the output of var_dump($totalCounts);
The reason that you get undefined index is that you don't assign an exact index first_name.
Lets say the user's first name is Burak, status is Something and count is 5 within the line below.
$totalCount[$count->first_name][$count->status] = $count->count;
And actually you assign like below.
$totalCount['Burak']['Something'] = 5;
So if you want to access to it, then you need to reach to the first name within your view. To access them within your view :
#foreach ($totalCount as $name => $count)
<tr>
<td>
{{$name}}
<br>
Closed
</td>
#for($i = 1; $i <= 6; $i++)
<td><a href="#">
#foreach ($count as $status => $c)
#if($i == $status)
{{ $c }}
#endif
#endforeach
</a></td>
#endfor
</tr>
#endforeach

Categories