Return view from route Laravel
Return view from route Laravel
In this session, we will try our hand at solving the "Return view from route Laravel".
You can return to view from route in Laravel. You have to create a web route with the call back function and use return view('home') in callback to return to view file or you can also use Route::view() method to return to view file in Laravel-
Return view from route using return::view() method
//routes\web.php //Route definition Route::get('/home', function () { return view('home'); }); //resources\views\home.blade.php <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Bootstrap CSS --> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous"> <title>Home !</title> </head> <body> <h1> Home page </h1> </body> </html>
You can return to view file in laravel using return view() method in callback function while defining the web route in php
-
Return data to view using route::view method in Laravel
//routes\web.php Route::view('/welcome', 'welcome', ['id' => 1]); //resources\views\welcome.blade.php <!doctype html> <html lang="en"> <head> <!-- Required meta tags --> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Bootstrap CSS --> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous"> <title>Welcome !</title> </head> <body> <h1> {{ $id }} </h1> </body> </html>
You can use Route::view() method in web.php file while defining the web route and return to view (passed as second agruments to route::view method) without any callback function with data which is passed as third arguments to route definition.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Eager loading dynamically in laravel
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- If condition in foreach loop in laravel
- Count all and get 10 records after where condition in laravel
- Command to create MySQL Docker image and access the MySQL command-line interface (CLI) within a running Docker container
- Always load the relationship data with eager loading in Laravel
- How to automatically update the timestamp of parent model in Laravel
- Rename Pivot Table in Laravel
- Setup laravel project with docker
- Get last record from table in laravel
- How to display user profile after login in laravel
- How to check query string exists or not in laravel blade
- Send OTP using textlocal api in laravel
- How to get images from AWS s3 and display in Laravel blade
- How to restore deleted records in laravel
- Insert current date time in a column using Laravel
- Best code example for create order in Laravel for ecommerce
- Target class [admin] does not exist.
- Display first n record from collection in laravel view
- Conditional validation in laravel
- Update if exist else insert new record in laravel
- How to pass data to multiple partial view files in laravel
- How to call model in blade laravel
- Add [name] to fillable property to allow mass assignment on [App\Models\Project]
- Get last week data in Laravel