I have items and for every item there are related items, so when I open the homepage it shows all item, when I want to click on any item, ajax will pass this item id to controller to get the related items for this item, the problem is I want to show the related item in a modal dialogue, the modal dialogue now show all related items to all items not to the current item. I think the problem is because the include of modal in the homepage which has the foreach!, hope you can help me in solving this issue
route
Route::get('/',['as'=>'showItems','uses'=>'HomeController@getItem']); Route::get('Get_relateditem',['as'=>'Get_relateditem','uses'=>'HomeController@getItem']);
ajax
$(function(){ $('.Item_root').on("click", function () { var item_id = $(this).data('id'); $.ajax({ type:'get', url: '/', data: { '_token': $('input[name=_token]').val(), 'item_id':item_id, }, success:function(data){} }); }); });
controller
public function getItem(Request $request) { $currentitemid =$request->item_id; $ritems = Relateditem::orderBy('id', 'asc')->where('ritemf_id','LIKE','%'.$currentitemid.'%')->with('items')->get()->groupBy('ritemf_id'); $items = Item::orderBy('category_id', 'asc')->with('category')->get()->groupBy('category_id'); $categories = Category::orderBy('category_id', 'asc')->get(); return view('home')->with('items',$items)->with('categories',$categories)->with('ritems',$ritems); } }
modal
@foreach($ritems as $item_id => $realtedItems) @foreach($realtedItems as $ritem) <div class="SuggestedItem_container"> <label color="red" class="Checker_root Checker_red Checker_left"> <input type="checkbox" class="Checker_input" value="on"> <div class="SuggestedItem_nameContainer"> <div> <span class="SuggestedItem_name">{{$ritem->riteml_id}}</span> <span class="SuggestedItem_price styles_small styles_base styles_spacing-base">+$3.95</span></div></div> <div class="SuggestedItem_description styles_small styles_base styles_spacing-base"> <span class="SuggestedItem_category styles_bold">Appetizers</span> <span> ยท Edamame soybean pods harvested right before the beans begin to harden are lightly boiled and seasoned with sea salt.</span> </div> </label> </div> @endforeach @endforeach
Advertisement
Answer
Modify routes:
Route::get('/','HomeController@getItem'); Route::get('/get_related_items/{id}','HomeController@getRelatedItems');
modify getItem
to get only the items and categories:
public function getItem(Request $request) { $items = Item::orderBy('category_id', 'asc') ->with('category')->get() ->groupBy('category_id'); $categories = Category::orderBy('category_id', 'asc')->get(); return view('home',['items' => $items,'categories' => $categories]); }
get related items for a single item id:
public function getRelatedItems(Request $request, $id) { $ritems = Relateditem::orderBy('id', 'asc') ->where('ritemf_id',$id) ->with('items') ->get() ->groupBy('ritemf_id'); return response()->json($ritems); }
now for the js part:
$(function(){ $('.Item_root').on("click", function () { var item_id = $(this).data('id'); $.ajax({ type:'get', url: '/get_related_items/' + item_id, data: { '_token': $('input[name=_token]').val() }, success:function(data){ if(data && data.length){ var con_el = $('.SuggestedItem_container'); for(var i = 0; i < data.length;i++){ var related_item_el = "<div class='related_item'><p>" + data[i].id + "</p></div>" con_el.append(related_item_el); } }else{ console.log('no related items found'); } } }); }); });
this will insert all the related items inside the SuggestedItem_container
i didn’t write the view template cuz that part is easy, and note that i only included the related item id as example cuz i don’t know what fields the item has. i hope this helps you