Laravel How to add orderBy on categorized posts - php

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!

Related

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

Laravel take first 5 foreach

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

How to make url with slug in Laravel 5.4?

Right now I am trying to do a Laravel url with slug. I have my main page where I show all categories:
<div class="panel-body">
#foreach($categories as $category)
<p>{{ $category->name }}</p>
#endforeach
</div>
And now I want to go to a certain category page. I created routes:
Route::get('/test', 'FuelAccountingController#index')->name('fuel_accounting');
Route::get('/test/{slug}', 'FuelAccountingController#element');
And functions in controller:
public function index()
{
$categories = DB::table('fuel_accounting_categories')
->select('id', 'name', 'slug')
->get();
return view('pages.fuel_accounting')->with('categories', $categories);
}
public function element($slug)
{
$category = DB::table('fuel_accounting_categories')
->select('slug')
->where('slug', '=', $slug)
->first();
return view('pages.fuel_accounting_element')->with('category', $category);
}
And when I am trying to reach a page (for example: laravel.dev/test/current_category) it does not work. Can someone explain me how to do that?
Error: Sorry, the page you are looking for could not be found. (1/1) NotFoundHttpException
FIXED
<div class="panel-body">
#foreach($categories as $category)
<p>{{ $category->name }}</p>
#endforeach
</div>

Laravel recursive partials view categories

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.

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