Setup Menus in Admin Panel

  • No products in the cart.

  • LOGIN

Creating Category and News Manager

Eloquent ORM

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding “Model” which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

By default for User model is already created by laravel.
User.php

Suppose if you need to render the data from the User you have to perform the following steps:

Step 1:
Go to the directory app/Http/Admin/UserController.php and you have to define the data type as in figure below:

$data = [];
$data['rows'] = User::all();
dd($data);

Note:
This is for selecting all the data from the Users table.

 

Step 2:
For selecting the specfic data to show.

$data = [];
$data['rows'] = User::select('id', 'created_at', 'email','username','contact_no')->get();
return view('admin.user.index',compact('data'));

compact(‘data’) will assign the value of the $data.We will pass a data to view by using compact method.

 

You have to follow this pattern as in screenshot:

 

Code:

public function index()
{
    $data = [];
    $data['rows'] = User::select('id', 'created_at', 'email','username','contact_no','status')->get();
    return view('admin.user.index',compact('data'));
  }

 

Now, we have to make the user table dynamic. So, go to the directory resources/views/admin/user/index.blade.php
and remove the table rows except one

Remove the highlighted part as in screenshot below:

and perform the foreach statement for making the table dynamic.

So go to the directory resources/views/admin/user/index.blade.php
and perform the foreach statement as:

@foreach($data['rows'] as $key => $key)
<tr>
....
</tr>

@endforeach

 

Screenshot:

Still you have the static data. Now you have to make the data dynamic as in screenshot below:

 

Screenshot:

 

Dashboard Screenshot:

Now making the breadboard dynamic:

Screenshot:

 

 

Add User form:

Step 1:
Similar to user lists make the path dynamic

Screenshot:

 

Step 2:
Open the form-elements.html and copy and paste the required form section to add.blade.php.

Screenshot:

 

Screenshot user form

 

 

Creating category manager:
Similar to the user manager create a route path for category manager.
Step 1.Go to web.php and create a route

Route::get('category', [ 'as'=> 'category', 'uses' => 'Admin\[email protected]']);
Route::get('category/add', [ 'as'=> 'category.add', 'uses' => 'Admin\[email protected]']);
Route::post('category/store', [ 'as'=> 'category.store', 'uses' => 'Admin\[email protected]']);
Route::get('category/edit/{id}', [ 'as'=> 'category.edit', 'uses' => 'Admin\[email protected]']);
Route::post('category/update/{id}', [ 'as'=> 'category.update', 'uses' => 'Admin\[email protected]']);
Route::get('category/delete/{id}', [ 'as'=> 'category.delete', 'uses' => 'Admin\[email protected]']);

Step 2.
Go to the directory resources/views/includes/sidebar.blade.php and copy the User Manager section and paste below it for creating the category manager and change the directory.

Step 3.Creating controller class.
Go to the directory app/Http/Controller/Admin/ and Create the category controller (i.e CategoryController.php) which is similar to the UserController.php.

Step 4.Creating view for Category.
Go to the directory resources/views/admin/ then copy and paste the user folder and rename as category and give the necessary url path for category.

Step 5.
By default we had User model but for Category we have to create the model for category.
Go to the directory app and create a folder name as Models.
Now we can create a model by using the following artisan command:

php artisan make:model Models/Category

Screenshot:

 

Directory:app/Models/Category.php

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories';
protected $fillable = ['title', 'slug', 'image','status'];
}

Screenshot for edit and store:

 

Submiting a form

user
add.blade.php

<form action="{{ route('user/add') }}" method="POST" class="form-horizontal" role="form">     line 196

usercontroller.php

public function add(Request $request)
{
$this->validate($request, [
'username' => 'required',
'email' =>'required',
'contact_no' => 'required'
]);
return view('admin.user.add');
}

———————

views admin user index   line->8-10

<script type="text/javascript">
try{ace.settings.check('breadcumbs' , 'fixed')} catch (e) {}
</script>

—————-

front end validation:

for image uploading
at the end of the category/add.blade.php
add the following section

@section('js')

<script src="{{ asset('admin_panel/assets/js/jquery.validate.min.js') }}"></script>

@endsection

For this we have to give the id name in the form section

id="validation-form"

Screenshot:

{!! Form::open(['route' => 'admin.category.store', 'class' =>'form-horizontal','id'=>'validation-form']) !!}

and close the form tag
{!! Form::close !!}

 

Screenshot

Now we will bind the jQuery

Creating a request

php artisan make:request Category/EditFormValidation

This command is creating a class inside “app/Http/Requests/Category” directory.

 

News Manager

Step 1:
Creating route

Route::get('news', [ 'as'=> 'news', 'uses' => 'Admin\[email protected]']);
Route::get('news/add', [ 'as'=> 'news.add', 'uses' => 'Admin\[email protected]']);
Route::post('news/store', [ 'as'=> 'news.store', 'uses' => 'Admin\[email protected]']);
Route::get('news/edit/{id}', [ 'as'=> 'news.edit', 'uses' => 'Admin\[email protected]']);
Route::post('news/update/{id}', [ 'as'=> 'news.update', 'uses' => 'Admin\[email protected]']);
Route::get('news/delete/{id}', [ 'as'=> 'news.delete', 'uses' => 'Admin\[email protected]']);

Step2:
NewsController
copy and paste CategoryController

NewsController.php

protected $base_route = 'admin.new';
protected $view_path = 'admin.new';
protected $folder_name = 'news';
protected $panel = 'News';
protected $folder_path;

then create a folder name news in the directory public/images

Step 3:
Create a model for news

Step 4:
Install ckeditor

composer require unisharp/laravel-ckeditor

Creating a custom middleware:

php artisan make:middleware CustomAuth

 

Point the middleware:

'middleware' => 'auth-custom' to handle

CustomAuth.php

<?php

namespace App\Http\Middleware;

use Closure;

class CustomAuth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(auth()->check()){
return $next($request);
}else{
return redirect()->route('login');
}
dd('in middleware..');
return $next($request);
}
}


Attachments15

SEE ALL Add a note
YOU
Add your Comment
 

Advanced Course Search Widget

Popular Courses

Course Categories

© PHPhelp. 2002 - 2018 All rights reserved.
X