Pass Data Route to View in Laravel 11

 function getUsers()

{
    return [
        1 => ["name" => "Amitabh", "phone" => "9123456789", "city" => "Goa"],
        2 => ["name" => "Salman", "phone" => "9123456789", "city" => "Delhi"],
        3 => ["name" => "Sunny", "phone" => "9123456789", "city" => "Mumbai"],
        4 => ["name" => "Akshay", "phone" => "9123456789", "city" => "Agra"]
    ];
}


Route::get("/users", function () {
    $nameTwo = "Next time we will play game in the morning.";

    $names = getUsers();
    // Below is the First way for passing the data Route to View :
    return view("users", ["name" => "Hello World", "nameslist" => $names, "namepresent" => $nameTwo, "city" => "Ahmedabad", "party" => "<script>alert('Hello, How are you ?');</script>"]);

    // Below is the Second way for passing the data Route to View :
    // return view("users")->with("name", "Hello World")->with("namepresent", $nameTwo)->with("city", "Ahmedabad")->with("party", "<script>alert('Hello, How are you ?');</script>");

    // Below is the Third way for passing the data Route to View :
    // return view("users")->withName("Hello World")->withNamepresent($nameTwo)->withCity("Ahmedabad")->withParty("<script>alert('Hello, How are you ?');</script>");
})->name("usershere");


Route::get("/user/{id}", function ($id) {
    $users = getUsers();
   
    abort_if(!isset($users[$id]), 404);

    $user = $users[$id];

    return view("user", ["id" => $user]);
    // return "<h1>User : " . $id . "</h1>";
})->name("view.user");

Above File is routes/web.php File






Below File is users.blade.php File
<h1>Users Page</h1>
<h1>How are you {{ $name }}</h1>
<h2>Hii {{ $namepresent }}</h2>
<h1>City : {{ !empty($city) ? $city : "No city" }}</h1>
{{-- <h2>Party : {!! $party !!}</h2> --}}

@foreach($nameslist as $id => $value)
    <h2>{{ $id }} : {{ $value["name"] }} | {{ $value["phone"] }} | {{ $value["city"] }} | <a href="{{ route('view.user', $id) }}">Show</a> </h2>
@endforeach




Below File is user.blade.php File
<h1>User Detail</h1>

<h2>Name : {{ $id["name"] }}</h2>
<h2>Phone : {{ $id["phone"] }}</h2>
<h2>City : {{ $id["city"] }}</h2>



Comments

Popular posts from this blog

Eloquent Many to Many Relationship Tutorial in Laravel 11

Eloquent with JSON Data Columns Tutorial in Laravel 11

Blade Template Tutorial Three Template Inheritance in Laravel 11