User crud operation

In this article, I will show you what CRUD means, and what the individual terms mean and do. I will also show you how create, read, update, and delete operations work in the real world.

Controller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use Auth;

class SubscriberController extends Controller
{


function user_store(Request $req){
$name = $req->name;
$email = $req->email;
$password = $req->password;
$confirm_password = $req->confirm_password;
$phone = $req->phone;
$role = $req->role;

$validated = $req->validate([
'name'=>'required',
'email'=>'required',
'role' => 'required',
'password' => 'required',
]);

$count = DB::table('users')->where('email', $email)->count();

if($count){
return back()->with('msg2', 'Email Already Exists !');
}

if($confirm_password != $password){
return back()->with('msg2', 'Password did not matched !');
}


$query = DB::table('users')->insert([
'name' => $name,
'email' => $email,
'role' => $role,
'password' => Hash::make($password),
'phone' => $phone,
]);

return back()->with('msg','Your data has been saved !');

}




function edituser(Request $req, $id){
return view('admin.users.edit', ['user_id' => $id]);
}

function updateuser(Request $req){

$name = $req->name;
$email = $req->email;
$password = $req->password;
$confirm_password = $req->confirm_password;
$phone = $req->phone;
$role = $req->role;
$id = $req->user_id;

$validated = $req->validate([
'name'=>'required',
'email'=>'required',
'role' => 'required',
'password' => 'required',
]);


$query = DB::table('users')->where('id', $id)->update([
'name' => $name,
'email' => $email,
'role' => $role,
'password' => Hash::make($password),
'phone' => $phone,
]);

return back()->with('msg','Your data has been updated !');

}

function deleteuser(Request $req, $id){
$query = DB::table('users')->where('id', $id)->delete();
return back()->with('msg','Your data has been deleted !');
}



}
add-user.blade.php

<nav class="sidebar">
<div class="logo d-flex justify-content-between">
<a class="large_logo" href="{{route('dashboard')}}"><img src="{{asset('/')}}img/logo.png" alt=""></a>
<a class="small_logo" href="{{route('dashboard')}}"><img src="{{asset('/')}}img/mini_logo.png" alt=""></a>
<div class="sidebar_close_icon d-lg-none">
<i class="ti-close"></i>
</div>
</div>


<ul id="sidebar_menu" class="metismenu">

<li class="">
<a href="{{route('dashboard')}}">
<div class="nav_icon_small">
<img src="{{asset('/')}}img/menu-icon/dashboard.svg" alt="">
</div>
<div class="nav_title">
<span>Dashboard</span>
</div>
</a>
</li>

<li class="">
<a class="has-arrow" href="#" aria-expanded="false">
<div class="nav_icon_small">
<img src="{{asset('/')}}img/menu-icon/4.svg" alt="">
</div>
<div class="nav_title">
<span>User Management</span>
</div>
</a>
<ul class="mm-collapse">
<li><a href="{{route('all-users')}}">All Users</a></li>
<li><a href="{{route('add-users')}}">Add Users</a></li>
</ul>
</li>


<li class="">
<a class="has-arrow" href="#" aria-expanded="false">
<div class="nav_icon_small">
<img src="{{asset('/')}}img/menu-icon/11.svg" alt="">
</div>
<div class="nav_title">
<span>Counsellor Management</span>
</div>
</a>
<ul class="mm-collapse">
<li><a href="">All Counsellor</a></li>
<li><a href="">Add Counsellor</a></li>
</ul>
</li>


<li class="">
<a href="#">
<div class="nav_icon_small">
<img src="{{asset('/')}}img/menu-icon/8.svg" alt="">
</div>
<div class="nav_title">
<span>Contact Form</span>
</div>
</a>
</li>


<li class="">
<a class="has-arrow" href="#" aria-expanded="false">
<div class="nav_icon_small">
<img src="{{asset('/')}}img/menu-icon/7.svg" alt="">
</div>
<div class="nav_title">
<span>Plan Management</span>
</div>
</a>
<ul class="mm-collapse">
<li><a href="">All Plans</a></li>
<li><a href="">Add Plan</a></li>
</ul>
</li>


