doudang1052 2015-12-25 07:35
浏览 49
已采纳

Laravel 5.0雄辩的关系问题

I have Tournament, Inside a tournament, I have Categories For each categories, I have CategoriesSettings

I defined a relationship in the Category Model :

 public function settings()
{
    return $this->hasOne('App\CategorySettings');
}

So, I can access $tournament->categories without any problem ( with a belongsToMany rel. in Tournament model )

I have 3 tables:

Tournaments, Categories, category_tournament, and CategoriesSettings

category_tournament is just a pivot table.

But when I try to loop on categories, and try to reach settings in the view:

@foreach($categories as $category)
    {{ $category->settings}}
@endforeach

I just get the result for the first category.

Then the result is an array, and I expect a CategorySettings Object.

The funny stuff is when I dd $categories, I have a collection of 6 Categories, but then, when I check the category object, I can't find my hasOne Relationship:

 0 => Category {#437 ▼
  #table: "category"
  +timestamps: true
  #fillable: array:2 [▶]
  #connection: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  #attributes: array:4 [▶]
  #original: array:8 [▶]
  #relations: array:1 [▼
    "pivot" => Pivot {#436 ▼
      #parent: Tournament {#426 ▶}
      #foreignKey: "tournament_id"
      #otherKey: "category_id"
      #guarded: []
      #connection: null
      #table: "category_tournament"
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:4 [▶]
      #original: array:4 [▶]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #fillable: []
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }

I just find my pivot relation

Here is the query that is ran with :

$tournament->categories

.

SELECT 
`ken_category`.*,
`ken_category_tournament`.`tournament_id` AS `pivot_tournament_id`,
`ken_category_tournament`.`category_id` AS `pivot_category_id`,
`ken_category_tournament`.`created_at` AS `pivot_created_at`,
`ken_category_tournament`.`updated_at` AS `pivot_updated_at`
FROM
`ken_category`
    INNER JOIN
`ken_category_tournament` ON `ken_category`.`id` =     `ken_category_tournament`.`category_id`
WHERE
`ken_category_tournament`.`tournament_id` = ?

and the final query:

$category->settings


select * from `ken_category_settings` 
where `ken_category_settings`.`category_id` = ? 
and `ken_category_settings`.`category_id` is not null limit 1

Can anyone explain me this behaviour ( and how to fix :)???

Tx

  • 写回答

1条回答 默认 最新

  • duanmu8911 2015-12-25 07:51
    关注

    Try putting the condition in if statement. If it has settings it will echo out other wise it will keep continue

    @foreach($categories as $category)
      @if(count($category->settings) > 0)
       {{ $category->settings->columnname }}
     @endif
    @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?