php - How to get data using ajax passing parameter (laravel) -
i want fill table doing sql query has id = $id
.
code ajax:
$('#ingredienttable').datatable({ "ajax": { "url": "api/ingredients", "type": "post", "data" : { '_token': token, "id" : '1' , } }, "columns":[ {data:'name', name: 'ingredients.name'}, ], });
in route:
route::post('api/ingredients', function() { return datatables::of( db::select('select * ingredients inner join ingredient_product on ingredient_product.ingredient_id = ingredients.id ingredients.id=' . $id . ' ;')) ->make(true); });
i error:
post http://localhost:8080/pizzeria/public/api/ingredients 500 (internal server error) , datatables warning: table id=ingredienttable - ajax error. more information error, please see http://datatables.net/tn/7
the r
in route
should capitalized
route::post
your select()
statement incorrect, need use db::raw()
allow write raw sql. however, pointed out @aynber, expose sql injection. instead, should done this:
db::table('ingredients') ->join('ingredient_product', 'ingredients.id', 'ingredient_product.ingredients_id') ->where('ingredients.id', request()->has('id') ? request()->get('id' : null) ->select('*') ->get();
this safeguards sql injection , correctly utilize laravel's query builder
Comments
Post a Comment