Laravel Tutorials
- What is laravel
- Laravel Installation
- Directory Structure
- htaccess
- Remove public from url
- Artisan Command
- Laravel Configuration
- Routing Configuration
- Namespaces
- Request
- Response
- Controller
- Model
- User Authentication
- Multi User Authentication
- Database Seeding
- Database
- Database Query
- ORM
- One-to-One Relationship
- One-to-Many Relationship
- Many to Many Eloquent Relationship
- Has One Through
- Has Many Through
- Querying Relations
- Middleware
- Laravel Views
- Blade Views
- Print data on view page
- Get Site URL
- Get URL Segment
- Get images from Storage folder
- Clear cache
- Form Class not found
- Flash Message in laravel
- Redirections
- path
- CRUD Projerct
- CRUD in Laravel
- CRUD progran
- Laravel Validation
- Jquery Validation
- Cookie
- Session
- Email Send in laravel
- File uploading
- CSRF Protection
- Helper in Laravel
- Helper Functions
- Guzzle Http Client
- Paypal Payment Gatway Integration
- Cron Job in laravel
- Flash message
- path
- Errors Handling
- Date Format
- Date Format Validation
- Display Image on View Page
- Update User Status using toggle button
- Delete Multiple Records using Checkbox in Laravel?
- Confirmation Before Delete Record
- Delete image from storage
- Remove/Trim Empty & Whitespace From Input Requests
- Block IP Addresses from Accessing Website
- How to Disable New User Registration in Laravel
- Redirect HTTP To HTTPS Using Laravel Middleware
- CKEditor
- slug generate unique
- Prevent Browser's Back Button After Logout
- Datatable dunamically
- encrypt & Decript
- Download File
- Rest API
- Shopping Cart
- Shopping Cart Example
- Dynamic Category-Subcategory Menu
- Ajax Search
- Interview Question
- laravel Tutorilal link
- laravel Tutorilal
Important Links
How to create Dynamic menu in laravel Application ?
https://www.larashout.com/categories-navigation
You will learn Categories Navigation. you can easily create site navigation with the categories.
First you Install Nested Collection Package
File name : index.php
composer require typicms/nestablecollection
composer require typicms/nestablecollection
Using version ^1.1 for typicms/nestablecollection
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing typicms/nestablecollection (V1.1.18): Loading from cache
Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested.
Writing lock file
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/tinker
Discovered Package: laravel/ui
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Package manifest generated successfully.
48 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Category Model
You First Create category model
File name : Category.php
php artisan make:model Category -m
Add the NestableTrait in our model
use TypiCMS\NestableTrait;
File name : Category.php
<?php
namespace App;
use TypiCMS\NestableTrait;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use NestableTrait;
public $timestamps = true;
protected $fillable=['name','slug','parent_id','thumbnail','status'];
}
Call to undefined function str_slug()
composer require laravel/helpers
Now we are ready to use the nested collection based on our "parent_id" column
File name : CreateCategoriesTable.php
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name')->unique();
$table->string('slug')->unique();
$table->unsignedSmallInteger('parent_id');
$table->string('thumbnail')->nullable();
$table->enum('status',['1','0']);
$table->timestamps();
});
}
Route
File name : web.php
Route::get('add-categorymenu', 'admin\CategoryController@index');
Route::post('addcategorymenu', 'admin\CategoryController@add_categorymenu')->name('addcategorymenu');
Category Controller
File name : CategoryController.php
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Category;
class CategoryController extends Controller
{
public function index()
{
//$category = Category::get();
$category = Category::orderByRaw('-name ASC')->get()->nest()->listsFlattened('name');
return view('admin.add-categorymenu',compact('category'));
}
public function add_categorymenu(Request $request)
{
$data = $request->all();
$slug = str_slug($data['name']);
$obj = new Category;
$obj->name = $data['name'];
$obj->slug = $slug;
$obj->parent_id = $data['parent_id'];
$obj->save();
return redirect()->back()->with('msg',"Category Save");
}
}
View add-categorymenu
File name : add-categorymenu.blade.php
<form name="frm" id="frm" method="post" action="{{route('addcategorymenu')}}" enctype="multipart/form-data">
@csrf
<label for="name">Category Name</label>
<div class="form-group">
<input type="text" id="name" name="name" class="form-control" placeholder="Enter category Name" value="{{ old('name') }}">
@if ($errors->has('name'))
<span class="text-danger">
<strong id="name-error"> {{ $errors->first('name') }}</strong>
</span>
@endif
</div>
<label for="parent_category">Parent Category</label>
<div class="form-group">
<!--<select name="parent_id">
<option>--- Please Select --- </option>
@foreach($category as $row)
<option value="{{--$row->id--}}">{{--$row->name--}} </option>
@endforeach
</select>
-->
<select id="parent_id" class="form-control custom-select mt-15 @error('parent_id') is-invalid @enderror" name="parent_id">
<option value="0">Select a parent category</option>
@foreach($category as $key => $row)
<option value="{{ $key }}"> {{ $row }} </option>
@endforeach
</select>
@error('parent_id') {{ $message }} @enderror
@if ($errors->has('parent_category'))
<span class="text-danger">
<strong id="parent_category-error"> {{ $errors->first('parent_category') }}</strong>
</span>
@endif
</div>
<!--
<label for="logo">Choose Category Image.</label>
<div class="form-group">
<input type="file" id="image" name="image" class="form-control" placeholder="Enter Category Image Here..." value="{{ old('image') }}">
@if ($errors->has('image'))
<span class="text-danger">
<strong id="image-error"> {{ $errors->first('image') }}</strong>
</span>
@endif
</div>
-->
<button type="submit" class="btn btn-raised btn-primary btn-round waves-effect">Submit</button>
</form>
File name : index.php
Show Category View for Frontend
File name : index.php
Creating Category Controller for Frontend
File name : index.php
php artisan make:controller frontend\CategoryController
File name : index.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Category;
class CategoryController extends Controller
{
public function index()
{
//$categories = CategorySubcategory::orderBy('name','ASC')->where('status','1')->get();
$categories = Category::orderBy('name','ASC')
->where('parent_id', 1)
->first();
return view('frontend.index',compact('categories'));
}
}
Creating View Composer Service Provider
The best way to pass categories to our navigation.blade.php view is by using the View Composers
File name : index.php
php artisan make:provider ViewComposerServiceProvider
open this generate provider from app/Providers folder and replace the content
File name : ViewComposerServiceProvider.php
<?php
namespace App\Providers;
use App\Category;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
class ViewComposerServiceProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
View::composer('frontend.layouts.navigation', function ($view) {
$view->with('categories', Category::orderByRaw('-name ASC')->get()->nest());
});
}
}
Adding View Composer Service Provider to Providers Array
File name : config/app.php
App\Providers\ViewComposerServiceProvider::class,
Update navigation.blade.php File for Categories Navigation
File name : index.php
<div class="collapse navbar-collapse" id="main_nav">
<ul class="navbar-nav">
@foreach($categories as $cat)
@foreach($cat->items as $category)
@if ($category->items->count() > 0)
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="{{ route('category.show', $category->slug) }}" id="{{ $category->slug }}"
data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{ $category->name }}</a>
<div class="dropdown-menu" aria-labelledby="{{ $category->slug }}">
@foreach($category->items as $item)
<a class="dropdown-item" href="{{ route('category.show', $item->slug) }}">{{ $item->name }}</a>
@endforeach
</div>
</li>
@else
<li class="nav-item">
<a class="nav-link" href="{{ route('category.show', $category->slug) }}">{{ $category->name }}</a>
</li>
@endif
@endforeach
@endforeach
</ul>
</div>
https://www.larashout.com/categories-navigation
File name : index.php
File name : index.php
File name : index.php