提问人:micky 提问时间:8/12/2016 最后编辑:Peter Mortensenmicky 更新时间:2/12/2017 访问量:4281
在 JavaScript 代码中访问 Laravel 关系
Accessing Laravel relations in JavaScript code
问:
产品具有一对多关系,“productbrand”,与表 productbrand 和 productbrand 具有一对一关系,“brand”,与表品牌。表品牌有一列,“品牌”。并且我能够访问产品的品牌。所有其他类别、用户名等都可以正确访问。
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));
在 Ajax 中:
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
}
答:
我认为您应该创建一个路由并使用 AJAX 来获取所需的数据。 一旦你把数据传递给 javascript 使用的 DOM,它就不了解你在 Laravel 中定义的对象关系。
使用您的第一种方式。
更改为$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。
为什么要声明所有这些变量:
$category = $show->category->category;
$username = $show->user->username;
$getbrands = $show->productbrand;
如果类别、用户、产品品牌和品牌是与产品的关系,并从类别中获取类别,从用户中获取用户名等。
相反,保持与“with”的关系;
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);
}
}
});
Product 具有一对多关系“ProductBrand”与表 productBrand 和 productBrand 具有一对一关系“Brand”。 表品牌有一个列“brand”。并且我无法访问产品的品牌。访问这样的品牌。
var result = 'Brands: ';
for(var i=0; i<res.show.productbrand.length; i++)
{
result += res.show.productbrand[i].brand.brand;
}
上一个:如何从网站上阅读?[已结束]
评论