获取具体的列与渴望加载laravel 4 [英] Getting specific columns with eager loading laravel 4

查看:83
本文介绍了获取具体的列与渴望加载laravel 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,并提出了一个基本的应用程序来掌握关系。我已经实现了一对一的关系,并希望从基表和相关表中获取特定的列。我有两个表,即用户和identity_cards.Relationship定义如下

  class用户扩展Eloquent实现UserInterface,RemindableInterface {

使用UserTrait ,RemindableTrait;

/ **
*模型使用的数据库表。
*
* @var string
* /
protected $ table ='users';

/ **
*从模型的JSON表单中排除的属性。
*
* @var数组
* /
// protected $ hidden = array('password','remember_token');

protected $ fillable = array('first_name','last_name','email','created_at','updated_at');

public function identity_cards()
{
return $ this-> hasOne('IdentityCard');
}
}


class IdentityCard扩展Eloquent {

protected $ table ='identity_cards';
protected $ fillable = array('issuance_date','expiry_date','issuance_location','user_id');
public $ timestamps = false;

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

当我尝试检索所有列时,一切正常

  $ users = User :: with('identity_cards') - > get() - > toArray() 

但是,当我尝试从两个或两个表中获取特定列时,我得到一个空数组身份证表记录

  $ users = User :: with(array('identity_cards'),function($ query) {
$ query-> select('issuance_location'); //从身份证表获取特定列
}
) - > get(array(first_name)) - > ;指定者();

以上陈述的结果如下: -

  Array 

[0] => Array

[first_name] => User1
[identity_cards] =>


[1] =>数组

[first_name] => User2
[identity_cards] = >


[2] => Array

[first_name] => User3
[identity_cards] =>



这可以使用查询构建器实现,但我想要解决方案与雄辩。为什么我得到一个空数组为我的相关表,即使我有它的数据。有什么具体的列可以使用eager加载?

解决方案

1您需要始终选择父母的主键 外键(),否则口述将不能匹配相关模型。



2封闭传递给渴望的加载是数组的值,而不是的第二个参数 function:

  User :: with(array('identity_cards'=> function($ query){
$ query-> select('issuance_location','user_id');
})) - > get(array('first_name','id')) - > toArray();

3我将该关系重命名为 identity_card ,因为它是 hasOne - 它会更容易使用。


I am new to laravel and making a basic application to get a grip on relationships.I have implemented one to one relationship and want to get specific columns from both the base table and the related table.I have two tables namely users and identity_cards.Relationship is defined as follows

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * 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', 'remember_token');

    protected $fillable = array('first_name','last_name','email','created_at','updated_at');

    public function identity_cards() 
    {
        return $this->hasOne('IdentityCard');
    }
}


class IdentityCard extends Eloquent {

    protected $table    = 'identity_cards';
    protected $fillable = array('issuance_date','expiry_date','issuance_location','user_id');
    public $timestamps  = false;

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

Everything works fine when i try to retrieve all the columns

$users = User::with('identity_cards')->get()->toArray();

But when i try to get specific columns from either or both the tables,i got an empty array against the record for identity card table

 $users = User::with(array('identity_cards'),function($query) {
                $query->select('issuance_location'); //getting specific column from identity card table
              }
          )->get(array("first_name"))->toArray();

Result against the above statement is as follows:-

Array
(
    [0] => Array
    (
        [first_name] => User1
        [identity_cards] => 
    )

    [1] => Array
    (
        [first_name] => User2
        [identity_cards] => 
    )

    [2] => Array
    (
        [first_name] => User3
        [identity_cards] => 
    )

 )

This can be achieved using query builder but i want the solution with eloquent.Why am i getting an empty array for my related table even if i have the data in it.How specific columns can be fetched using eager loading??

解决方案

1 You need to always select primary key of the parent and foreign key of the child of a relation, otherwise Eloquent won't be able to match related models.

2 Closure passed to the eager loading is a value of the array, not 2nd parameter of with function:

User::with(array('identity_cards' => function($query) {
            $query->select('issuance_location', 'user_id');
      }))->get(array('first_name', 'id'))->toArray();

3 I would rename that relation to identity_card, for it's hasOne - it will be easier to work with.

这篇关于获取具体的列与渴望加载laravel 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