dongyaobo9081 2014-03-20 17:27
浏览 30
已采纳

半复杂的Eloquent查询

I am working on a eloquent query to compile a newsletter but I have hit a brick wall.

What I'm trying to do is create a UI where the user can select a publication and date. Ideally it would then compile a list of that publication's categories (where stories > 0) and stories belonging to it.

Here are my 3 models:

Story

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

public function publication()
{
        return $this->belongsTo('Workbench\Dailies\Publication', 'publication_id');
}

public function category()
{
        return $this->belongsTo('Workbench\Dailies\Category', 'category_id');
}

Publication

public function story()
{
    return $this->belongsTo('Workbench\Dailies\Story');
}

public function stories()
{
    return $this->hasMany('Workbench\Dailies\Story', 'publication_id');
}

public function category()
{
    return $this->belongsTo('Workbench\Dailies\Category', 'publication_id');
}

Category

public function story()
{
        return $this->belongsTo('Workbench\Dailies\Story', 'category_id');
}

public function publications()
{
        return $this->belongsTo('Workbench\Dailies\Publication', 'publication_id');
}

public function stories()
{
        return $this->hasMany('Workbench\Dailies\Story', 'category_id');
}

Here is how my tables look:

Story

  • content
  • user_id
  • publication_id
  • category_id
  • publish_date

Publication

  • id
  • name

Category

  • id
  • name
  • publication_id

Here is what I currently have in my Repository.

public function compileStories($input)
{
    return Category::has('stories', '>', 0)
    ->with('publications')
    ->whereHas('stories', function ($query) use ($input)
        {
            $query->where('publish_date', $input['publish_date']);
            $query->where('publication_id', $input['publication_id']);
        });
}

Am I headed in the right direction here or is there any way to improve the code above? It is not currently functioning as expected.

  • 写回答

1条回答 默认 最新

  • duanliang4009 2014-03-20 18:54
    关注

    There are a couple of things I see here that may help straighten you out.

    First - Some of the models have strange relationships without knowing more about your whole application. The Story model does not need the publication relationship since it can be retrieved through the category relationship, unless you have need of it otherwise. The Category model does not need both a story and a stories relationship, again, unless there's more to the story I don't know. In your example, you should only need the hasMany relationship. The Publication model only needs the categories relationship.

    Now, after some cleanup of the models, let's look at your query. Using the category model to return your results seems completely appropriate for your desired results. You can check for the publication without having to dive into your stories, though. I haven't tested it, but you may not need the use $input line since $input is in the larger scope. You're also missing a conditional check in your where statments in the whereHas clause. The query should be able to be simplified as follows:

    Category::where('publication_id', '=', $input['publication_id'])
        ->whereHas('stories', function($query)
        {
            $query->where('publish_date', '=', $input['publish_date']);
        })
        ->get()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nrf52810-c三个a 程序
  • ¥15 lego-loam跑出来的roll误差很大
  • ¥50 求一个半透明没有锯齿的圆角窗体的实现例子
  • ¥15 STM32cubeMX里的FreeRTOS无法释放内存
  • ¥15 CATIA有些零件打开直接单机确定终止
  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对