2015-07-13 147 views
1

登录后会重定向到http://localhost/laravel/public/homeLaravel 5主页控制器

所以查看文件的位置将被\资源\意见\ home.blade.php。

现在这个主页上我可以在使用

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

控制器创建成功获得登录用户ID

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

现在我已创建控制器。

现在,在首页我必须显示一些数据的基础上登录用户。

在家庭控制器,如果我做回声退出,但它不工作。那么在哪个控制器文件中我必须编写代码?

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) 
    { 
     // 
    } 
} 
+0

显示您的家庭控制器 –

+0

已添加问题。 –

+0

这只是我还是这个问题真的含糊不清?我假设你有你的路线工作,你的中间件设置,你有你的观点返回,但**你只是想从'Auth :: user()** **检索数据? – ash

回答

0

您可以访问用户数据这样

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

例如:

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

Thah我已经有了。谢谢。我需要使用控制器。因为根据用户ID我必须从另一个表格取数据 –

+0

Eloquent为您提供了不使用控制器的好方法。你可以这样做:$ user-> your_table,如果你之前在模型中定义了一段关系。我强烈建议你。检查文档:http://laravel.com/docs/4。2 /雄辩#关系 – suarsenegger

+0

如果您阅读文档,也请检查它的版本。 – ash

0

首先你可能需要创建路由的路由.php

Route::get('laravel/public/home', [ 
'as' => 'home', 'uses' => '[email protected]' 
]); 

然后在您的HomeController的index()函数中添加代码。

0

在您的HomeController.php中添加下面的内容来利用它。

use Auth; 

所以,你HomeController.php看起来像这样

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

而现在,你可以从你的控制器返回的loggedIn用户的ID做return Auth::user()->id

它看起来像这样

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

注:

你只能看到用户是否得到认证的Auth::user()->id