在JavaScript代码中访问Laravel关系
产品与表productbrand和productbrand之间存在一对多关系,“品牌”与表productbrand之间存在一对一关系。表brand有一列“brand”。我能够接触到产品的品牌。正确访问所有其他类别、用户名等在JavaScript代码中访问Laravel关系,javascript,php,laravel,Javascript,Php,Laravel,产品与表productbrand和productbrand之间存在一对多关系,“品牌”与表productbrand之间存在一对一关系。表brand有一列“brand”。我能够接触到产品的品牌。正确访问所有其他类别、用户名等 public function show(Request $request) { if($request->ajax()) { $id = $request->id; if($id) {
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);
}
}
});
在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
}
for(var i=0;i我认为您应该创建一个路由并使用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]);
}
并以与以前相同的方式在js中访问它
PS:我假设您使用的是laravel 5.2。为什么要声明所有这些变量:
$category = $show->category->category;
$username = $show->user->username;
$getbrands = $show->productbrand;
如果类别、用户、productbrand和then brands是与产品相关的模型,则从类别中获取类别、从用户获取用户名等
而是保持与"同"的关系,
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);
}
}
});
if(res.status==true)
{
变量结果='Category:'+res.show.Category.Category+'
'+
'产品作者:'+res.show.user.username+'
'+
'价格:'+res.show.Price+'
'+
'价格类型:'+res.show.Price_type+'
'+
'产品视图:'+res.show.Views+'
';
$('#result').html(result);
}
}
});
“产品品牌”与“餐桌产品品牌”具有一元关系,“产品品牌”与“餐桌品牌”具有一元关系。表品牌有一列“品牌”。而且我无法接触到产品的品牌。像这样访问品牌
var result = 'Brands: ';
for(var i=0; i<res.show.productbrand.length; i++)
{
result += res.show.productbrand[i].brand.brand;
}
var结果='品牌:';
对于(var i=0;iTry this:$show=Product::where(['Product_id'=>$id])->with('productbrand')->first();@autistaz,问题是使用javascript访问。我正在写一个答案,但我注意到您的代码非常非常奇怪。我真的建议您看看www.laracasts.com课程,您将学到很多关于laravel的知识。祝您好运。
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);
}
}
});
var result = 'Brands: ';
for(var i=0; i<res.show.productbrand.length; i++)
{
result += res.show.productbrand[i].brand.brand;
}