-3

Can you help me how can I solve this.. I am trying to display the values from the foreach array.. Relationship makes me more complicated to understand so I opt to do it on my way.

It displays an error of like this

Property [pic] does not exist on this collection instance.

$dataTagged = DB::table('tagging_tagged')
                ->where('tag_slug', '=',$slug)
                ->get();
foreach($dataTagged as $tagged){
$dataProductquantity[] = Productquantity::where('id','=',$tagged->taggable_id)->first();    
}

and my view is

@foreach($dataProductquantity as $Productquantity)
            <div class="col-lg-4 col-md-6 mb-4">
              <div class="card h-100">
                <a href="#"><img class="card-img-top" src="{{ asset('productimg') }}/{{$Productquantity->pic}}" alt=""></a>
                <div class="card-body">
                  <h4 class="card-title">
                    <a href="#">{{$Productquantity->product->product_name}}</a>
                  </h4>
                  <h5>&#8369; {{$Productquantity->price}}.00 </h5>
                  <p class="card-text">{{$Productquantity->description}}</p>
                </div>
                <div class="card-footer" align="center">
                <a class="btn btn-warning" align="center"><i class="icon-shopping-cart"></i><span style="color:#fff;">Add to Cart</span></a>
                </div>
              </div>
            </div>
            @endforeach

please help. Thank you

tereško
  • 58,060
  • 25
  • 98
  • 150
EasyWay
  • 365
  • 1
  • 5
  • 20
  • Your error message actually tells enough to solve this on your own. However... not sure how you build up your Models, but you probably should use `$Productquantity->product->pic`, because imho it makes no sense to have a `pic` property on a `Productquantity` model. Simply doublecheck where the `pic` propertly is located. Still, you should also check why `$Productquantity` in view is a collection and not the model itself. – Tschitsch Mar 12 '18 at 15:59

2 Answers2

2

Another way could be

public function methodName($slug) { 
    // Obtaining the TaggingTagged that include the needed slug. 
    $dataTagged = DB::table('tagging_tagged')->where('tag_slug', '=',$slug)->get();
    // or using the Model to obtain the data
    // $dataTagged = TaggingTagged::where('tag_slug', '=',$slug)->get();

    // Then Iterate into the $dataTagged Collection, the result is a new Collection
    $dataProductquantity = $dataTagged->each(function ($tagged, $key) {
        return Productquantity::where('id','=',$tagged->taggable_id)->first();
    });

    // Passing the $dataProductquantity to the view. You must need replace "viewName"
    return view('viewName')->with(['dataProductquantity' => $dataProductquantity]);
}
Maru Amallo
  • 430
  • 5
  • 11
  • Could be better if you can explain the code. Answers based only in copy&pasting code will be removed. – King Midas Mar 12 '18 at 15:20
  • Hi Jorge, it was not a copy&paste, I did not consider necessary a more detailed explanation, even I have added comments explaining the code. I hope my answer is now correctly explained and that it is not removed – Maru Amallo Mar 12 '18 at 15:52
  • 1
    With the comments now is more understandble. Thanks. – King Midas Mar 13 '18 at 09:44
1

Try this

$dataProductquantity = [];

foreach($dataTagged as $tagged){
   array_push($dataProductquantity,Productquantity::where('id','=',$tagged->taggable_id)->first());
}
Sérgio Reis
  • 2,483
  • 2
  • 19
  • 32