dynamic menu in laravel - php

Im trying to create dynamic menu. Basically,I have two tables :Category and pages.Not sure how should I do this but following is something I have tried
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="{{url('/',null)}}" class="pull-left">Consulate</a>
</div>
<div>
<ul class="nav navbar-nav">
<li class="active">Home</li>
#foreach($categories as $category )
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">{{$category->title}}<span class="caret"></span></a>
<ul class="dropdown-menu">
#foreach($pages as $page)
<li>{{$page->title}}</li>
#endforeach
</ul>
</li>
#endforeach
With above code, I got the same drop down menus in all the categories.I require dropdown only if the category have pages.
example1
example2
My models looks like following:
Pages model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Pages extends Model
{
protected $fillable=[
'title',
'details',
'image',
'category_id',
];
//A page has a category
public function category()
{
return $this->belongsTo('App\Categories');
}
}
categories model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Categories extends Model
{
protected $fillable=[
'title',
'details',
];
public function pages()
{
return $this->hasMany('App\Pages');
}
}

You could store your categories in a database table called categories (id, name, url). You could then also have another table called pages (id, name, url, category_id).
Create a Category and Page model.
Define a one-to-many relationship (one category-to-many pages).
You could then do:
#foreach( $categories as $category )
<!-- display your category html -->
#foreach( $category->pages as $page )
<!-- display your page html -->
#endforeach
#endforeach
Have a look at one-to-many relaitonships in Laravel: Eloquent: Relationships - one-to-many

Related

How to show only one data in blade that coming from collection in Laravel?

im new at Laravel, so i have following problem:
I have db with issue table, that contains 15 names of diseases with it's description. This is my model
class Issue extends Model
{
use HasFactory;
use SoftDeletes;
protected $guarded = false;
public function gender() {
return $this->belongsTo(BodyPart::class);
}
}
And here's controller:
use App\Http\Controllers\Controller;
use App\Models\Issue;
class IndexController extends Controller
{
public function __invoke()
{
$eye_issues = Issue::all();
return view('app.issues.man.eyes.index', compact('eye_issues'));
}
}
And finally below my blade file:
<main>
<section id="schedule" class="section-with-bg">
<ul class="nav nav-tabs" role="tablist" data-aos="fade-up" data-aos-delay="100">
#foreach($eye_issues as $issue)
#if($issue->body_part_id == 2 and $issue->gender_id == 1)
<li class="nav-item">
<a class="nav-link" href="#issue-{{ $issue->id }}" role="tab"
data-bs-toggle="tab">{{ $issue->issue }}</a>
</li>
#endif
#endforeach
</ul>
<div class="tab-content row justify-content-center" data-aos="fade-up" data-aos-delay="200">
#foreach($eye_issues as $issue)
#if($issue->body_part_id == 2 and $issue->gender_id == 1)
<div role="tabpanel" class="col-lg-9 tab-pane fade show active" id="issue-{{ $issue->id }}">
<div class="row schedule-item justify-content-center">
<div class="col-md-10" style=text-align:center>
<h4>{{ $issue->description }}</h4>
Test yourself
</div>
</div>
</div>
#endif
#endforeach
</div>
</div>
</section>
</main>
So problem is, after loading the page, navigation tabs should show all disease names (as it does) and the description section must be empty and show according description of disease that selected from nav-tabs and change description if disease is changed from nav-tabs. But after loading page, in the description section, there are all disease description, even nav-tabs selected to none onload. Only after selecting all diseases from nav-tabs, page works properly.
P.S I use MySQL if case u wanted to know. i tried to use all JS and CSS hide and show functions and other methods from internet, but no one works, and i guess it depends only on laravel it self. if you dont understand what i mean, comment it, i'll leave link for demo video of the issue

I got an error while creating sub menu in my database mysql

I am new to laravel. I want to create submenu for my website using the eloquent. However it gives me and error saying that
Base table or view not found: 1146 Table 'shopping.sub_menus' doesn't exist (SQL: select * from sub_menus where sub_menus.menu_id = 1 and sub_menus.menu_id is not null)
my model menu
Menu.php
class Menu extends Model
{
public function submenu()
{
return $this->hasMany(SubMenu::class);
}
}
// SubMenu Model
class SubMenu extends Model
{
public function menu()
{
return $this->belongsTo(Menu::class);
}
}
and below is blade
side-nav.blade.php
<ul>
#foreach ($menu as $_menu)
<li>
<a href="{{ route($_menu->route) }}" id={{ $_menu->id }} class="side-menu">
#if ($_menu->submene->count())
<ul>
#foreach ($_menu->submenu as $_submenu)
<li><a href="{{ route($_menu->route) }}" id={{ $_menu->id }}>{{$_submenu->$_menu}} </a></li>#endforeach
<div class="side-menu__icon"> <i data-feather={{ $_menu->icon_name }}></i> </div>
<div class="side-menu__title"> {{ $_menu->menu_name }} </div>
</a>
</li>
#endforeach

