仅当没有参与者关联时,才删除注册类型

时间:2018-07-24 15:10:36

标签: php laravel

我有一个删除注册类型的按钮,但是仅当没有注册该参与者的参与者(即,没有与该注册类型关联的参与者)时,才应允许用户删除注册类型。 / p>

但它不能与下面的代码一起使用,它显示为:

SQLSTATE [42S02]:找不到基表或视图:1146表'project.registration_registration_types'不存在(SQL:选择registrations。*,registration_registration_typesregistration_type_idpivot_registration_type_idregistration_registration_typesregistration_id作为{{1}的pivot_registration_id,来自registrations上的registration_registration_types内部联接registrationsid = { {1}}。registration_registration_types,其中registration_idregistration_registration_types = 3)

所以我有这个HTML,其中包含删除注册类型的链接,单击该链接时会显示一个模式,以便用户确认是否要删除注册类型:

registration_type_id

以及删除注册类型的方法:

@foreach($registrationType as $rtype)
    <div class="form-check">
        <input
                {{ (old('radiobutton') && old('radiobutton') == $rtype->id) ? 'checked' : '' }}
                class="form-check-input radio" type="radio" name="radiobutton"
                value="{{ $rtype->id }}" id="{{$rtype->id}}">
        <label class="form-check-label" for="exampleRadios1">
            {{$rtype->name}} <a data-toggle="modal" class="btn btn-sm btn-outline-light-gray ml-4"
                                data-target="#removeRtype">
                <i class="fa fa-times" aria-hidden="true"></i> Remove</a>
        </label>
    </div>
    <div class="modal fade bd-example-modal-lg"  id="removeRtype" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Remove registration type</h5>
                    <button type="button" class="close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="container">
                        <div class="row d-flex justify-content-center">
                            <p>Remove Registration type?</p>
                            <button class="btn btn-outline-primary" id="cancel_remove" href="#"  data-dismiss="modal">No</button>
                            <a class="btn btn-primary ml-2" id="confirm_remove"
                               href="{{route('rtype.remove', ['rtypeID' => $rtype->id])}}">Yes</a>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" id="close_login_modal" class="btn btn-primary"
                            data-dismiss="modal">Close
                    </button>
                </div>
            </div>
        </div>
    </div>
@endforeach

路线:

 public function destroy($rtypeID)
    {
        $rtype = RegistrationType::findOrFail($rtypeID);

        $registrationsCount = $rtype->registrations->count();

        dd($registrationsCount);

        if ($registrationsCount == 0) {
            $rtype->delete();
            Session::flash('success', 'Registration type removed with success.');
            return redirect()->back();
        } else {
            Session::flash('error', 'Is not possible to remove the registration type since there are already participants registered in it.');
            return redirect()->back();
        }
}

1 个答案:

答案 0 :(得分:0)

通过聊天讨论,发现您在多对多关系中有错误的数据透视表。应该是这样

RegistrationType模型:

public function registrations(){ 
   return $this->belongsToMany('App\Registration', 'participants'); 
} 

注册模型:

public function registration_types(){ 
   return $this->belongsToMany('App\RegistrationType', 'participants'); 
} 

现在这样获取

$rtype = RegistrationType::with('registrations')->findOrFail($rtypeID);
$registrationsCount = $rtype->registrations->count();
dd($registrationsCount);