简体   繁体   中英

Ensute linking / pivot table insert is unique in Laravel 4.1

I've got a linking table that needs to each link to be unique. So team_id 1 can only be in competition_id 1 once.

Does Laravel have any nice way to enforce this?

$competitionTeam = new CompetitionTeam();
$competitionTeam->competition_id = $competition_id;
$competitionTeam->team_id = $team_id;
$competitionTeam->save();

You can checkout the Eloquent Relationships in documentation.

Eloquent Documentation

Basically what you need to do is while creating Eloquent model you have to define what type of relation you have between tables. I think One-to-Many relation will do your business.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM