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

Php 如何在没有foreach的情况下访问laravel中的关系?,php,laravel-4,eloquent,Php,Laravel 4,Eloquent,我不熟悉php中的laravel和OOP。 我试图通过雄辩的对象进行迭代,但我认为我没有好的方法 例如:分类模型: class Categorie extends Eloquent { protected $table = 'categorie'; protected $timestamp = true; protected $fillable = array('id_parent'); public function children() { return $this->hasMa

我不熟悉php中的laravel和OOP。 我试图通过雄辩的对象进行迭代,但我认为我没有好的方法

例如:分类模型:

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,“traducation”:[{“id”:1,“id_categorie”:3,“id_langue”:1,“nom”:“MEUBLES”}},{“id”:4,“id_parent”:null,“created_at”:“created_at”:“2014-08-08-08-08-07:07:05”,“updated_at”:“2014-08-08-08-08-07:07:07:05”,“deleted(traducation id”:“traducation,“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,“traducation”:[{“id”:4,“id_-categorie”:6,“id_-langue”:1,“nom”:“装饰”}]

这正是我所需要的,但我想直接接触这样的交易

echo $categorie->traduction->nom;
但我有一个错误:

Undefined property: Illuminate\Database\Eloquent\Collection::$traduction
如何在面向对象中使用traduction迭代类别,我可以在foreach中使用foreach实现,但似乎不是最佳实践

感谢您的帮助,请执行以下操作:

$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 ) 
尝试: echo$categorie->traducation->list('nom')


希望这会有帮助!G

你想得到类别的交易,在那里(“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();

尝试这些后发布您的结果。

列表()不是laravel查询生成器的一部分吗?如果是,那就不起作用了。不,它是Lightlight\Support\Collection$categorie->traduction()->List('nom'的一部分)看看我在问题中的评论,这位同事看了看他的代码,注意到我们都遗漏了一件事:laravel没有看到“交易”“作为一个合适的收集对象。我永远不知道为什么要使用
$categorie->traducation->nom导致了一个集合错误,因为您提供的输出是json格式的,因此是模糊的。尝试var_dumping分类并将其放入问题中,我将能够帮助您找出your命令导致错误的原因。您无法访问
->nom
,因为它是单个
Traduction
属性,而
Traduction
是一个集合(因为关系是
有许多
)。因此,使用
列表
获取该集合中所有项目的
nom
属性。