在插入数据库Laravel之前检查数据

时间:2018-04-28 06:03:16

标签: database laravel duplicates

有一些问题,寻找一些建议如何在将数据插入数据库之前进行检查。我想向团队添加成员,但我需要检查它是否已经存在。否则不检查它是否重复。用户可以属于同一团队+1000次。 我有一个名为:Teammembersall的表,我在其中添加了存储TeamId,UserId数据(这是我可以识别哪个团队用户所属的方式)。 但在我将用户添加到团队之前,我需要检查一下这个用户是否已经属于团队。不要复制它。 也许有些建议怎么做?

这是我的控制者:

 public function insertUserToTeam(Request $req)
{
    $teamNameSelectBoxInTeamMembers = $req->input('teamNameSelectBoxInTeamMembers');
    $userNameSelectBoxInTeamMembers = $req->input('userNameSelectBoxInTeamMembers');

    $data = array(
        'TeamId' => $teamNameSelectBoxInTeamMembers,
        'UserId' => $userNameSelectBoxInTeamMembers
    );

    DB::table('teammembersall')->insert($data);

    session()->flash('msg', 'Successfully done!.');
    return redirect()->back();
}

这是我的观点:

<div class="container" id="container-home-bottom">
    <div class="row">

        <div class="col-md-2">
        </div>

        <div class="col-md-4">
            <div class="card-header">Add User to Team</div>

            <div class="form-group">
                <p id="note-assist">Can find your team? <a href="{{ route('teamCreate') }}">Create new Team!</a></p>
                <form action="/insertUserToTeam" method="post">
                    {{csrf_field()}}

                    <div class="form-group" >
                        <label for="sel1">Select your team:</label>
                        <select class="form-control" id="sel2" name="teamNameSelectBoxInTeamMembers" required>
                            <option selected disabled hidden>Select a team</option>
                                @foreach ($team as $teams_each)
                                    <option value="{!! $teams_each->teamId !!}">{!! $teams_each->name !!}</option>
                                @endforeach

                        </select>
                        <br>

                        <label for="sel2">Select user:</label>
                        <select class="form-control" id="sel3" name="userNameSelectBoxInTeamMembers">
                            <option selected disabled hidden>Select user</option>
                            @foreach ($users as $user_each)
                                <option value="{!! $user_each->id !!}">{!! $user_each->name !!}</option>
                            @endforeach

                        </select>
                    </div>
                    <input id="add-team" type="submit" class="btn btn-primary btn-block" name="submitEditTeamCompany" value="Add to Team">
                </form>
                <br>
            </div>
        </div>

1 个答案:

答案 0 :(得分:0)

您必须检查该表中是否已存在userid和teamid:

public function insertUserToTeam(Request $req)
{
    $teamNameSelectBoxInTeamMembers = $req->input('teamNameSelectBoxInTeamMembers');
    $userNameSelectBoxInTeamMembers = $req->input('userNameSelectBoxInTeamMembers');


    $data = array(
        'TeamId' => $teamNameSelectBoxInTeamMembers,
        'UserId' => $userNameSelectBoxInTeamMembers
    );

    $count = DB::table('teammembersall')->where('TeamId', $teamNameSelectBoxInTeamMembers)
                                ->where('UserId',  $userNameSelectBoxInTeamMembers)
                                ->count();

    if($count > 0){
        // This user already in a team
        //send error message
    }else{
        DB::table('teammembersall')->insert($data);
    }

    session()->flash('msg', 'Successfully done!.');
    return redirect()->back();
}