//controller
$promotion = Promotion::findOrFail($id);
//return
Array
(
[id] => 2
[en_title] => promo1
[game_id] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
[amount] => 100.00
[start_at] => 2021-02-22
[end_at] => 2222-02-22
[status] => 1
)
//model promotion
class Promotion extends Model
{
use HasFactory;
protected $guarded = [];
protected $casts = [
'game_id' => 'array'
];
public function getAllGames()
{
return $this->belongsTo(Game::class, 'game_id', 'id');
}
}
问题:
目前,我有2个表是游戏促进和,但我遇到了麻烦,当进入一个关系,因为列的game_id内部提升表是一个JSON,所以很难加入。是否有任何工作可以轻松地将它们连接在一起,以便检索游戏数据?
在你的游戏和推广表之间建立多对多关系,
将你的game_ids放在pivot表(game_promotion)中而不是JSON字段,
多对多关系