How to show data after login in laravel?

by muriel.schmidt , in category: PHP Frameworks , 10 months ago

How to show data after login in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , 10 months ago

@muriel.schmidt 

To show data after login in Laravel, you can create a route and controller method that retrieves the data to be displayed. Here's a general guideline on how to achieve this:

  1. Create a route for the page where you want to display the data after login. For example, in your web.php routes file:
1
Route::get('/dashboard', 'DashboardController@index')->name('dashboard');


  1. Create a controller named DashboardController by running the following command in your terminal:
1
php artisan make:controller DashboardController


  1. In the DashboardController, define a method (e.g., index) that retrieves the data you want to display and returns a view:
1
2
3
4
5
6
7
8
class DashboardController extends Controller
{
    public function index()
    {
        $data = // Retrieve the data from your database or any other source
        return view('dashboard', compact('data'));
    }
}


  1. Create a view file named dashboard.blade.php where you will display the data. You can access the $data variable passed from the controller in this file.
  2. In your login controller method (e.g., LoginController), redirect the user to the dashboard route upon successful login:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use IlluminateSupportFacadesAuth;

public function login(Request $request)
{
    // Your login logic here

    if (Auth::attempt($credentials)) {
        return redirect()->route('dashboard');
    }
    
    // Handle login failure
}


  1. Ensure that the user is authenticated before accessing the dashboard route by adding the auth middleware to the route:
1
Route::middleware(['auth'])->get('/dashboard', 'DashboardController@index')->name('dashboard');


That's it! After the user logs in successfully, they will be redirected to the dashboard route where the data will be displayed.

Related Threads:

How to show data after login in laravel?
How to show data from two related tables with laravel?
How to get customer login data from shopify?
How to redirect after login in Laravel?
How to overridden laravel default login function?
How to override auth login function in laravel?