Laravel and infinity categories as a tree

how to display all categories, with sub, sub, sub (..) category, I have simple table:
and my model:
class Category extends Model
{
public $fillable = [
'parent_id',
'name',
'description'
];
public function parent()
{
return $this->belongsTo(Category::class, 'parent_id', 'id');
}
public function children()
{
return $this->hasMany(Category::class, 'parent_id', 'id');
}
}
I want to get:
<ul>
<li>Category
<ul>
<li>Category 1.1</li>
<li>Category 1.2</li>
</ul>
</li>
(...)
</ul>
This practical approach work for n number of categories with n number of children
First create a partial view category.blade.php file which recursively will call itself to load its children
<li>
#if ($category->children()->count() > 0 )
<ul>
#foreach($category->children as $category)
#include('category', $category) //the magic is in here
#endforeach
</ul>
#endif
</li>
Then in the main view you add this code which loads all children recursively
<ul>
#foreach ($categories as $category)
#if($category->parent_id == 0 )
#include('category', $category)
#endif
#endforeach
</ul>

How to use two foreachs in laravel?

I have a category table, it is organized by 'parent_id' and 'categoryid'. I need to organize it in a list, where I group the parent class with the daughter class.
I created this code.
In the controller I get the value of the categories.
public function index()
{
$cat1 = Category::where('erp_parentid', '=', 0)->get();
foreach($cat1 as $categoria1){
$cat2 = Category::where('erp_parentid', '=', $categoria1->erp_categoryid)->get();
return view('admin.categories')->with('cat1', $cat1)->with('cat2', $cat2);
}
}
$cat2 is the child category, I get its values through the categoryid of the parent category.
But when I pass the values to the view, all parent categories take the same value as the first.
I used that code to display the values in the view:
<div class="container">
<div class="row">
<ul class="list-group">
#foreach($cat1 as $value)
<a data-toggle="collapse" data-target="#catfilha{{$value->erp_categoryid}}"><li class="list-group-item">{{$value->erp_name}}</li></a>
<ul id="catfilha{{$value->erp_categoryid}}" class="collapse">
#foreach($cat2 as $value2)
<li>{{$value2->erp_name}}</li>
#endforeach
</ul>
#endforeach
</ul>
</div>
</div>
I searched for similar cases here on the site, but found no resemblance, any suggestions? Thank you in advance.
You should define the relations in the model and call them in the view. Try something like this:
In Category Model:
public function getParent()
{
return self::where('erp_parentid', '=', $this->erp_categoryid)->get();
}
In Controller:
public function index()
{
$cat1 = Category::where('erp_parentid', '=', 0)->get();
return view('admin.categories')->with('cat1', $cat1);
}
In the View:
<div class="container">
<div class="row">
<ul class="list-group">
#foreach($cat1 as $value)
<a data-toggle="collapse" data-target="#catfilha{{$value->erp_categoryid}}"><li class="list-group-item">{{$value->erp_name}}</li></a>
<ul id="catfilha{{$value->erp_categoryid}}" class="collapse">
#foreach($value->getParent() as $value2)
<li>{{$value2->erp_name}}</li>
#endforeach
</ul>
#endforeach
</ul>
</div>
</div>
In your code, the return statement is inside the loop, so cat2 will be always the categories from the first item of cat1.

Laravel 5.2 filter with dropdownlist

I want to make drop-down list filtering.
I have a web page, that shown some post with title and categories.
The page has a drop-down in nav.blade.php. I dynamically generate drop-down from categories table. But when I select an item of drop-down (for example a category name) I want page to show me the posts only of that category. Also I have created the Category and Posts model and set the relationships. I can see all posts on my main page but can't filter content with drop-down list.
What I am doing wrong? and how can I solve this issue?
My nav.blade:
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"
aria-haspopup="true" aria-expanded="false">Dropdown
<span class="caret"></span></a>
<ul class="dropdown-menu">
<li>#foreach($categories as $category)
<a href="{{URL::route('home',$category->id)}}">
<option value="{{$category->id}}">{{ $category->name }}</option>
</a>
#endforeach
</li>
</ul>
</li>
This would get you started:
Assuming you have a route like:
Route::get('/{category_id}', ['as'=>'home', 'uses'=>'PostController#show']);
In the PostController#show method:
public function show($category_id)
{
$categories = Category::all();
$selected_category = Category::with('posts')->where('id', $category_id)->first();
$posts = $selected_category->posts;
return redirect()->back()->with(compact('posts', 'categories'));
}
You can change the redirect location.

Categories