【问题标题】:Laravel 4 Eager Loading Multiple Table and Easy Access To The ViewLaravel 4 渴望加载多个表并轻松访问视图
【发布时间】:2014-11-16 07:06:07
【问题描述】:

大家好,我是 Laravel 4 PHP 框架的新手,我只知道一点。只是想问一下如何急切加载 3 个表(或更多)并为视图提供一种干净的方式访问变量(我有一个表用户、配置文件和状态)

**Users Table**
id (int) ai
code (varchar)
email_id (int) ref to email table
username (varchar)
password (varchar)
softdeletes, timestamps, remember token laravel defaults

**Profile Table**
id (int) ai
user_id (int) index ref to users table
lastname (varchar)
firstname (varchar)
middlename (varchar)
birthdate (date)
and more...
laravel defaults...

**Statuses Table**
id (int) ai
user_id (int) index ref to users table
message (text)
laravel defaults...

laravel 提供的 User 模型我只是添加了一个公共函数配置文件和状态

<?php

class User extends \Eloquent {
    ......

public function profile()
{
    return $this->hasOne('Profile');
}

public function statuses()
{
    return $this->hasMany('Status');
}
}

配置文件和状态模型

class Profile extends \Eloquent {

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

class Status extends \Eloquent {

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

在我的 HomeController 索引方法中,我声明了一个像这样的变量 $user。

$user = User::with('profile', 'statuses')->whereId( Auth::id() )->first();

$statuses = $user->statuses;

然后我将状态压缩到我的视图中,然后运行一个

@foreach($statuses as $status)
    {{ $status->user->profile->firstname; }}

    {{ $status->message; }}
@endforeach

但我遇到了一个错误。

谢谢:)

【问题讨论】:

    标签: php laravel laravel-4 blade


    【解决方案1】:

    您需要知道,Eloquent 关系不是双向的。

    话虽如此,在users 上加载statuses 不会在每个status 上加载user

    $user = User::with('statuses')->first();
    // query users table once and statuses table once
    
    // now you can use statuses:
    $user->statuses; // collection
    $user->statuses->first(); // single status
    

    但这是完全不同的故事

    $user->statuses->first()->user; 
    // query users table for the user of this status
    // despite you already loaded that user and stored in $user variable
    
    // in your case you did that in your loop:
    @foreach($statuses as $status)
      {{ $status->user->profile->firstname; }}
      // here you query users table AND profiles table again
    

    因此,如果您在 foreach 循环中执行此操作,那么您最终会得到与集合中的状态一样多的查询。


    现在,对您来说最简单、最具表现力的方式是:

    // controller
    $user = Auth::user()->load('statuses', 'profile');
    // the same as User::with('statuses', 'profile')->where('id', Auth::id())->first();
    
    return View::make('some.view', compact('user'));
    

    那么在你看来:

    @foreach ($user->statuses as $status)
    
      // you can access the user, not affected by the loop
      $user->username
    
      // you can access the profile, not affected by the loop
      $user->profile->firstname
    
      // and of course each status in the loop
      $status->message
    
    @endforeach
    

    【讨论】:

    • 通过使用 $user = Auth::user()->load('statuses', 'profile');导致 3 个查询 'select * from users where users.id = ?限制 1', 'select * from profiles where profiles.user_id in (?)', 'select * from statuses where statuses.user_id in (?)'
    • 现在我还可以在视图中使用@unless($user->statuses-count()) 检查特定用户的状态表是否为空,谢谢!
    【解决方案2】:

    如何获取具有状态的用户的名字和姓氏 关联到那里的 ids

    那你可以试试这个(通知User::has('statuses'),这将只获取拥有statuses的用户):

    $user = User::has('statuses')
                ->with('profile', 'statuses')
                ->whereId(Auth::id())
                ->first();
    

    没有has('statuses'),您将获得所有用户,即使他们没有相关状态。然后在你的view:

    {{ $user->profile->firstname }}
    {{ $user->profile->lastname }}
    

    【讨论】:

      【解决方案3】:

      如果我在 Blade 视图中理解你应该使用:

      {{ $user->profile->firstname }} {{ $user->profile->lastname }}  
      
      @foreach ($user->statuses as $status)
        {{ $status->message }}  {{ $status->id}}
      @endforeach
      

      获取选定用户的所有状态消息和 ID。

      编辑

      当您添加更多代码时,您应该为您的视图分配 $user$statuses 变量。

      现在你可以这样做了:

      @foreach ($statuses as $status)
      
          {{ $user->profile->firstname }} {{ $user->profile->lastname }}
      
          {{ $status->message }}
      @endforeach
      

      因为这些消息的每个用户都是相同的(您从具有选定 ID 的数据库用户那里获得)

      但是您只能将$user 分配给您的视图,并且以下代码应该可以工作:

      @foreach ($user->statuses as $status)
      
          {{ $user->profile->firstname }} {{ $user->profile->lastname }}
      
          {{ $status->message }}
      @endforeach
      

      【讨论】:

      • 不需要foreach?因为我想为我的提要部分获取带有全名的状态。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 2014-11-29
      • 2014-04-19
      • 2019-12-21
      • 2018-04-20
      • 2014-02-24
      相关资源
      最近更新 更多