【发布时间】:2019-09-04 07:12:33
【问题描述】:
我与数据透视表有一个多对多关系。在我的模型 Deck 和 PlayCard 之间,我怎样才能将我的甲板和他的 Playcard 放在里面?
类似这样的:
id: 1,
...
play_cards: [
{
id: 1, ...
},
{
id: 2, ...
}
]
我尝试使用with() 函数,但它不起作用。
这是我的功能:
public function addToDeck(Request $request)
{
$play_card = Auth::user()->playCards()->where('uid', $request->card_uid)->first();
$deck = Auth::user()->decks()->where('token', $request->deck_token)->first();
if (!$play_card || !$deck) {
return ResponseService::respondWithErrors(
400,
$this->routes_messages[__FUNCTION__],
['Error Deck or Uid unknow.']
);
}
if ($play_card->decks()->find($deck->id)) {
return ResponseService::respondWithErrors(
400,
$this->routes_messages[__FUNCTION__],
['Card already in this deck.']
);
}
$deck->playCards()->attach($play_card);
$deck->save();
return ResponseService::respondOK(
200,
$this->routes_messages[__FUNCTION__],
$deck
);
}
【问题讨论】:
-
你能发布你收到的消息或者你面临的问题是什么?
-
能否附上模特的代码?
标签: laravel eloquent many-to-many relationship