<li class="">
<a href="#">
<div class="nav_icon_small">
<img src="{{asset('/')}}img/menu-icon/10.svg" alt="">
</div>
<div class="nav_title">
<span>Payment Gaytways</span>
</div>
</a>
</li>


</ul>
</nav>
all-users.blade.php

@extends('common.master')
@section('title', 'All Users')
@section('content')

<body class="crm_body_bg">

@include('admin.common.sidebar')
<section class="main_content dashboard_part large_header_bg">
@include('admin.common.header')

<div class="container-fluid p-0">
<div class="row justify-content-center">
<div class="col-lg-12">
<div class="white_card card_height_100 mb_30">
<div class="white_card_header">
<div class="box_header m-0">
<div class="main-title">
<!-- <h3 class="m-0">All Users</h3> -->
</div>
</div>
</div>
<div class="white_card_body">
<div class="QA_section">
<div class="white_box_tittle list_header">
<h4 style="visibility:hidden;">shorting Arrow</h4>

<div class="box_right d-flex lms_block">
<div class="serach_field_2" style="display:none;">
<div class="search_inner">

<form active="#">
<div class="search_field">
<input type="text" placeholder="Search content here...">
</div>
<button type="submit"> <i class="ti-search"></i> </button>
</form>
</div>
</div>

</div>
</div>

<div style="position:relative; float:right;">
<a href="{{route('add-users')}}" class="btn btn-dark btn-sm">Add Users</a>
</div>

<h4 class="m-0">All Users</h4>
<div class="QA_table mb_30">
<div id="DataTables" class="dataTables_wrapper no-footer"><table class="table lms_table_active3 dataTable no-footer dtr-inline" id="DataTables_Table_1" role="grid" aria-describedby="DataTables_Table_1_info" style="width: 1142px;">
<thead>
<tr role="row"><th scope="col" class="sorting_asc" tabindex="0" aria-controls="DataTables_Table_1" rowspan="1" colspan="1" style="width: 127.2px;" aria-sort="ascending" aria-label="title: activate to sort column descending">S.No.</th><th scope="col" class="sorting" tabindex="0" aria-controls="DataTables_Table_1" rowspan="1" colspan="1" style="width: 166px;" aria-label="Category: activate to sort column ascending">Name</th><th scope="col" class="sorting" tabindex="0" aria-controls="DataTables_Table_1" rowspan="1" colspan="1" style="width: 158px;" aria-label="Teacher: activate to sort column ascending">Email</th><th scope="col" class="sorting" tabindex="0" aria-controls="DataTables_Table_1" rowspan="1" colspan="1" style="width: 155px;" aria-label="Lesson: activate to sort column ascending">Phone</th><th scope="col" class="sorting" tabindex="0" aria-controls="DataTables_Table_1" rowspan="1" colspan="1" style="width: 98px;" aria-label="Enrolled: activate to sort column ascending">Role</th>
<th scope="col" class="sorting" tabindex="0" aria-controls="DataTables_Table_1" rowspan="1" colspan="1" style="width: 139.2px;" aria-label="Status: activate to sort column ascending">Status</th></tr>

</thead>

<tbody>

<?php 
$usersArr = DB::table('users')->where('id', '!=', auth()->user()->id)->get();
foreach($usersArr as $key => $users){
?>

<tr role="row" class="odd">
<th scope="row" tabindex="0" class="sorting_1">{{$key+1}}</th>
<td>{{$users->name}}</td>
<td>{{$users->email}}</td>
<td>{{$users->phone}}</td>
<td>{{$users->role}}</td>

<td>
<a href="/user/edit-user/{{$users->id}}" class="status_btn">Edit</a> &nbsp; 
  <a href="/user/delete-user/{{$users->id}}" class="status_btn" style="background:red">Delete</a>
</td>
</tr>

<?php 
}
?>

</tbody>
</table>

</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

<script>

$(document).ready(function() {
    $('#DataTables').dataTable({
        "scrollX": false,
        "pagingType": "numbers"
    } );
} );

</script>

<style>

td, tr {
  text-align:center;
}

</style>

@include('admin.common.footer')

</section>

</body>

@endsection

 

 

 

 

 

 

 

 

 

 

Leave a Reply