How to set active class? - laravel

I get link something like this : http://local.dev/blog/articles/2/Category12
#foreach($categories as $category)
<li>{{$category->title}} ({{ $category->count }})</li>
#endforeach
What i want is to set active class on li for category that im on. Any suggestion? Maybe i need to compare id from url and this in foreach. But how?

You can get ID of a category from URI with request()->segment(3):
<li {{ request()->segment(3) == $category->id ? 'class="active"' : '' }}>

yeild() in the <li> section as css class with a name and active it from the view layout. Like
<li class="#yield('mymenu')">
<a href="/blog/articles/{{$category->id}}/{{$category->slug}}">{{$category>title}} ({{ $category->count }})
</a>
</li>
In the view file
#section('mymenu', 'active')
So when you'll click on the link it will generate a class which name mymenu and when it will load to a view file then it will be active.

Related

spring boot thymleaf i am trying to use conditional if in tymeleaf and tie it to a controller class to return true or false no pojo needed

i have a nav bar that i am using as a template.. see pic
if i am on any of the tabs then i want home to show. But if i am on the home page i don't want Home tab to show its redundant. i cant seem to figure out how to write the thymleaf code..
i have something like this the first line is what i am interested in fixing... can you please help with the controller part also i am sure i can do a model.addAttribute line in each method call and set the isActive to true except the home method i can set the isActive to false ... thank you
li th:text="${isActive} ? 'Home : "
<li th:text="${isActive} ? '<a th:href="#{/}">Home</a> : " </li>
<li><a th:href="#{/about}">About</a></li>
<li class="auctions">Auctions <i class="fas fa-caret-down"></i>
<ul>
<li>Current Auctions</li>
<li>Register</li>
</ul>
</li>
<li><a th:href="#{/contact}">Contact</a></li>
<li><a th:href="#{/locations}">Locations</a></li>
you can use this in controller:
model.addAttribute("isActive",true);
and use this in html code:
<li th:if="${isActive == null || !isActive}"><a th:href="#{/}">Home</a></li>
read more about Conditionals in Thymeleaf and simple conditionals if and unless

Homepage won't show newly added records but other pages do

I'm using a Laravel 5.7 in an old web project last year 2019 but now I encountered a very weird bug.
This is a website that when you add a new game, it will show to the menu list and if it is featured it will show to the homepage content.
this is the table ['game_id', 'name', 'key', 'status', 'order', 'is_featured']
in the header view I put this code:
<ul class="nav-menu">
<li class="{{ ($page == "games" ? 'menu-active' : '') }} submenu dropdown"><a href="/games" >Games</a>
<ul class="dropdown-menu">
#foreach(GetPublishedGames() as $game)
<li class="nav-item">{{$game->name}}</li>
#endforeach
</ul>
</li>
...
calling this helper code GetPublishedGames()
function GetPublishedGames(){
$data = DB::table("game")->where("status", 1)->orderBy("order")->get();
return $data;
}
in the homepage controller I had this line
"feat_games" => Game::where("status", 1)->where("is_featured", 1)->orderBy("order")->limit(5)->get(),
that calls in the homepage view:
#if(count($feat_games))
#foreach($feat_games as $feat_game)
<div class="feat-game-item wow fadeInUp">
... some content ...
</div>
#endforeach
#endif
I have no idea why this happening, it works before in the local and development stage. I did tried to clear cache.
any idea?

How to check which controller data object being executed?

I have faced a typical problem. The problem is which controller data object being executed in view page. Here the data object is $items. I have looked most of controller but can not find data object $items . Please help me to find it. Thank in advance. The portion of view page :
#foreach($items as $item)
<div class="partner-logo">
<div class="inner-div red-box">
<p>
<img src="{{ ($item->image) ? asset('file/images/' . $item->image) : "" }}" alt="" />
</p>
<h3>
<a
href="{{ isset($item->url) ?route('page.content.show', $item->url) : "#" }}"> {{ $item->title }}
</a>
</h3>
</div>
</div>
#endforeach
In Laravel, controllers usually pass their data to views in this format:
return view('nameOfView', [
'firstParam' => $firstParam,
'secondParam' => $secondParam,
'etc' => $etc
]);
So you can search in your controllers for the view name and then for the parameter name (in this case $items). If there are multiple controller functions with the $items param and the same view, add a test param to each and see which one your view renders. You can also check in routes/web.php for the current URL you are on, and find the correct controller function that way.

Laravel url with id not creating active class

after getting last url segment i am creating a class active.
last segment id i have in :
#php$get=Request::segment(3);#endphp
<a href="{{BASE}}User/li-ac">
<div class="col-md-12">
<div class="col-md-12 {{ collect(request()->segments())->last()=='li-ac' || collect(request()->segments())->last()=='li-de' ? 'Active' : '' }}">
</div>
</div>
</a>
i want to add this $get after
collect(request()->segments())->last()=='li-de' in if condition to get
my Active class.
when Url Is:
mysite.com/user/li-ac
Its working fine
But Url is:
mysite.com/User/li-de/3
Its not showing that class
how can i add the id at the end of the url in if condition for
sideactive class..
When you're using /newdata/li-de/3 URI, the last segment is 3. So, change the code to:
{{ in_array(request()->segments()[1], ['li-ac', 'li-de']) ? 'sideactive' : '' }}
I am done here is the answer:
#php
$get=Request::segment(2);
#endphp
{{ collect(request()->segments())->last()=='li-de' || $get=='li-de' ? 'active' : '' }}

Laravel: request::is() and request::patch()

Can somebody explain me difeerence between
Request::is()
and
Request::patch()
??
I used request::is in my code to active navbar like this, but i saw that somebody use too request::patch
<div class="nav-collapse">
<ul class="nav">
<li class="{{ Request::is('/') ? 'active' : ''}}"><a href="/">#
</a</li>
#foreach($categories as $category)
<li class="{{ Request::is($category->name) ? 'active' : '' }}">
{{$category->name}}</li>
#endforeach
</ul>
</div>
In Side patch Request :
patch request says . only send the data which one you want to update
and it won't effecting or changing other data . so no need to send all
value again . just i want to update my first name so i need to send
only first name to update .
In Side Is Request :
https://indigotree.co.uk/laravel-4-using-request-is-named-routes/
Laravel 4.1 Request::is for active menu not working
Example
<li
{{{ (Request::is('/core') ? 'class=active' : '') }}}>Control Panel
</li>
and Check 'class="active"' to 'class=active'
This working fine for <li> tag but not <a> tag, needs to be used like so:
Overview

Resources