Laravel – ajax 教學

ajax

$.ajax({
  type:"POST",
  url:"/getSomething",
  dataType:"json",
  data:{somedata:somedata},
  success:function(result){
    console.log(result);
  }
});

route / web.php

//Route到你所定義的Controller
Route::post('/getSomething', 'YourController@yourFunction');

Controller

public function yourFunction(Request $request){
  //取回data
  $data = $request->input('somedata');
  
  //取得所有data
  $allData = $request->all();

  $results = DB::table('table')
  ->first(); //只取第1行row (二選一)
  ->get();  //取得所有row (二選一)

  //取得多個table data
  $results = DB::table('tableA_name')
  ->select('tableA_name.column1', 'tableB_name.column1', 'tableB_name.column2 as yourCustomName')
  ->where('tableA_name.id, "=", '20')
  ->join('tableA_name.name', '=", 'tableB_name.name')
  ->get();

  //傳回到前端
  return $results; //json
  var_dump $results; //text
  
}

開始在上面輸入您的搜索詞,然後按回車進行搜索。按ESC取消。

返回頂部