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
Related
I have a menu and in this menu items come from Categories
For the test, I create a new section called Menu for this menu to create the menu manually.
But after that, I return all codes to the previous version, But still, I have an error for undefined $menus
But I don't have this menu anywhere, where is the problem?
Error
Undefined variable $menus (View:
C:\Web\projects\thermotajhiz\resources\views\layouts\header.blade.php)
View
<ul class="list-menu-level-2">
#foreach($categories as $category)
#if($category->menu == 1)
<li class="item-menu-2">
<a href="#" class="list-category-menu-2" rel="nofollow">
<i class="fa fa-desktop"></i>
{{ $category->name }}
</a>
<ul class="megamenu-level-3" style="display:block;">
#include('layouts.categories-group',['categories' => $category-
>get_sub($category->id)])
</ul>
</li>
#endif
#endforeach
</ul>
Controller
$categories = Category::with('child')->where('parent_id', 0)->get();
return view('index', compact('categories'));
Please run a command php artisan view:clear, because laravel makes a cache for every view file. after runnig this command, all compiled views should clear and rebuild cache for view for present file.
Here I want to make one submenu active out of four submenu. This is for dynamic slug .
<div class="solution_tabs">
#foreach($allMenu as $menu)
<ul class="submenu">
#if(isset($menu->submenus))
#foreach($menu->submenus as $submenu)
#if(isset($submenu->page->slug))
<li class="active">{{ $submenu->name}}</li>
#else
<li>{{ $submenu->name}}</li>
#endif
#endforeach
#endif
</ul>
#endforeach
</div>
same as this image,i want one active submenu from dynamic submeun of the menu
You should compare the current URL with your link URLs in order to detect coincidences. One way to accomplish it could be injecting request in your blade file, using
#inject('request', 'Illuminate\Http\Request')
and then check for matches like this:
<li class="{{ $request->segment(1) == $submenu->page->slug ? 'active' : '' }}">{{ $submenu->name}}</li>
Please note that segment() is 0 based, so segment(1) works when your url is like example.com/segment(0)/page->slug
I am having a struggle with an error I am getting: Undefined variable: orbitdata.
I have looked at some answers and I know this happens when there is no data in the database that matches the filter that I have in the controller. If there is data there is no problem.
The controller I am using is this:
$satellite = DB::table('satellites')->where('norad_cat_id', $norad_cat_id)->get();
$data = DB::table('satellites')->where('norad_cat_id', $norad_cat_id)->get();
$orbitalelements = DB::table('tle')->where('norad_cat_id', $norad_cat_id)->get();
return view('pages/satellite', compact($data, 'data', $orbitalelements, 'orbitalelements'));
The logic behind the controller is to filter the database by norad_cat_id and return the row which has the same norad_cat_id.
The 'tle' table might not have any data for the filtered norad_cat_id, so $orbitalelements returns no data. When it does not I get this error.
My blade file contains this foreach statement for the $orbitalelements variable:
#foreach($orbitalelements as $orbitdata)
#endforeach
I have tried adding an isset statement (which did not work):
#if(isset($orbitalelements))
#foreach($orbitalelements as $orbitdata)
#endforeach
#endif
How do you make it so that I don't get an Undefined variable error when there is no data from the database to return?
EDIT: Here is my full blade file:
<div class="satellite-content">
<div class="main-satellite-data">
<ul>
#foreach($data as $satellite)
<span id="satellite-name">{{$satellite->satname}}</span>
<li><span id="intldes">INTLDES: </span>{{$satellite->intldes}}</li>
<li><span id="noradid">NORAD Cat ID: </span>{{$satellite->norad_cat_id}}</li>
<li><span>Object Type: </span>{{$satellite->object_type}}</li>
<li><span>Country: </span>{{$satellite->country}}</li>
<li><span>Launch Date: </span>{{$satellite->launch}}</li>
<li><span>Launch Site: </span>{{$satellite->site}}</li>
<ul>
<li id="list-name"><span>ORBITAL ELEMENTS</span></li>
<li><span id="inclination">Inclination: </span>{{$satellite->inclination}}</li>
<li><span id="period">Period: </span>{{$satellite->period}}</li>
<li><span id="apogee">Apogee: </span>{{$satellite->apogee}}</li>
<li><span id="perigee">Perigee: </span>{{$satellite->perigee}}</li>
<li><span id="decay">Decay: </span>{{$satellite->decay}}</li>
#endforeach
#forelse($orbitalelements as $orbitdata)
<li><span id="epoch">Epoch: </span>{{$orbitdata->epoch}}</li>
<li><span id="meanmotion">Mean Motion: </span>{{$orbitdata->mean_motion}}</li>
<li><span id="eccentricity">Eccentricity: </span>{{$orbitdata->eccentricity}}</li>
<li><span id="meananomaly">Mean Anomaly: </span>{{$orbitdata->mean_anomaly}}</li>
<li><span id="bstar">Bstar: </span>{{$orbitdata->bstar}}</li>
</ul>
#empty
<p>no data</p>
#endforelse
</ul>
<div class="map-tle-padding">
<div id="satellitemap"></div>
<div class="tle-data">
<ul>
<li id="list-name-tle"><span>TLE Data</span></li>
#forelse($orbitalelements as $orbitdata)
<li id="tle-data-main">{{$orbitdata->tle_line0}}</li>
<li id="tle-data-main">{{$orbitdata->tle_line1}}</li>
<li id="tle-data-main">{{$orbitdata->tle_line2}}</li>
#empty
<p>no data</p>
#endforelse
</ul>
</div>
</div>
</div>
</div>
Results:
Without data: http://i.imgur.com/LNQCDLD.png
With data: http://i.imgur.com/27HD6qb.png
Laravel includes an option for this:
#forelse($orbitalelements as $orbitdata)
...
#empty
...No data...
#endforelse
Also, you are using compact wrong. It should just be:
return view('pages/satellite', compact('data', 'orbitalelements'));
#if(count($orbitalelements))
#foreach($orbitalelements as $orbitdata)
#endforeach
#endif
Should work since count($orbitalelements) count is zero, which is falsy.
I have 2 models 'Trips.php'
public function region()
{
return $this->belongsTo('App\Region');
}
Region.php
public function trips()
{
return $this->hasMany('App\Trip');
}
I'm trying to show the trips that are in one one specific region. For example: I have a region named Lake Side and I want to show all trips that are in Lake Side region in list.
I tried the following:
In controller:
$trips = Trip::all();
In view:
#foreach($trips as $trip)
<li><a href="#">
{{$trip->region->name}}</a>
<ul class="dropdown">
<li><a href="#">
{{$trip->title}}</a>
</li>
</ul>
</li>
#endforeach
This gives me region name and trip name but repeats region name if more than one trip is made in same region.
And tried another way around (inverse):
<ul class="dropdown">
#foreach($regions as $region)
<li><a href="#">
{{$region->tour->title}}</a>
</li>
#endforeach
</ul>
And getting error Trying to get property of non-object
One way to do that is to use whereHas():
$trips = Trip::whereHas('region', function($q) use ($regionName) {
$q->where('name', $regionName);
})->get();
Pass $trips and $regionName to the view:
#foreach ($trips as $trip)
<li>{{ $regionName }}
<ul class="dropdown">
<li><a href="#">
{{ $trip->title }}</a>
</li>
</ul>
</li>
#endforeach
Alternatively, you can use eager loading:
$region = Region::where('name', $regionName)->with('trips')->first();
And in the view:
#foreach ($region->trips as $trip)
<li>{{ $region->name }}
<ul class="dropdown">
<li><a href="#">
{{ $trip->title }}</a>
</li>
</ul>
</li>
#endforeach
By getting trips with regions, what you're essentially getting is every trip with a region property.
Instead do the reverse Region::with('trips')->where('id', $regionId)->get() and you'll get regions with their trips. So now every region result has a trips property which contains many trips.
Alternatively don't use eager load. So Region::firstOrFail($regionId) then just use $region->trips.
And you can loop through them like
// Can print region here
#foreach($region->trips as $trip)
// Can print region's trips here
#endforeach
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.