Отправить массив переменных в контроллер через ajax (Laravel) - PullRequest
0 голосов
/ 13 февраля 2020

Я пытаюсь отправить массив переменных для получения точных данных из базы данных на основе указанного массива, но в консоли выдается ошибка. Это успешно, когда передается только одно значение массива, но когда я передаю два или более значения массива, это показывает ошибку в консоли

irel__ref_category было из моей модели. вот мой код

javascript с ajax


     $(document).ready(function() {
                     var cuid= "{{ $cuid }}" ; //var cuid is my array value .
                     console.log(cuid);
                     getBranchAjax(cuid);

                 });



                      function getBranchAjax( cuid  ){
                $.ajaxSetup({
                        headers: {
                            'X-CSRF-Token': $('meta[name=csrf-token]').attr('content')
                        }
                    });
                $.ajax({
                    url:"{{ route('reporting.getCategoryAjax',['cuid']) }}",
                    method:'GET',
                    data:{cuid:cuid},
                    dataType:'json',
                    success:function(data){

                        console.log(data.category[0].irel__ref_category);
                       // console.log(data.branch.length);

                    },
                    fail: function(xhr, textStatus, errorThrown){
                    alert('request failed');
                    }
                })
            }

контроллер


 public function getCategoryAjax(Request $request){

            $custid = $_GET['cuid'];
            if($custid != ''){
                $data = $this->hdrelcustcategory->findCustByID($custid);

            }


            return response()->json([
                'category' =>$data
            ]);
        }

модель


public function irel_RefCategory()
    {
        return $this->belongsto(RefCategory::class,'cc_category_code','Ct_Code');
    }

    public function findCustByID($custid){
        $relcustcat = $this->select('cc_category_code')
                           ->distinct()
                           ->where('cc_customer_id',$custid)
                           ->where('cc_status_rec','R01')
                           ->orderby('cc_category_code','ASC')
                           ->with(['irel_RefCategory' =>function($query){
                                $query->select('Ct_Code','Ct_Level','Ct_Abbreviation','Ct_Description','Ct_Parent')
                                      ->where('Ct_Status_Rec','R01');
                           }]);
        return $relcustcat->get();
    }

ошибка в консоли is:

Uncaught TypeError: Невозможно прочитать свойство 'irel__ref_category' из неопределенного в Object.success (199,143,128: 138) в i (jquery -3.2.1.min. js: 2) в Object.fireWith [as resolWith] (jquery -3.2.1.min. js: 2) в A (jquery -3.2.1.min. js: 4) в XMLHttpRequest. (jquery -3.2.1.мин. js: 4)

1 Ответ

0 голосов
/ 13 февраля 2020

возможно попробуйте это ->whereIn('cc_customer_id',$custid)

, поэтому метод будет таким,

    public function findCustByID($custid){
        $relcustcat = $this->select('cc_category_code')
                           ->distinct()
                           ->whereIn('cc_customer_id',$custid)
                           ->where('cc_status_rec','R01')
                           ->orderby('cc_category_code','ASC')
                           ->with(['irel_RefCategory' =>function($query){
                                $query->select('Ct_Code','Ct_Level','Ct_Abbreviation','Ct_Description','Ct_Parent')
                                      ->where('Ct_Status_Rec','R01');
                           }]);
        return $relcustcat->get();
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...