dongnong3799 2018-07-24 15:10
浏览 10
已采纳

仅在没有与之关联的参与者时删除注册类型

I have a button to remove a registration type, but the user should only be allowed to remove a registration type if there are no participant registered in that registration type, that is if there are no participants associated with that registration type.

But its not working with code below, it appears:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'project.registration_registration_types' doesn't exist (SQL: select registrations.*, registration_registration_types.registration_type_id as pivot_registration_type_id, registration_registration_types.registration_id as pivot_registration_id from registrations inner join registration_registration_types on registrations.id = registration_registration_types.registration_id where registration_registration_types.registration_type_id = 3)

So I have this HTML that has the link to remove the registration type that when is clicked it shows a modal so the user confirm if he wants or no remove the registration type:

@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

And the method to remove the registration type:

 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();
        }
}

Route:

 Route::get('conference/destroy/{rtypeID}/rtypes',    [ 'uses' => 'RegistrationTypeController@destroy', 'as'=>'rtype.remove']);
  • 写回答

1条回答 默认 最新

  • doucongqian6644 2018-07-24 16:49
    关注

    From chat discussion it found that you have wrong pivot table in many to many relation. It should be like this

    RegistrationType model:

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

    Registration model:

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

    Now fetch it like this

    $rtype = RegistrationType::with('registrations')->findOrFail($rtypeID);
    $registrationsCount = $rtype->registrations->count();
    dd($registrationsCount);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探