Monday, May 17, 2021

How to pass data to the view from the route in Laravel ??

 We can pass the data to the view using help function 

  1. with ( )
  2. compact ( )

Using With 

Route::get('/greet'function () {
   $name = 'Dev';
    // declaring and assigning a value to the variable 
    return view ('greeting')->with('name'$name); // pass the variable to the view 
});
               

HTMl


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Greeting</title>
</head>
<body>
    <h1>Good Morning: <?= $name ?></h1> // calling the variable

    <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. 
        Rem repudiandae vel hic natus quo iure reprehenderit esse possimus
         illum placeat magni labore dolorem eveniet quasi, aut optio, dolor 
            harum quibusdam.</p>
</body>
</html>

Out Put























By using compact( ).

 Route::get('/greet'function () {
    $name = 'Dev <span style = "color:red">coder</span>';
    $post = 'coding';
     // declaring and assigning a value to the variable 
     return view ('greeting',compact'name','post'));
 });

html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Greeting</title>
</head>
<body>
    <h1>Good Morning: <?= $name ?></h1> <!-- php syntax  with out escaping the css  -->

    <h1>Good Morning: {{ $name }}</h1> <!-- laravel syntax escaping -->
    
    <h1>Good Morning:{!! $name !!}-{!! $post !!}</h1> <!-- laravel syntax  with out escaping the css  -->

    <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit.
         Rem repudiandae vel hic natus quo iure reprehenderit esse possimus illum placeat magni labore dolorem eveniet quasi, aut optio, dolor harum quibusdam.</p>
</body>
</html>

output





























 

       


 

                                                                                                                                                                                      

No comments:

Post a Comment

Call Factory and make multiple record entry Using tinker

  $d = Factory(App\Department::class,3)->create(); > $d = Factory(App\Department::class, 10)->create(); = Illuminate\Database\Eloqu...