Laravel资源路由器忽略急切加载的数据并获取新查询

时间:2014-06-24 21:50:52

标签: laravel-4

在我的资源路由器中,我有这个函数,它应该只加载configureName为$ id的行。问题是,当我打电话给$ demo-> demoSettings时,资源路由器忽略了我的急切负载并抓住了与这个$ demo相关的所有demoSettings。

这不是我遇到的这个问题的唯一例子。为了方便起见,我还有几个循环,我从相关表中将数据注入到我的api索引中。我添加一个调用来访问我急切加载的数据的那一刻,它再次查询并将数据注入我的资源返回。我通过在其他实例中使用unset来解决这个问题,但是这个是不同的,因为我实际上想要数据列表而不是单个值。

public function show($demoId,$id)
{
    $demo = Demo::with(array('DemoSettings' => function($query) use ($id)
    {
        $query->where('configureName', '=', $id);
    }))->where('demoId','=',$demoId)->first();
    return $demo->demoSettings;
}

演示模型

class Demo extends Eloquent {

protected $table = 'Demo';
public $timestamps = false;

/**
 * The key column used by the model.
 * @var string
 */
protected $primaryKey = 'idDemos';

/**
 * one(demo) to many(demoSettings) relationship
 * @return array of the demoSettings associated with the demo
 */
public function demoSettings()
{
    return $this->hasMany('DemoSettings','idDemos','idDemos')->orderBy('configureName','asc');
}
}

DemoSettings Model

class DemoSettings extends Eloquent {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'DemoSettings';
    /**
     * The key column used by the model.
     * @var string
     */
    protected $primaryKey = 'idDemoSettings';

    /**
     * many(demoSettings) to one(demo) relationship
     * @return demo the parent record
     */
    public function demo()
    {
        return $this->belongsTo('Demo','idDemos','idDemos');
    }

}

路线

Route::group(array('prefix' => 'api/v1'), function(){
Route::group(array('prefix' => 'demos'), function(){
    Route::resource('/', 'api_DemosController', array('only' => array('index','store','destroy','show')));
    Route::resource('/{demoId}/settings', 'api_DemoSettingsController', array('only' => array('index','store','show')));
});

});

0 个答案:

没有答案