如何使用外键解决One to One关系的问题(How can I solve issue with One to One relationship using foreign keys)

编程入门 行业动态 更新时间:2024-10-26 08:29:01
如何使用外键解决One to One关系的问题(How can I solve issue with One to One relationship using foreign keys)

我一直在模特中使用雄辩。 我有以下两个表:

Singlecard ->id ->card_id Card ->id ->card_id

我的Singlecard模型具有以下功能:

public function info() { return $this->hasOne('Card', 'card_id', 'card_id'); }

我用它来获取卡片(我的测试中只有一张牌在卡片中)。

$cards = Singlecard::where('deck_id', '=', $deck)->get(); foreach ($cards as $card) { $cards_array[] = $card; }

它得到了正确的卡并使用var_dump我验证了。 但是,这是问题所在:

<div class="row singlecard"> <a class="" href="{{ $single->id }}"> <div class="large-2 columns"> <img src="{{ $single->info->card_image }}"> </div> <div class="large-10 columns"> <div class="row"> <div class="large-12 columns"> <p>{{ $single->info->name }}</p> </div> </div> <div class="row"> <div class="large-12 columns"> @foreach ($single->attributes as $attribute) <p>{{ $attribute->alias }}</p> @endforeach </div> </div> </div> </a> </div>

这是扭曲:属性代码工作正确。 它从我定义的一对多关系中抓取了正确的属性。 但它从卡表中抓取了错误的信息。 即使我定义了要匹配的键,它也会根据单个ID和Cards表中的card_id进行匹配。

我试过删除键,但没有做任何事情。 我甚至一起删除了这个函数只是为了验证那是被调用的函数。 我不确定是什么问题?

更新:

我弄清楚了,我做了两件事。 一,我使用卡表中的id作为与Singlecards匹配的记录。 我也改变了我在Singlecards模型中的功能:

public function info() { return $this->belongsTo('Card', 'card_id'); }

这使我能够正确地查询关系。

I've been using eloquent in my models. I've got the following two tables:

Singlecard ->id ->card_id Card ->id ->card_id

My Singlecard Model has the following function:

public function info() { return $this->hasOne('Card', 'card_id', 'card_id'); }

I used this to get the card (there's only one card in the deck for my test).

$cards = Singlecard::where('deck_id', '=', $deck)->get(); foreach ($cards as $card) { $cards_array[] = $card; }

It got the correct card and using var_dump I verified that. However, here's the problem:

<div class="row singlecard"> <a class="" href="{{ $single->id }}"> <div class="large-2 columns"> <img src="{{ $single->info->card_image }}"> </div> <div class="large-10 columns"> <div class="row"> <div class="large-12 columns"> <p>{{ $single->info->name }}</p> </div> </div> <div class="row"> <div class="large-12 columns"> @foreach ($single->attributes as $attribute) <p>{{ $attribute->alias }}</p> @endforeach </div> </div> </div> </a> </div>

Here's the twist: The attributes code works correct. It grabbed the correct attributes from a one to many relationship I defined. But it's grabbing the wrong info from the Cards table. Even though I defined the keys to match on, it is matching based on the ID of the singlecard and the card_id in the Cards table.

I've tried removing the keys and that didn't do anything. I even removed the function all together just to verify that that was the function being called. I'm not sure what's wrong?

UPDATE:

I figured it out, I did two things. One, I used id from the Cards table as the record to match with Singlecards. I also changed my function in the Singlecards model like so:

public function info() { return $this->belongsTo('Card', 'card_id'); }

This allowed me to properly query the relationship.

最满意答案

我需要更新我的模型的相关性,并更好地形成关系。 我还需要更改模型,以便Singlecards属于卡片。 我认为它应该是相反的。

卡包含有关各种卡的所有信息,而Singlecards是每个人手/甲板上的内容。 我认为这会使Singlecards成为父母,但这是一个错误。 一旦我将模型中的函数更改为:

public function info() { return $this->belongsTo('Card', 'card_id'); }

然后它奏效了。

I needed to update how my models were related and better form the relationship. I also needed to change the model so that Singlecards belonged to Cards. I assumed it should be the opposite.

Cards contains all the info about the various cards and Singlecards is what is in each individuals hands/decks. I assumed that would make Singlecards the parent but that was a mistake. Once I changed the function in the model to be like this:

public function info() { return $this->belongsTo('Card', 'card_id'); }

Then it worked.

更多推荐

本文发布于:2023-08-07 12:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464197.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   关系   solve   issue   foreign

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!