2014-02-23 79 views
0

的財產,我努力理解如何laravel作品,我有一個非常困難的時間與它Laravel試圖讓非對象

型號 - user.php的用戶模型

<?php 

use Illuminate\Auth\UserInterface; 
use Illuminate\Auth\Reminders\RemindableInterface; 

class User extends Eloquent implements UserInterface, RemindableInterface { 

protected $fillable = array('email' , 'username' , 'password', 'code'); 
/** 
* The database table used by the model. 
* 
* @var string 
*/ 
protected $table = 'users'; 

/** 
* The attributes excluded from the model's JSON form. 
* 
* @var array 
*/ 
protected $hidden = array('password'); 

public function Characters() 
{ 
     return $this->hasMany('Character'); 
} 

/** 
* Get the unique identifier for the user. 
* 
* @return mixed 
*/ 
public function getAuthIdentifier() 
{ 
    return $this->getKey(); 
} 

/** 
* Get the password for the user. 
* 
* @return string 
*/ 
public function getAuthPassword() 
{ 
    return $this->password; 
} 

/** 
* Get the e-mail address where password reminders are sent. 
* 
* @return string 
*/ 
public function getReminderEmail() 
{ 
    return $this->email; 
} 

} 

模式 - Character.php人物模型

<?php 

    class Character extends Eloquent { 

protected $table = 'characters'; 

protected $fillable = array('lord_id','char_name', 'char_dynasty', 'picture'); 

public function user() 
    { 
     return $this->belongsTo('User'); 
    } 

public function Titles() 
    { 
     return $this->hasMany('Title'); 
    } 
} 

    ?> 

routes.php文件

Route::group(array('prefix' => 'user'), function() 
{ 

    Route::get("/{user}", array(
     'as' => 'user-profile', 
     'uses' => '[email protected]')); 

}); 

ProfileController.php

<?php 
    class ProfileController extends BaseController{ 



public function user($user) { 
    $user = User::where('username', '=', Session::get('theuser')); 

    $char = DB::table('characters') 
      ->join('users', function($join) 
      { 
       $join->on('users.id', '=', 'characters.user_id') 
        ->where('characters.id', '=', 'characters.lord_id'); 
      }) 
      ->get(); 

    if($user->count()) { 
     $user = $user->first(); 
     return View::make('layout.profile') 
     ->with('user', $user) 
     ->with('char', $char); 
    } 

    return App::abort(404); 
} 


} 

在我的代碼,我會重定向到這條路線有以下:

return Redirect::route('user-profile', Session::get('theuser')); 

在視圖我只想做: 歡迎回來,{{$用戶 - >用戶名}},你的主要字符是{{$ char-> char_name}}

我的問題是,我會收到此錯誤:嘗試獲取我的視圖中的非對象的屬性。我相信它指的是$ char-> char_name。出了什麼問題?我很難理解Laravel。我不知道爲什麼。提前致謝!

回答

0

您應該使用Auth類來獲取登錄用戶的會話信息。

$user = Auth::user(); 

$welcome_message = "Welcome back, $user->username, your main character is $user->Character->char_name"; 

您不需要將任何內容傳遞給該路由。只需檢查用戶是否登錄,然後檢索數據。您可以從應用程序的任何位置訪問此數據。

if (Auth::check()) 
{ 
    //the user is logged in 
    $user = Auth::user(); 

要回答你的問題的評論,閱讀documentation將解決所有這些問題,但是:

public function user() 
{ 
    if (Auth::check()) 
    { 
     $user = Auth::user(); 
     return View::make('rtfm', compact('user')); 
    } 
    else 
    { 
     return "The documentation explains all of this very clearly."; 
    } 
} 
+0

我怎樣才能將消息發送到視圖? –

+0

當我在我的視圖中編寫此代碼時,仍然收到相同的錯誤:歡迎回來,{{$ user-> username}},您的主角是{{$ user-> Character-> char_name}} –