如何在没有foreach的情况下访问laravel中的关系?

时间:2014-08-08 18:34:51

标签: php laravel-4 eloquent

我是php的laravel和OOP的新手。 我想要迭代Eloquent对象,但我想我没有好方法。

例如:分类模型:

class Categorie extends Eloquent {

protected $table = 'categorie';
protected $timestamp = true;
protected $fillable = array('id_parent');

public function children() {

return $this->hasMany('Categorie','id_parent','id');
}

public function traduction() {

return $this->hasMany('Categorie_Langue','id_categorie','id');

}


}

控制器索引:

public function index()
{

    $categorie = Categorie::
    whereNull('id_parent')
    ->has('traduction')
    ->with('traduction')
    ->get()
    ;
    return ($categorie);
}

返回:

[{"id":3,"id_parent":null,"created_at":"2014-08-08 07:04:57","updated_at":"2014-08-08 07:04:57","deleted_at":null,"traduction":[{"id":1,"id_categorie":3,"id_langue":1,"nom":"MEUBLES"}]},{"id":4,"id_parent":null,"created_at":"2014-08-08 07:07:05","updated_at":"2014-08-08 07:07:05","deleted_at":null,"traduction":[{"id":2,"id_categorie":4,"id_langue":1,"nom":"MEUBLES"}]},{"id":6,"id_parent":null,"created_at":"2014-08-08 11:27:11","updated_at":"2014-08-08 11:27:11","deleted_at":null,"traduction":[{"id":4,"id_categorie":6,"id_langue":1,"nom":"DECORATIONS"}]}]

正是我需要的东西,但我想直接访问这样的traduction;

echo $categorie->traduction->nom;

但我收到了这个错误:

Undefined property: Illuminate\Database\Eloquent\Collection::$traduction

如何在面向对象的情况下迭代带有traduction的类别,我可以在foreach中实现foreach,但似乎不是最佳实践?

感谢您的帮助,

3 个答案:

答案 0 :(得分:0)

这样做:

$categorie = Categorie::
whereNull('id_parent')
->has('traduction')
->with('traduction')
->get()->toArray();

然后使用do:

$childArray = array_fetch(YOUR_ARRAY, 'traduction.nom');

这是一个粗略的草图,进行了适当的测试。我决定测试它:

$array = array(
    array(
        "id"=>3,
        "id_parent"=>null,
        "created_at"=>"2014-08-08 07:04:57",
        "updated_at"=>"2014-08-08 07:04:57",
        "deleted_at"=>null,
            "traduction"=> array(
                "id"=>1,
                "id_categorie"=>3,
                "id_langue"=>1,
                "nom"=>"MEUBLES"
            )
        ),
    array(
        "id"=>3,
        "id_parent"=>null,
        "created_at"=>"2014-08-08 07:04:57",
        "updated_at"=>"2014-08-08 07:04:57",
        "deleted_at"=>null,
            "traduction"=> array(
            "id"=>1,
            "id_categorie"=>3,
            "id_langue"=>1,
            "nom"=>"MEUBLES"
        )
    )
);

$childArray = array_fetch($array, 'traduction.nom');
print_r($childArray);

输出:

Array ( [0] => MEUBLES [1] => MEUBLES ) 

答案 1 :(得分:0)

尝试:     echo $ categorie-> traduction->列表(' nom');

希望它会有所帮助! ģ

答案 2 :(得分:0)

你想得到类别的转义(“parent_id”,“=”,NULL);正确?

然后你应该能够做到这一点

    $categories = Categorie::all();
    foreach($categories as $category)
{
     $categories->load(array('traduction'=>function($q){
$q->where('Categorie.parent_id', NULL);
}))
}
    return $traductions;

您也可以尝试使用with

return Categorie::with(array('traduction'=>function($q){
$q->where('Categorie.parent_id', NULL);
}))->get();

在尝试这些后发布您的结果..