Eloquent ORM Read 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("welcomeTwo", compact("users"));
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(User $user)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(User $user)
    {
        //
    }

    /**
     * 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 File





Below 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;
}






Below File is resources\views\welcomeTwo.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-7">
                <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>
                    </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>
                        </tr>
                    @endforeach
                </table>
            </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::resource("/user", UserController::class);








Comments

Popular posts from this blog

Eloquent Many to Many Relationship Tutorial in Laravel 11

Blade Template Tutorial Three Template Inheritance in Laravel 11