【问题标题】:How can I return data from a foreign table when querying the USER in Laravel?在 Laravel 中查询 USER 时如何从外部表中返回数据?
【发布时间】:2015-09-25 08:44:11
【问题描述】:

例如,假设我的用户表有一个引用外部表(如 HEIGHT)的列。如果我使用:

return \Auth::user()->height_id;

在我的控制器中,我获得了 HEIGHT 表中引用的主键的 ID,但是如何检索外部​​表中引用的行内的数据?

【问题讨论】:

    标签: sql laravel eloquent key


    【解决方案1】:

    你应该试试

    return \Auth::user()->height()->YOUR_HEIGHT_TABLE_FIELD;
    

    这应该是你已经建立了UserHeight 模型之间的关系。
    Read More..

    【讨论】:

      【解决方案2】:

      每 cmets再次更新

      这取决于用户与身高的关系。

      我假设它是一个用户只有一个高度。如果是这种情况,您可以像这样设置您的用户和身高模型。

      用户

      class User extends Model {
          /**
           * The user's height
           * @see App\Height
           * @return \Illuminate\Database\Eloquent\Relations\HasOne
           */
          public function height()
          {
              // if you break naming conventions you can specify a different key.
              // I am unclear because I do not know your db structure.
              // from docs:
              // return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
              return $this->hasOne('App\Height');
          }
      }
      

      身高

      class Height extends Model {
          /**
           * The height of a user
           * @see App\Height
           * @return \Illuminate\Database\Eloquent\Relations\HasOne
           */
          public function user()
          {
              return $this->hasOne('App\User');
          }
      

      }

      控制器

      public function show($id)
      {
          $user = User::with('height')->firstOrFail($id);
          return view('yourview')->with(compact('user'));
      }
      

      查看

      {{ $user->height->height }}
      {{ $user->height->id }}
      

      无论你做什么,我都会阅读Eloquent: Relationships。应该会有所帮助。

      附录

      有几种方法可以做到这一点,我可以立即想到。这些都会导致两个查询,我很确定你可以用一个来实现。

      public function show($id)
      {
          $user_id = \Auth::user()->id;
          $user = User::with('height')->firstOrFail($user_id);
          return view('yourview')->with(compact('user'));
      }
      

      public function show($id)
      {
          $user = \Auth::user();
          $height = $user->height();
          return view('yourview')->with(compact('user'))->with(compact('height'));
      }
      

      public function show($id)
      {
          $user = \Auth::user();
          $height = Height::find($user->height_id);
          return view('yourview')->with(compact('user'))->with(compact('height'));
      }
      

      【讨论】:

      • 我得到未定义的属性:Illuminate\Database\Eloquent\Relations\HasOne::$height 但是该列在表中称为“高度”...
      • @Chriz74 根据您的评论更新。您应该查看文档,这些都在里面而且很容易阅读。
      • 我是否违反了命名约定?不应该是$this->belongsTo('App\User');
      • 如果你指的是另一个表,你想命名相关键单数表名+_id。所以在heights 表中你将拥有user_id,在users 表中你将拥有height_id
      • 是的,我在用户表“height_id”中有什么我在高度表中有“高度”。
      猜你喜欢
      • 1970-01-01
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      相关资源
      最近更新 更多