@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:
- 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');
|
- Create a controller named DashboardController by running the following command in your terminal:
1
|
php artisan make:controller DashboardController
|
- 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'));
}
}
|
- 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.
- 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
}
|
- 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.