Laravel 5主页控制器

After login it will redirect to http://localhost/laravel/public/home

So view file location will be esources\views\home.blade.php.

Now on this home page i'm able to get login user id

<?php
echo $id = Auth::id();
?>

now i have created controller using

D:\wamp\www\laravel>php artisan make:controller HomeController

Controller created successfully.

Now on home page i have to show some data on basis of logged in user.

In home controller if i do echo exit, but it's not working. So in which controller file i have to write code?

HomeController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class HomeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        //
    }
}

You can access to the user data this way

 {{ Auth::user()->field_name }}

For example:

{{ Auth::user()->id }}
{{ Auth::user()->name }}
{{ Auth::user()->email }}

first you may need to create a route in routes.php

Route::get('laravel/public/home', [
'as' => 'home', 'uses' => 'HomeController@index'
]);

and then add code in your HomeController's index() function.

In your HomeController.php add the below lines to make use of it.

use Auth;

So, Your HomeController.php will look like this

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;

And Now, you can do return Auth::user()->id from your controller to return the Loggedin user's Id.

It will look like this

public function index()
{
return Auth::user()->id;
}

Note :

You can see the Auth::user()->id only if the user is get authenticated.