I am currently trying to set up my blade layout with laravel 5.4 but I seem to be having issues with the #empty statement while using it with loops
The code I am trying to use is:
#foreach ($notifications as $notification)
<li data-for='{{ $notification->lobbyid }}'>
#if ($notification->approved == 1)
<div class='notification-text'><strong>{{ $notification->sentBy }}</strong> wants access to your lobby: \"{{$notification->title}}\".</div>
<div class='notification-actions' id='HI' data-for='{{ $notification->lobbyid }}' data-userid='{{ $notification->userid }}'><span data-type='{ \"as\": \"requesting\", \"action\": 1 }'>Accept</span><span data-type='{ \"as\": \"invited\", \"action\": 0 }'>Deny</span></div>
#else
<div class='notification-text'><strong>{{ $notification->sentBy }}</strong> invites you to join his lobby: \"{{$notification->title}}\".</div>
<div class='notification-actions' id='HI' data-for='{{ $notification->lobbyid }}' data-userid='{{ $notification->userid }}'><span data-type='{ \"as\": \"invited\", \"action\": 1 }'>Accept</span><span data-type='{ \"as\": \"invited\", \"action\": 0 }'>Deny</span></div>
#endif
</li>
#empty
<li>No Notifications</li>
#endforelse
And I get the following error:
Undefined variable: __empty_0
When used separately it works great:
#endforeach
#empty($notifications)
<li>No Notifications</li>
#endempty
But I would like the cleaner syntax like on the laravel documentation.
#foreach ($notifications as $notification)
it should be #forelse not #foreach
Related
I have followed Jeffery Way in Laravel 8 from scratch amazing series but I'm having a trouble in pagination. In the index page we're making our latest post to have a specific styling and then first 2 posts comes after with a different styling and then rest of posts in different styling. The problem is that these styles are carrying over with us to page2 and 3 and ...etc
I want to make this to only posts in page1 or homepage and when I go to page 2 I should have a default styling for all posts.
This is the index
<x-bloglayout>
#include('posts.__header')
#if ($posts->count())
<x-featuredCard :post="$posts[0]" />
#if ($posts->count() > 1)
<div class="lg:grid lg:grid-cols-2">
<x-postCard :post="$posts[1]" />
<x-postCard :post="$posts[2]" />
</div>
<div class="lg:grid lg:grid-cols-3">
#foreach ($posts->skip(3) as $post)
<x-postCard :post="$post" />
#endforeach
</div>
#endif
{{ $posts->links() }}
#else
<p class="text-center">No posts matches your search, please check back later</p>
#endif
</x-bloglayout>
I tried to use if directive to say if route is home or ?/page1 do this if not do that but it doesn't seem to work.
This is my pagination:
public function index()
{
return view('posts.index', [
'posts' => Post::latest()->filter(request(['search', 'category', 'author']))->paginate(6)->withQueryString(),
]);
}
Thanks
You can use currentPage() method on paginator and check if it's on first page. May not be fanciest way but here is an example:
<x-bloglayout>
#include('posts.__header')
#if ($posts->count())
<x-featuredCard :post="$posts[0]" />
#if ($posts->count() > 1)
#if($posts->currentPage() == 1)
<div class="lg:grid lg:grid-cols-2">
<x-postCard :post="$posts[1]" />
<x-postCard :post="$posts[2]" />
</div>
#endif
<div class="lg:grid lg:grid-cols-3">
#foreach ($posts->skip($posts->currentPage() == 1 ? 3 : 0) as $post)
<x-postCard :post="$post" />
#endforeach
</div>
#endif
{{ $posts->links() }}
#else
<p class="text-center">No posts matches your search, please check back later</p>
#endif
</x-bloglayout>
You can check Paginator methods from Laravel 8.x document: https://laravel.com/docs/8.x/pagination#paginator-instance-methods
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.
Hey everyone I was creating a laravel project and I wanted to add the pagination but I faced this error :
Method Illuminate\Database\Query\Builder::links does not exist. (View:
C:\xampp\htdocs\TestingTask\resources\views\income\index.blade.php)
Here is the controller code:
public function index()
{
$income =Income::orderBy('created_at','desc')->paginate(1);
return view('income.index', compact('income'));
}
The view code:
#extends('layouts.app')
#section('content')
<h3>Profit</h3>
<br>
#if(count($income)>0)
#foreach($income as $income)
<div class="card bg-muted">
<h3>{{$income->title}}</h3>
</div>
#endforeach
{{$income->links()}}
#else
<p>No result</p>
#endif
#stop
You have a typo in your code. change $income to $inc.
#extends('layouts.app')
#section('content')
<h3>Profit</h3>
<br>
#if(count($income)>0)
#foreach($income as $inc)
<div class="card bg-muted">
<h3>{{$inc->title}}</h3>
</div>
#endforeach
{{$income->links()}}
#else
<p>No result</p>
#endif
#stop
This is because of your foreach loop variable kindly change as following
#if(count($income)>0)
#foreach($income as $income_single)
...
#endforeach
{{$income->links()}}
#else
I am fetching records with pagination. The first page is working fine but when I click on page number 2 then it return following error
Undefined offset: 0
I am trying following script in controller
$Jobs = Jobs::orderBy('job_id', 'asc')->paginate(5);
return view('veteran.job-posting.index',['Jobs'=>$Jobs]);
In the blade following code I am trying
#if($Jobs)
#foreach($Jobs as $Job)
{{ ucfirst(trans($Job->job_title)) }}
#endforeach
#endif
For pagination I am using following script
<div class="pagination-test">
{{ $Jobs->links() }}
</div>
Please guide me why it does returning error when I click on page number 2. I would like to appreciate
After Request For More Detail from #pseudoanime
For more detail about error
2/2) ErrorException
Undefined offset: 0 (View: E:\xampp\htdocs\project\project-sub-root\resources\views\index.blade.php)
Try adding this outside of the loop
{{ $Jobs->links() }}
Can you try to add Pagination like this.
#if(count($Jobs) > 0)
#foreach($Jobs as $Job)
{{ ucfirst(trans($Job->job_title)) }}
#endforeach
{{ $Jobs->links() }}
#endif
With your CSS class
#if(count($Jobs) > 0)
#foreach($Jobs as $Job)
{{ ucfirst(trans($Job->job_title)) }}
#endforeach
<div class="pagination-test">
{{ $Jobs->links() }}
</div>
#endif
Edited Update
Try With With in controller
return view('veteran.job-posting.index')->with('Jobs',$Jobs);
Update your controller's function return and use compact function for to pass jobs to view.
$jobs = Job::orderBy("id", "asc")->paginate(5);
return view("jobs", compact("jobs"));
You need to add this for pagination in blade.
<ul class="pagination" >
{!! $Jobs->render() !!}
</ul>
I want to be able to display infinitive ammount of subcategories in my view for example I have in my blade view
#if (count($projects) > 0)
<ul>
#foreach ($projects as $project)
#include('partials.project', $project)
#endforeach
</ul>
#else
#include('partials.projects-none')
#endif
partials.project
<li>{{ $project['name'] }}</li>
#if (count($project['children']) > 0)
<ul>
#foreach($project['children'] as $project)
#include('partials.project', $project)
#endforeach
</ul>
#endif
projects-none
You have no projects!
but after I enter I get error message
Undefined variable: projects but when I dd($projects) i get data from database
Controller code
$projects= Projects::all(); (I tried it even with pluck but it didn't work)
return view('projects')->with('projects', $projects);
I have added dd($projects) after if loop in view to see if I'm receiving it all and I was receiving it;
I. With your "messy way"
- Change
#include('partials.project', $project)
- To
#include('partials.project', ['project'=>$project])
II. Try this better way. I though
- All in partials.projects
<ul>
#each ('partials.project', $projects, 'project', 'partials.projects-none')
</ul>
Similar to partials.project. I lot of code will be saved.