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 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