14

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.

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({
    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
    Try this: $show=Product::where(['product_id'=>$id])->with('productbrand')->first(); Commented Aug 12, 2016 at 11:37
  • @Autista_z the problem is accessing with javascript. Commented Aug 12, 2016 at 11:40
  • I was writting an answer, but I've noticed that your code is very, very strange. I really suggest you to take a look at www.laracasts.com lessons, you will learn a lot about laravel. Good luck. Commented Aug 21, 2016 at 1:35

3 Answers 3

12
+50

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.

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:

        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.

var result = 'Brands: ';
for(var i=0; i<res.show.productbrand.length; i++)
{
    result += res.show.productbrand[i].brand.brand;
}
Sign up to request clarification or add additional context in comments.

Comments

3

Using your first way.

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

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

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.

PS: I am assuming you are using laravel 5.2.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.