2014-02-23 55 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}} –