Laravel recursive partials view categories - php

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.

Related

How to foreach categories from 3rd index from my database / Laravel?

I am issuing a problem with the following:
I foreach-ed my categories from index 1 to 3 in my blade file.
Code
#foreach($categories->take(3) as $category)
<ul>
<li>{{$category->name}}</li>
</ul>
#endforeach
But now I want to foreach second half of my categories from my database on the other ul list.
How to do that..?
Thanks:)
You can use splice method
to popout the first 3 elements and then use another foreach to create another ul list from the other categories like below:
<?php
#foreach($categories->splice(0, 3) as $category)
<ul>
<li><a>{{ $category->name }}</a>
</ul>
#endforeach
#foreach($categories as $category)
<ul>
<li><a>{{ $category->name }}</a>
</ul>
#endforeach

Laravel How to add orderBy on categorized posts

I have a laravel-application where I have a Blog section. The posts are split up into categories so that the user can switch between each categories. So far so good but since I added the categorization and I click the "All"-tab, the orderBy()-method does not work anymore. Instead, the posts appear under their respective category, ignoring the orderBy-method.
Here is my controller:
$blogs = QueryBuilder::for(Blog::class)
->allowedFilters([
AllowedFilter::exact('category', 'blog_category_id'),
])
->orderBy('publishes_on', 'desc')
->where("publishes_on", "<=", Carbon::now())
->where('blog_status_id', '=', '2')
->with('blog_category', 'employee')
->get();
$blog_categories = BlogCategory::get();
return view('blog.index', compact('blogs', 'blog_categories'));
then in my view:
// the tab menu - click events handled with Javascript
<ul>
<li data-tab-id="all" class="tab all active">All</li>
#foreach ($blog_categories as $category)
<li data-tab-id="{{ strtolower($category->name) }}" class="tab {{ strtolower($category->name) }}">{{ $category->name }}</li>
#endforeach
</ul>
// the posts loop
#foreach($blog_categories as $category)
#foreach($category->blog_post as $blog)
<div class="col-md-4 post {{ strtolower($category->name) }}">
<p>{!! $blog->text !!}</p>
</div>
#endforeach
#endforeach
so, how can I achieve that the orderBy works again as intented?
thanks in advance!

get unique Tasks From relation

I have relation tags that have many tasks whet I get data from foreach I get duplicated values
in example
in my path i have 2 tags PHP ,HTML5
PHP Has [PHP Task_1 , PHP_Task_2]
HTML5 Has [HTml5 Task_1,PHPTask_2]
because task has many tags so i will get to duplicated PHP_Task_2
i need to get every task just one time
My Controller
$posts2 = Path::with(['pathtags' => function ($q) use ($TagArray)
{$q->with(['Tasks' => function ($q) use ($TagArray) {$q->has('tasktags', '=', 2)
->with('tasktags');
}]);
}])->where('id', '=', 1)->get();
My Blade
#foreach ($posts2 as $item)
<h2> {{$item->name}}</h2>
#foreach ($item->pathtags as $Tag)
<li> Path Tag :: {{ $Tag->name }} </li>
#foreach ($Tag->Tasks as $Task)
<li> Task :: {{ $Task->task_name }} </li>
#foreach ($Task->tasktags as $TaskTag)
<li> Task Tags :: {{ $TaskTag->name }} </li>
#endforeach
#endforeach
#endforeach
#endforeach

Undefined offset: 1 in CompilesLoops.php laravel 5

I have a services list. Now I want to add sub-services to services list. I have two tables 'services' and 'sub-services' with foreign key constraint 'service_id'. Now, I want to show the 'services' and related 'sub-services' in master.blade.php. For services it was working fine, but, when trying with sub-services then getting this error. Would someone please help to get the expected result.
In master.blade.php-
<li class="dropdown"> Services
<ul class="dropdown-menu services-dropdown" role="menu">
#forelse(App\Model\Service::all() as $service)
<li class="dropdown-submenu">
{{ $service->title }}
<ul class="dropdown-menu sub-services">
#foreach(App\Model\SubService::where('service_id', '=',$service->id)->get()) as $subservice)
<li>
{{ $subservice->title }}
</li>
#endforeach
</ul>
</li>
#empty
#endforelse
</ul>
</li>
Two tables are here-
1.Services table
2.Sub-services table
You're using the wrong syntax. You're using redundant ) near the get(), so change it to:
#foreach(App\Model\SubService::where('service_id', $service->id)->get() as $subservice)
Also, as I say in my best practices repo, you shouldn't execute queries in a Blade template. Consider moving the logic to a controller.
This error also occurs when you don't close you loop correctly.
Use #foreach() to start a loop and #endforeach to close the same loop.
Its a bad way to write a logic part in blade file.I would suggest you to move it to controller because if in case you need to change the code you have to edit blade page.As well as please make use of relationship for fetching the data you can relate your Service with SubService
E.g
Service.php (model file)
public function subServices()
{
return $this->hasMany('App\SubService');
}
SubService.php (model file)
public function services()
{
return $this->belongsTo('App\Service','service_id');
}
your blade code:
#forelse(App\Model\Service::all() as $service)
<li class="dropdown-submenu">
{{ $service->title }}
<ul class="dropdown-menu sub-services">
#foreach($service->subServices as $subservice)
<li>
{{ $subservice->title }}
</li>
#endforeach
</ul>
</li>
#empty
#endforelse

Multi-level menu in laravel 5.1 - loops

I am a little bit new with laravel 5.1 framework. Last couple of days I create my database (insert, update, delete) for dynamic menu I want to create. I connect with default layout in which i put menu. From route code looks like this.
View::composer('layouts.default',function($view){
$menus = Menu::where('parent_id',0)->orderBy('order')->get();
$submenus = Menu::where('parent_id','!=',0)->get();
$view->with(compact('menus','submenus'));
});
In main menu are items with parent_id = 0. Submenu items have parent_id = id, and soo on.
I want to display correct but when I hover main menu items that dont have items, css block appear, becouse i didnt make good if condition. Is there any way to do this?
Code in view look like this.
#foreach($menus as $menu)
<li class="dropdown {!! (Request::is('typography') || Request::is('advancedfeatures') || Request::is('grid') ? 'active' : '') !!}"> {!! $menu->title !!}
<ul class="dropdown-menu" role="menu">
#foreach($submenus as $submenu)
#if($submenu->parent_id === $menu->id)
<li>{!! $submenu->title !!}
#foreach($submenus as $smenu)
#if($smenu->parent_id === $submenu->id)
<ul class="dropdown-submenu" role="menu">
<li>{!! $smenu->title !!}
</li>
</ul>
#endif
#endforeach
</li>
#endif
#endforeach
</ul>
</li>
#endforeach
One more question is how to take only one value from Menu model for example id that can be used to point only one submenu.
Best regards!
You can do it simply by using recursion. I removed html classes for readability.
Note: Set NULL parent_id for root items.
Add this relation to your Menu Model.
function childs()
{
return $this->hasMany('Namespace\Menu','parent_id', 'id');
}
In your controller get the menus who has no parent.
$menus = Menu::whereNull('parent_id')->orderBy('order')->get();
Then display them in your view.
<ul>
#foreach($menus as $menu)
<li>
{!! $menu->title !!}
#include('childItems')//recursion view
</li>
#endforeach
</ul>
And this is what your childItems.blade.php will look like.
<ul>
#foreach($menu->childs as $menu)
<li>
{!! $menu->title !!}
#include('childItems')//call itself for deeper relations.
</li>
#endforeach
</ul>
That's it.

Categories