2016-08-12 120 views
13

產品與表格品牌具有一對多關係'productbrand',產品品牌與表格品牌具有一對一關係'品牌'。表品牌有一個專欄,'品牌'。而且我能夠訪問產品的品牌。所有其他類別,用戶名等都可以正確訪問。在JavaScript代碼中訪問Laravel關係

public function show(Request $request) 
{ 
    if($request->ajax()) 
    { 
     $id = $request->id; 
     if($id) 
     { 
      $show = Product::where(['product_id'=>$id])->first(); 
      $category = $show->category->category; 
      $username = $show->user->username; 
      $getbrands = $show->productbrand; 
      foreach($getbrands as $getbrand) 
      { 
       $brand=$getbrand->brand->brand; 
      } 
      if($show) 
      { 
       echo json_encode(array('status' => TRUE, 'show' => $show, 'username' => $username, 'category' => $category, 'brand' => $brand)); die; 
      } 
     } 
    } 
    echo json_encode(FALSE);die; 
} 

的Ajax和jQuery:

$.ajax({ 
    type: "POST", 
    url: "{{url('/product/show')}}", 
    data: {id:id}, 
    success: function (data) { 
     var res = $.parseJSON(data); 
     if(res.status == true) 
     { 
      var result = 'Category: ' + res.category + '<br>' + 
         'Product by: ' + res.username + '<br>' + 
         'Brands: ' + res.brand + '<br>' + 
         'Price: ' + res.show.price + '<br>' + 
         'Price type: ' + res.show.price_type + '<br>' + 
         'Product Views: ' + res.show.views + '<br>'; 
      $('#result').html(result); 
     } 
    } 
}); 

這樣我能得到只有一個品牌。我也嘗試了以下方法,但失敗了。

在控制器:

$getbrands = $show->productbrand; 
echo json_encode(array('status' => TRUE, 'show' => $show, 'username' => $username, 'category' => $category, 'getbrands' => $getbrands)); 

在阿賈克斯:

for(var i=0; i<res.getbrands.length; i++) 
{ 
    var brands=res.getbrands[i].brand.brand; //First 'brand' is relation and second is the brand I am trying to access 
} 
+0

嘗試這種情況: $顯示=產品::其中([ 'PRODUCT_ID'=> $ ID]) - >用( 'productbrand') - >第一(); –

+0

@Autista_z問題是使用JavaScript訪問。 – micky

+0

我正在寫一個答案,但我注意到你的代碼非常非常奇怪。我真的建議你看一下www.laracasts.com課程,你會學到很多關於laravel的知識。祝你好運。 –

回答

6

爲什麼聲明所有這些變量:如果類別,用戶,productbrand然後品牌型號與產品的關係,並獲取從類別分類,從用戶的用戶名等

而是保持

$category = $show->category->category; 
$username = $show->user->username; 
$getbrands = $show->productbrand; 

與'與'的關係;

public function show(Request $request) 
{ 
    if($request->ajax()) 
    { 
     $id = $request->id; 
     if($id) 
     { 
      $show = Product::where(['product_id'=>$id])->with('category') 
                 ->with('user') 
                 ->with('productbrand.brand') // product has onetomany relation 'productbrand' with table productbrand and productbrand has onetoone relation 'brand' with table brand 
                 ->first(); 
      if($show) 
      { 
       echo json_encode(array('status' => TRUE, 'show'=>$show)); die; 
      } 
     } 
    } 
    echo json_encode(FALSE);die; 

而且在JavaScript:

 if(res.status == true) 
     { 
      var result = 'Category: ' + res.show.category.category + '<br>' + 
         'Product by: ' + res.show.user.username + '<br>' + 
         'Price: ' + res.show.price + '<br>' + 
         'Price type: ' + res.show.price_type + '<br>' + 
         'Product Views: ' + res.show.views + '<br>'; 
      $('#result').html(result); 
     } 
    } 
}); 

產品具有表productbrand一對多關係 'productbrand' 和productbrand具有onetoone關係 '品牌' 與表品牌。表品牌有一個專欄'品牌'。我無法訪問產品的品牌。訪問這樣的品牌。

var result = 'Brands: '; 
for(var i=0; i<res.show.productbrand.length; i++) 
{ 
    result += res.show.productbrand[i].brand.brand; 
} 
2

我想你應該創建一個路由,使用AJAX來得到你需要的數據。 一旦你將數據傳遞給javascript使用的DOM,它就沒有任何關於你在Laravel中定義的對象關係的知識。

3

使用你的第一種方式。

變化$brand=$getbrand->brand->brand;$brand[] = $getbrand->brand->brand;

然後在JS,你可以去在這樣的牌子:

for(var i=0; i < res.brand.length; i++) 
{ 
    console.log(brand[i]); 
} 

這樣然而讓品牌會做很多數據庫查詢。

相反,您可以使用預先加載。

像這樣:

$show = Product::with('productbrand.brand')->where(['product_id'=>$id])->first(); 
$brand = $show->productbrand->pluck('brand')->collapse(); 

,並獲得同樣的方式在JS像以前一樣。 PS:我假設你使用的是laravel 5.2。