在JavaScript代码中访问Laravel关系

时间:2022-10-16 00:09:36

Product has a one-to-many relation, 'productbrand', with table productbrand and productbrand has a one-to-one relation, 'brand', with table brand. Table brand has a column, 'brand'. And I am able to access the brand of the product. All other categories, username, etc. are accessed properly.

产品具有一对多的关系,'productbrand',表productbrand和productbrand具有一对一的关系,'品牌',与表品牌。表品牌有一个专栏,'品牌'。我可以访问该产品的品牌。可以正确访问所有其他类别,用户名等。

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 and jQuery:

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

This way I could get only one brand. I have also tried the following way, but failed.

这样我只能得到一个品牌。我也试过以下方式,但失败了。

In the controller:

在控制器中:

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

In 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
}

3 个解决方案

#1


8  

Why declare all these variables:

为什么要声明所有这些变量:

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

If category, user, productbrand and then brands are models relation with product and to fetch category from category, username from user, etc.

如果category,user,productbrand和brands是与产品的模型关系,并从类别,用户的用户名等获取类别。

Instead maintain the relations with '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;

And in JavaScript:

在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 has onetomany relation 'productbrand' with table productbrand and productbrand has onetoone relation 'brand' with table brand. table brand has a column 'brand'. And am not not being able to access the brand of the product. Access brand like this.

产品与表品牌和产品品牌有关联'productbrand'与表品牌有关系'品牌'。表品牌有一个专栏'品牌'。并且我无法访问该产品的品牌。像这样访问品牌。

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

#2


3  

Using your first way.

用你的第一种方式。

Change $brand=$getbrand->brand->brand; to $brand[] = $getbrand->brand->brand;

改变$ brand = $ getbrand-> brand-> brand; to $ brand [] = $ getbrand-> brand-> brand;

And then in js you can go over the brands like this:

然后在js你可以浏览这样的品牌:

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

However getting brands like this will do a lot of database query.

但是,获得这样的品牌会进行大量的数据库查询。

Instead, you can use eager loading.

相反,您可以使用预先加载。

Like this:

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

And access it the same way in js as before.

并在js中以与以前相同的方式访问它。

PS: I am assuming you are using laravel 5.2.

PS:我假设你正在使用laravel 5.2。

#3


2  

I think you should create a route and use AJAX to get the data you need. Once you pass the data to the DOM that javascript uses it does not have any knowledge about the object relations you defined in Laravel.

我认为您应该创建一个路由并使用AJAX来获取所需的数据。一旦将数据传递给javascript使用的DOM,就无法了解您在Laravel中定义的对象关系。

#1


8  

Why declare all these variables:

为什么要声明所有这些变量:

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

If category, user, productbrand and then brands are models relation with product and to fetch category from category, username from user, etc.

如果category,user,productbrand和brands是与产品的模型关系,并从类别,用户的用户名等获取类别。

Instead maintain the relations with '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;

And in JavaScript:

在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 has onetomany relation 'productbrand' with table productbrand and productbrand has onetoone relation 'brand' with table brand. table brand has a column 'brand'. And am not not being able to access the brand of the product. Access brand like this.

产品与表品牌和产品品牌有关联'productbrand'与表品牌有关系'品牌'。表品牌有一个专栏'品牌'。并且我无法访问该产品的品牌。像这样访问品牌。

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

#2


3  

Using your first way.

用你的第一种方式。

Change $brand=$getbrand->brand->brand; to $brand[] = $getbrand->brand->brand;

改变$ brand = $ getbrand-> brand-> brand; to $ brand [] = $ getbrand-> brand-> brand;

And then in js you can go over the brands like this:

然后在js你可以浏览这样的品牌:

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

However getting brands like this will do a lot of database query.

但是,获得这样的品牌会进行大量的数据库查询。

Instead, you can use eager loading.

相反,您可以使用预先加载。

Like this:

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

And access it the same way in js as before.

并在js中以与以前相同的方式访问它。

PS: I am assuming you are using laravel 5.2.

PS:我假设你正在使用laravel 5.2。

#3


2  

I think you should create a route and use AJAX to get the data you need. Once you pass the data to the DOM that javascript uses it does not have any knowledge about the object relations you defined in Laravel.

我认为您应该创建一个路由并使用AJAX来获取所需的数据。一旦将数据传递给javascript使用的DOM,就无法了解您在Laravel中定义的对象关系。