I want to have pagination in single post page, datas comes from "contents" table. But i want to have pagination when catch spesific word (Section) as a title go to next page in same post.
Is this possible to make it with Laravel 5.4 .
Example :
Section 1 ( this is title)
text text text text text text
( next page > I want to add next page and it will go Section 2 page)
Section 2
text text text text text text
( next page >)
my Post.php
public function contents()
{
return $this->hasMany(Content::class);
}
blade view :
#if($post->contents)
#foreach ($post->contents as $content)
#if ($content->type == "header")
<h4>{{ $content->body }}</h4>
#endif
#if ($content->type == "text")
<p>{{ $content->body }}</p>
#endif
#endforeach
#endif
For pagination use paginate() inside your model.
public function contents()
{
return $this->hasMany(Content::class)->paginate(1);
}
View file
#if($post->contents)
#foreach ($post->contents() as $content)
#if ($content->type == "header")
<h4>{{ $content->body }}</h4>
#endif
#if ($content->type == "text")
<p>{{ $content->body }}</p>
#endif
#endforeach
{{ $post->contents()->links() }}
#endif
Related
In a laravel blade in third line of this code I want to check if parent_id exists in id column or not
please help me!
I'm using laravel 9
#if ($category->parent_id == 0)
no parent
#if ($category->parent_id)
no parent
#else
{{ $category->parent->name }}
#endif
I corrected it this way:
#elseif (empty($category->parent))
Using exists() function for parent()
Not that exists function works just with single relations (belongsTo, hasOne)
// This will run SQL query // returns boolean
$category->parent()->exists(); // Don't forget parentheses for parent()
If you want to save performance and not calling sql query
count($category->parent); // returns 0 if not exist
Balde:
you can use the empty() to check if empty.
#if ($category->parent == 0)
no parent
#elseif (empty($category->parent))
<p>no parent</p>
#else
{{ $category->parent->name }}
#endif
or ?? operator
{{ $category->parent_id ?? 'no parent' }}
You can use the is empty in twig as below:
{% if category is empty %}
<p> No parent </p>
{% endif %}
You can try by using isset() function
#if ($category->parent_id == 0)
no parent
#if (!isset($category->parent_id))
no parent
#else
{{ $category->parent->name }}
#endif
I have an #if/#else condition in a Blade file to present different values of a PHP table. The data comes from a Laravel/Livewire controller, and the Blade code that creates the issue is below.
#if(!$course->user_limit)
#if(in_array($course->id, $arrs))
<x-jet-button wire:click="confirmCourseInterest( {{ $course->id}})"
class="bg-orange-500 hover:bg-orange-700">
{{__('Κάντε κλικ για απόσυρση ενδιαφέροντος')}}
</x-jet-button>
#else
<x-jet-button wire:click="confirmCourseInterest( {{ $course->id}})"
class="bg-orange-500 hover:bg-orange-700">
{{__('Κάντε κλικ για εκδήλωση ενδιαφέροντος')}}
</x-jet-button>
#endif
#else
<div class="flex" id="user_limit_reached">
{{ __('User Limit Reached') }}
</div>
#endif
The issue occurs on the outer if/else that irregularly returns both if and else statements. The user_limit is false on the field that has the problem. Why does this happen?
Change your #else to #elseif and pass another logic there.
I have a dataset like below inside a cell named rolls.
[
"142650",
"142651",
"142603",
"142604"
]
I have an array named $rooms, where each room has cell called rolls.
#foreach($rooms as $room)
{{ $room->rolls }}
#endforeach
The code above displaying data like below in view
["142650", "142651", "142603", "142604", "142605"]
But I want to display like below...
142650, 142651, 142603, 142604, 142605
I have tried this
#foreach($rooms as $room)
#foreach($room->rolls as $roll)
{{ $roll }}
#endforeach
#endforeach
But getting error like below
Invalid argument supplied for foreach()
I think the cleanest solution, without changing controller is
#foreach($rooms as $room)
<h1>{{ $room->name }}</h1>
#foreach(json_decode($room->rolls) as $roll)
{{ $roll }}
#endforeach
#endforeach
Try not to use #php or <?php ?> in your blade templates
This is the solution I have found, I have not changed anything in my controller, this is what I have done in the view.
#foreach($rooms as $room)
<h1>{{ $room->name }}</h1>
<?php $rolls = json_decode($room->rolls); ?>
#foreach($rolls as $roll)
{{ $roll }}#if(!$loop->last), #endif
#endforeach
#endforeach
I trying to take out 5 users from DB by points.
BLADE:
#foreach ($users as $user)
<li>{!! $user->username !!} {!! $user->ELO_points !!}</li>
#endforeach
</div>
But i need just first 5 and first 3 with custom text content, example:
USER1 - CUSTOM CONTENT TEXT
USER2 - CUSTOM CONTENT TEXT
USER3 - CUSTOM CONTENT TEXT
USER4
USER5
I have been tried with #if ($loop->first)This is the first iteration.#endif
But loop take out just first and last one yeah?
You can use $loop->index in your loop :
#foreach ($users as $user)
<li><a href="/players/{!! $user->slug !!}">
{!! $user->username !!} {!! $user->ELO_points !!}
{!! $loop->index <=3 ? 'custom content text' : '' !!}
</a></li>
#endforeach
You can use the take way of doing it. Example
IN CONTROLLER (To make sure you only get the 5, user orderBy to either get the first or the last.)
$users = Users::all()->take(5)->get();
IN VIEW: (To make sure the 3 first get the custom content text
#foreach ($users as $user)
<li><a href="/players/{{ $user->slug }}">
{{ $user->username }} {{ $user->ELO_points }}
{{ $loop->index <=3 ? 'custom content text' : '' }}
</a></li>
#endforeach
I have a #foreach loop in the Blade template and need to apply special formatting to the first item in the collection. How do I add a conditional to check if this is the first item?
#foreach($items as $item)
<h4>{{ $item->program_name }}</h4>
#endforeach`
Laravel 5.3 provides a $loop variable in foreach loops.
#foreach ($users as $user)
#if ($loop->first)
This is the first iteration.
#endif
#if ($loop->last)
This is the last iteration.
#endif
<p>This is user {{ $user->id }}</p>
#endforeach
Docs: https://laravel.com/docs/5.3/blade#the-loop-variable
SoHo,
The quickest way is to compare the current element with the first element in the array:
#foreach($items as $item)
#if ($item == reset($items )) First Item: #endif
<h4>{{ $item->program_name }}</h4>
#endforeach
Or otherwise, if it's not an associative array, you could check the index value as per the answer above - but that wouldn't work if the array is associative.
Just take the key value
#foreach($items as $index => $item)
#if($index == 0)
...
#endif
<h4>{{ $item->program_name }}</h4>
#endforeach
As of Laravel 7.25, Blade now includes a new #once component, so you can do it like this:
#foreach($items as $item)
#once
<h4>{{ $item->program_name }}</h4> // Displayed only once
#endonce
// ... rest of looped output
#endforeach
Laravel 7.* provides a first() helper function.
{{ $items->first()->program_name }}
*Note that I'm not sure when this was introduced. So, it may not work on earlier versions.
It is only briefly mentioned in the documentation here.
The major problem with Liam Wiltshire's answer is the performance because:
reset($items) rewind the pointer of $items collection again and again at each loop... always with then same result.
Both $item and the result of reset($item) are objects, so $item == reset($items) requires a full comparison of its attributes... demanding more processor time.
A more efficient and elegant way to do that -as Shannon suggests- is to use the Blade's $loop variable:
#foreach($items as $item)
#if ($loop->first) First Item: #endif
<h4>{{ $item->program_name }}</h4>
#endforeach
If you want to apply a special format to the first element, then maybe you could do something like (using the ternary conditional operator ?: ):
#foreach($items as $item)
<h4 {!! $loop->first ? 'class="special"': '' !!}>{{ $item->program_name }}</h4>
#endforeach
Note the use of {!! and !!} tags instead of {{ }} notation to avoid html encoding of the double quotes around of special string.
Regards.
if you need only the first element you can use #break inside your #foreach or #if.see example:
#foreach($media as $m)
#if ($m->title == $loc->title) :
<img class="card-img-top img-fluid" src="images/{{ $m->img }}">
#break
#endif
#endforeach
you can do it by this way.
collect($users )->first();
To get the first element of a collection in Laravel, you can use :
#foreach($items as $item)
#if($item == $items->first()) {{-- first item --}}
<h4>{{$item->program_name}}</h4>
#else
<h5>{{$item->program_name}}</h5>
#endif
#endforeach