Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel如何使用ajax将数据从控制器传递到模式对话_Php_Jquery_Laravel - Fatal编程技术网

Php Laravel如何使用ajax将数据从控制器传递到模式对话

Php Laravel如何使用ajax将数据从控制器传递到模式对话,php,jquery,laravel,Php,Jquery,Laravel,我有项目,每个项目都有相关项目,因此当我打开主页时,它会显示所有项目,当我想点击任何项目时,ajax会将该项目id传递给控制器,以获取该项目的相关项目,问题是我想在模式对话中显示相关项目,模式对话框现在将所有相关项显示为所有项,而不是当前项。 我想问题是因为在主页中包含了modal,它有foreach!,希望你能帮我解决这个问题 路线 Route::get('/',['as'=>'showItems','uses'=>'HomeController@getItem']); Route

我有项目,每个项目都有相关项目,因此当我打开主页时,它会显示所有项目,当我想点击任何项目时,ajax会将该项目id传递给控制器,以获取该项目的相关项目,问题是我想在模式对话中显示相关项目,模式对话框现在将所有相关项显示为所有项,而不是当前项。 我想问题是因为在主页中包含了modal,它有foreach!,希望你能帮我解决这个问题

路线

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){}
    }); 
   });
  });
控制器

 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);
}
}
模态

@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
@foreach($ritems作为$item\u id=>$realtedItems)
@foreach($realtedItems作为$ritem)
{{$ritem->riteml\u id}
+$3.95
开胃菜
·在豆子开始变硬之前收获的毛豆豆荚被轻轻煮沸并用海盐调味。
@endforeach
@endforeach
修改路线:

Route::get('/','HomeController@getItem');
Route::get('/get_related_items/{id}','HomeController@getRelatedItems');
修改
getItem
以仅获取项目和类别:

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]);
}
获取单个项目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);

}
现在来看js部分:

$(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');
        }
      }
    });
  });
});
$(函数(){
$('.Item_root')。在(“单击”,函数(){
var item_id=$(this).data('id');
$.ajax({
类型:'get',
url:'/get_related_items/'+item_id,
数据:{
“_-token”:$(“输入[name=_-token]”)。val()
},
成功:功能(数据){
if(数据和数据长度){
var con_el=$('.SuggestedItem_container');
对于(变量i=0;i”
合同附件(相关项目);
}
}否则{
console.log(“未找到相关项”);
}
}
});
});
});
这将在
SuggestedItem\u容器中插入所有相关项目

我没有编写视图模板,因为这部分很简单,并且注意,我只包含了相关的项目id作为示例,因为我不知道该项目有哪些字段。
我希望这能帮助你

很多THNAK,不幸的是,我是一个初学者,我应用了所有步骤,但不知道如何将ajax中的变量放入模式,也从控制台我得到了错误:404(未找到)do
php artisan route:clear
,它将清除路由缓存,让我知道是否已修复,我建议您注意以下内容:相同的错误:404(未找到)您是否在第一步中按原样添加了路由?