Laravel如果ID将变量传递给模式对话

时间:2018-07-01 12:48:59

标签: php jquery laravel

我有3个表,categoriesitemsrelateditems。现在,我可以在主页中显示这3个表,这样我就可以看到类别名称,在它下面的项目标题和在项目标题下的项目都可以查看。

我想在模式对话框中显示变量,在主页上我可以毫无问题地显示它,但是由于其ID,我想显示特定的项目及其相关项目,现在我只能在模式中显示项目,但是t显示相关项目

Home.blade

@foreach($ritems as $categoryId => $groupItems)
    <div>
        <p>
            @if (!empty($groupItems->first()->category))        
                {{ $groupItems->first()->category->category_name }}
            @else
                {{$categoryId}}
            @endif
        </p>
    </div>
    @foreach($groupItems as $item)
        <button id="Item_root" data-titlefield="{{$item->item_title}}" class="Item_root">
            <span>{{$item->item_title}}</span>
            <p> related:</p>
            @foreach($item->relateditems as $relatedItem)
                @if ($relatedItem->item) 
                    {{$relatedItem->item->item_title}}  
                @endif
            @endforeach
        </button>
    @endforeach
@endforeach

javascript

<script>
    $(function(){  
        $('.Item_root').on("click", function () { 
            $('#myModal').modal('show');
            $('#title').text($(this).data('titlefield'));
        }); 
    });
</script>

控制器

   $ritems = Item::orderBy('category_id', 'asc')->with('category','relateditems', 'relateditems.item')->get()->groupBy('category_id');

模型

类别模型

 public function items() {

    return $this->hasMany('App\Item', 'category_id');
  }

项目模型

  public function category() 
 {
    return $this->belongsTo('App\Category', 'category_id');
 }

  public function relateditems() 
 {
return $this->hasMany('App\Relateditem', 'ritemf_id');     
 }

相关项目模型

 public function item() 
  {  
    return $this->belongsTo('App\Item', 'riteml_id');
   }

1 个答案:

答案 0 :(得分:1)

根据我们在聊天中的长时间讨论,您必须在模型视图中使用ajax:

$(function() {
  $(".Item_root").on("click", function() {
    var btn = $(this);
    var itemid = $(this).data("item-id");

    $.ajax({
      url: "url/to/router/" + itemid,
      type: "get",
      success: function(d) {
        $("#myModal").modal("show");
        $("#title").text(btn.data("titlefield"));
        $("#related_item").html(d);
      }
    });
  });
});

要获取相关项目,您必须添加类似以下内容的控制器功能:

function getRelatedItems()
{
    $currentid = request('item_id');
    $nritems = Item::where('item_id', $currentid)->first()->relateditems()->get();
    foreach ($nritems as $rItem) {

        $ItemDetails = Item::find($rItem->riteml_id);


        echo '<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 id="related_item" class="SuggestedItem_name">' . $ItemDetails->Item_title . '</span> 

            <span class="SuggestedItem_price styles_small styles_base styles_spacing-base">related item price</span></div></div> 

            <div class="SuggestedItem_description styles_small styles_base styles_spacing-base"> 
            <span class="SuggestedItem_category styles_bold">related item category</span> 

            <span>related item details</span> 
            </div> 
            </label> 
            </div>';
    }


}

祝你好运!