Eloquent ORM Create Data Tutorial in Laravel 11
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$users = User::all();
// return $users; //Here returns json data
// $users = User::find(2); //Here find() method doesn't return JSON, find() method returns Array
// $users = User::find(2,["name","email"]);
// $users = User::find([2,4],["name","email"]);
// $users = User::count();
// $users = User::min("age"); //min() and max() methods works with integer data Type Columns
// $users = User::max("age");
// $users = User::sum("age");
// $users = User::where("city","Delhi")->get();
// $users = User::where("city","Delhi")->where("age",">",20)->get(); //we can write this query as shown below(both works same):
// $users = User::where([
// ["city", "Delhi"],
// ["age", ">", 20]
// ])->get();
// $users = User::where("city","Delhi")->orWhere("age",">",20)->get();
// $users = User::where("city","Delhi")->orWhere("age",">",20)->count();
// $users = User::whereCity("Delhi")->get();
// $users = User::whereCity("Delhi")->whereAge(20)->get();
// $users = User::whereCity("Delhi")->whereAge(20)->select("name","email")->get();
// $users = User::whereCity("Delhi")->whereAge(20)->select("name","email as User Email Address")->get(); //Rename Column Name
// $users = User::whereCity("Delhi")->whereAge(20)->select("name","email as User Email Address")->toSql(); //Getting SQL Query
// $users = User::whereCity("Delhi")->whereAge(20)->select("name","email as User Email Address")->toRawSql();
// $users = User::whereCity("Delhi")->whereAge(20)->select("name","email as User Email Address")->dd();
// $users = User::whereCity("Delhi")->whereAge(20)->select("name","email as User Email Address")->ddRawSql();
// $users = User::whereCity("Delhi")->first();
// $users = User::where("Age","<>",20)->get(); //we can write this Not operator Sql command as shown below(Both works same):
// $users = User::whereNot("Age",20)->get();
// $users = User::whereBetween("Age",[20,22])->get(); // We can use Range data
// $users = User::whereNotBetween("Age",[20,22])->get();
// $users = User::whereIn("city",["Delhi","Goa"])->get();
// $users = User::whereNotIn("city", ["Delhi", "Goa"])->get();
// return $users;
// foreach($users as $data) {
// echo $data->name . "<br>";
// }
return view("home", compact("users"));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view("adduser");
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
// Method 1 : Insert data into database
// $user = new User;
// $user->name = $request->username;
// $user->email = $request->useremail;
// $user->age = $request->userage;
// $user->city = $request->usercity;
// $user->save();
// Method 2 : Mass Assignment for insert data into database
User::create([
"name" => $request->username,
"email" => $request->useremail,
"age" => $request->userage,
"city" => $request->usercity
]);
return redirect()->route("user.index")->with("status", "New User Added Successfully.");
}
/**
* Display the specified resource.
*/
public function show(string $id)
{
$users = User::find($id);
return view("viewuser", compact("users"));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(User $user)
{
return view("updateuser");
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, User $user)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(User $user)
{
//
}
}
Above File is app\Http\Controllers\UserController.php FileBelow File is app\Models\User.php File
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasFactory;
public $timestamps = false;
protected $fillable = ["name", "email", "age", "city"];
}
Below File is database\migrations\2024_11_29_114857_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");
$table->integer("age");
$table->string("city");
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users');
}
};
Below File is resources\views\adduser.blade.php File
@extends("layout")
@section("title")
Add New User
@endsection
@section("content")
<form action="{{ route("user.store") }}" method="POST">
@csrf
<div class="mb-3">
<label for="username" class="form-label">User Name</label>
<input type="text" class="form-control" name="username">
</div>
<div class="mb-3">
<label for="useremail" class="form-label">User Email</label>
<input type="email" class="form-control" name="useremail">
</div>
<div class="mb-3">
<label for="username" class="form-label">User Age</label>
<input type="number" class="form-control" name="userage">
</div>
<div class="mb-3">
<label for="usercity" class="form-label">User City</label>
<input type="text" class="form-control" name="usercity">
</div>
<div class="mb-3">
<input type="submit" value="Save" class="btn btn-success">
</div>
</form>
@endsection
Below File is resources\views\home.blade.php File
@extends("layout")
@section("title")
All Users Data
@endsection
@section("content")
<a href="{{ route("user.create") }}" class="btn btn-success btn-sm mb-3">Add New</a>
<table class="table table-striped table-bordered table-hovered mt-5">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>City</th>
<th>View</th>
<th>Delete</th>
<th>Update</th>
</tr>
@foreach($users as $data)
<tr>
<td>{{ $data->id }}</td>
<td>{{ $data->name }}</td>
<td>{{ $data->email }}</td>
<td>{{ $data->age }}</td>
<td>{{ $data->city }}</td>
<td><a href="{{ route("user.show",$data->id) }}" class="btn btn-primary btn-sm">View</a></td>
<td><a href="" class="btn btn-danger btn-sm">Delete</a></td>
<td><a href="{{ route("user.edit",$data->id) }}" class="btn btn-warning btn-sm">Update</a></td>
</tr>
@endforeach
</table>
@endsection
Below File is resources\views\layout.blade.php File
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewpot" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Eloquent ORM</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">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-8 bg-warning-subtle mb-3">
<h4>@yield("title")</h4>
</div>
</div>
<div class="row">
<div class="col-8">
@if(session("status"))
<div class="alert alert-success">
{{ session("status") }}
</div>
@endif
</div>
</div>
<div class="row">
<div class="col-7">
@yield("content")
</div>
</div>
</div>
</body>
</html>
Below File is resources\views\updateuser.blade.php File
@extends("layout")
@section("title")
Update User Data
@endsection
@section("content")
<form action="" method="POST">
<div class="mb-3">
<label for="username" class="form-label">User Name</label>
<input type="text" class="form-control" name="username">
</div>
<div class="mb-3">
<label for="useremail" class="form-label">User Email</label>
<input type="email" class="form-control" name="useremail">
</div>
<div class="mb-3">
<label for="username" class="form-label">User Age</label>
<input type="number" class="form-control" name="userage">
</div>
<div class="mb-3">
<label for="usercity" class="form-label">User City</label>
<input type="text" class="form-control" name="usercity">
</div>
<div class="mb-3">
<input type="submit" value="Save" class="btn btn-success">
</div>
</form>
@endsection
Below File is resources\views\viewuser.blade.php File
@extends("layout")
@section("title")
User Detail
@endsection
@section("content")
<table class="table table-striped table-bordered">
<tr>
<th width="80px">Name : </th>
<td>{{ $users->name }}</td>
</tr>
<tr>
<th>Email : </th>
<td>{{ $users->email }}</td>
</tr>
<tr>
<th>Age : </th>
<td>{{ $users->age }}</td>
</tr>
<tr>
<th>City : </th>
<td>{{ $users->city }}</td>
</tr>
</table>
<a href="{{ route("user.index") }}" class="btn btn-danger">Back</a>
@endsection
Below File is routes\web.php File
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
Comments
Post a Comment