2017-12-27 5 views
0

현재, 내 주소록에 dashboard/1/people처럼 보이기 위해 show을 내 컨트롤러에서 실행중인 쇼 페이지에 있습니다. 이제 한 사람을 클릭하면 다른 페이지로 연결되고 그 곳에서 getPeople이 호출됩니다.ajax에서 컨트롤러로 ID를 전달하는 방법 - Larvel & Ajax

scripts의 ajax 요청에서 내가 클릭 한 사람의 ID를 1으로 가져와 내 컨트롤러로 전달할 수 있습니까?

추신 : 지금이 순간, 나는 아약스 요청 1을 하드 코딩했지만 난

어떻게 내가이 작업을 수행합니까 그것은 동적하시기 바랍니다되고 싶어?

스크립트

datatable = $('#table').DataTable({ 

       "ajax": "{{ route('dashboard/1/people') }}", 
       "columns": [ 
        {data: 'check', name: 'check'},  

       ], 

컨트롤러

public function show($id) 
    { 
      $class = Class::whereId($id)->first(); 

     return view('show'); 
    } 



    public function getPeople($id) 
     { 
      $get_id = $id; 
      $class = Class::whereId($get_id)->first(); 
      $people = $class->peoples()->get(); 
      return Datatables::of($people)->addColumn('action', function ($ppl) { 
       //return 
    })->make(true);   


     } 

답변

1

이 작동합니다 :

당신의 getPeople 방법 매장에서 ID를 session 변수 :

public function getPeople($id) 
    { 
     $get_id = $id; 
     //using session helper method 
     session(['show_id' => $id]); 
     $class = Class::whereId($get_id)->first(); 
     $people = $class->peoples()->get(); 
     return Datatables::of($people)->addColumn('action', function ($ppl) { 
      //return 
     })->make(true);   
    } 

다음은 AJAX 코드에 액세스 :

datatable = $('#table').DataTable({ 

       "ajax": "{{ route('dashboard/'.session('show_id').'/people') }}", 
       "columns": [ 
        {data: 'check', name: 'check'},  

       ], 
0

DataTable의 아약스는 요 그냥 같은 오브젝트 형식의 추가 매개 변수를 전달할 수 있습니다 :

datatable = $('#table').DataTable({  
     "ajax": { 
      "type": "GET", 
      data:{id: my_id_var}, 
      "url": "my_route" 
     } 
    } 

그리고에 당신의 함수는 요청을 얻는다. var

public function getPeople(Request $request){ 
      $get_id = $request->id; 
      $class = Class::whereId($get_id)->first(); 
      $people = $class->peoples()->get(); 
      return Datatables::of($people)->addColumn('action', function ($ppl) { 
       //return 
    })->make(true);   
} 

더 자세한 정보는 Sorce Page

관련 문제