skip() and take() method tutorial in laravel 11
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
// we have to use skip() method with take() method compulsary simultaneously always
// But when we use take() method at that time we don't need to use skip() method simultaneously
public function indexTwo()
{
// $user = User::skip(4)->take(2)->get(); //Here first four records will skip and then fifth and sixth records will fetch
$user = User::skip(2)->take(2)->get(); //Here first two records will skip and then third and fourth records will fetch
echo "<pre>";
print_r($user);
}
}
Above File is app\Http\Controllers\UserController.php FileBelow File is app\Models\User.php File
<?php
namespace App\Models;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory, Notifiable;
public $timestamps = true;
protected $table = "users";
/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'email',
];
/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
];
}
}
Below File is database\json\users.json File
[
{
"name": "Sandeep",
"email": "sandeep@gmail.com"
},
{
"name": "Rajesh",
"email": "rajesh@gmail.com"
},
{
"name": "Durga",
"email": "durga@gmail.com"
},
{
"name": "Himesh",
"email": "himesh@gmail.com"
},
{
"name": "Sanjeev",
"email": "sanjeev@gmail.com"
},
{
"name": "Parminder",
"email": "parminder@gmail.com"
},
{
"name": "Kalpna",
"email": "kalpna@gmail.com"
},
{
"name": "Nisha",
"email": "nisha@gmail.com"
},
{
"name": "Aditya",
"email": "aditya@gmail.com"
},
{
"name": "Narayan",
"email": "narayan@gmail.com"
}
]
Below File is database\migrations\2025_01_08_110121_create_users_table.php File
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string("name");
$table->string("email", 50)->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users');
}
};
Below File is database\seeders\DatabaseSeeder.php File
<?php
namespace Database\Seeders;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
// User::factory(10)->create();
// User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
$this->call([
UserSeeder::class
]);
}
}
Below File is database\seeders\UserSeeder.php File
<?php
namespace Database\Seeders;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\File;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$json = File::get(path: "database\json\users.json");
$users = collect(json_decode($json));
$users->each(function ($user) {
User::create([
"name" => $user->name,
"email" => $user->email
]);
});
}
}
Below File is resources\views\users.blade.php File
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Users</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<div class="row justify-content-center">
<div class="col-10">
<div class="card">
<div class="card-header">
<h3>Users Data</h3>
</div>
<div class="card-body">
<form action="{{ route('deleteUsers') }}" method="POST">
@csrf
<table class="table table-bordered table-striped table-hovered">
<thead>
<th>Delete</th>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Created At</th>
<th>Updated At</th>
</thead>
@if(count($data) > 0)
@foreach($data as $user)
<tr>
<td>
<input type="checkbox" name="ids[{{ $user->id }}]" value="{{ $user->id }}"/>
</td>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->created_at }}</td>
<td>{{ $user->updated_at }}</td>
</tr>
@endforeach
@else
<tr>
<td colspan="6" class="text-center fw-bold text-danger">Users not found</td>
</tr>
@endif
</table><br>
<input type="submit" value="Delete Users" class="btn btn-danger"/>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Below File is routes\web.php File
<?php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::controller(UserController::class)->group(function () {
Route::get("/users", "index");
Route::post("/delete-users", "deleteUsers")->name("deleteUsers");
Route::get("/get", "indexTwo")->name("indexTwo");
});
.png)
.png)
Comments
Post a Comment