I have two tables , categories and product.Each category has id and name. Each product has id, name and category id. Now I want to load the data on each tab when a user click on specific category_id.so related product will be shown on each category tab. I have tried but couldn't get the solution.
Here is my controllers:
public function gettestItem(){
$products = Product::all();
$categories = Category::all();
return view('products', compact('products', 'categories'));
}
public function showitems(Request $request){
$id=$request->id;
$products = DB::select(DB::raw("SELECT products.name FROM products INNER JOIN categories on categories.id = products.category_id WHERE products.category_id ='id'"));
return \Response::json($products);
}
Here is the view :
<html>
<head>
<title> Item List</title>
<meta name="csrf-token" content="">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://ift.tt/2apRjw3" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<script src="http://ift.tt/2eBd7UN"></script>
<script src="http://ift.tt/2aHTozy"></script>
<meta name="csrf-token" content="">
</head>
<body>
<div class="container">
<div id="content">
<ul id="tabs" class="nav nav-tabs" data-tabs="tabs">
@foreach ($categories as $category)
<li><a href="showItem()" data-toggle="tab" ></a></li>
@endforeach
</ul>
<div id="my-tab-content" class="tab-content">
@foreach($categories as $category)
<div class="tab-pane " id="">
@foreach($products as $product)
@endforeach
</div>
@endforeach
</div>
</div>
</div>
<script>
function showItem(id) {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "POST",
url:"",
data:{id:id},
success: function(data) {
console.log(data);
}
});
}
</script>
</body>
</html>
Here is the route:
Route::get('/testItem',[
'uses'=>'ItemController@gettestItem',
'as'=>'testItem'
]);
Route::post('/showitems',[
'uses'=>'ItemController@showitems',
'as'=>'showitems'
]);
if anyone could help me solve the problem will be appreciated.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire