【问题标题】:Laravel Trying to get property of non-objectLaravel试图获取非对象的属性
【发布时间】:2014-03-25 10:17:51
【问题描述】:

我很难理解 laravel 的工作原理,而且我很难用它

Model - 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;
}

}

Model - 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' => 'ProfileController@user'));

});

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'));

在视图中我只想做: 欢迎回来,{{ $user->username }},你的主角是 {{ $char->char_name }}

我的问题是我会收到这个错误:尝试在我的视图中获取非对象的属性。我确定它指的是 $char->char_name。怎么了?我很难理解 Laravel。我不知道为什么。提前致谢!

【问题讨论】:

    标签: laravel properties eloquent laravel-routing


    【解决方案1】:

    您应该使用 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();
    

    要在 cmets 中回答您的问题,阅读 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.";
        }
    }
    

    【讨论】:

    • 我将如何将消息发送到视图?
    • 当我在我看来写这个时仍然收到同样的错误:欢迎回来,{{$user->username}},你的主角是 {{$user->Character->char_name} }
    猜你喜欢
    • 2015-09-22
    • 2017-03-20
    • 2015-01-25
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多