Object of class stdClass could not be converted to string

Published 7 months ago by Shawdow

can any one tell me what am i doing wrong here??

public function category{

 $subid = DB::table('products')->select('category_id')->get();

 $attr = DB::table('attributes')->select('*')->where('id','=',$subid)->get();
        
echo $attr;

}

Best Answer (As Selected By Shawdow)
LaraStorm

use this way

$subid =  \App\Product::pluck('category_id');

$attr = \App\Attribute::whereIn('id',$subid)->get();

echo $attr;

ModestasV

To start with - you are trying to echo a Collection() class. The query builder returns collection/array (depending on version you use) and you can't use echo there. To test if the data is correct you can use one of these two: dd($attr); or dump($attr); - they will print out what was returned.

LaraStorm

you have to use foreach

foreach ($subid as $sid) {

$attr = DB::table('attributes')->select('*')->where('id',$sid)->get();

}

and i prefer to not to use DB Query u can simply use model query

$attr = Attribute::where('id',$sid)->get();

Shawdow

@LaraStorm it doesn't works getting same error

            $subid = DB::table('products')->select('category_id')->get();
        
              foreach ($subid as $sid) {

                 $attr = \App\Attribute::where('id',$sid)->get();

             }
             
             echo $attr;

Shawdow

@ModestasV I have tried with dd() and dump() also shows me same error

LaraStorm

use this way

$subid =  \App\Product::pluck('category_id');

$attr = \App\Attribute::whereIn('id',$subid)->get();

echo $attr;

Shawdow

@LaraStorm yeah it work small doubt!

how can get the whole table value in the attributes table

example if i have an id column in the attributes table can i get it in this way in blade file

@foreach($attr as $attrs)

$attrs->id

@endforeach

LaraStorm

use this

$subid =  \App\Product::all();

foreach($subid as $sid) {

    $attr = \App\Attribute::where('id',$sid->category_id)->get();

}

return view('any-view')->with('attr',$attr);

Shawdow

@LaraStorm no sir it does not works i am trying to do like below in blade file

@foreach($subid as $subids)
    @foreach($attr as $attrs)

  @if($attrs->id  == $subid->attribute_id)
    {{$attrs->attribute_1 }} <input  type="checkbox">
   @endif
@endforeach

@endforeach

LaraStorm

can you show me your controller function here ?

Shawdow

@LaraStorm since the topic was new one i started with new thread you can see it sir

https://laracasts.com/discuss/channels/laravel/trying-to-get-property-of-non-object-53

august

Try something like this maybe: Your controller:

public function category() {
    $categoryIds = \App\Product::distinct()->pluck('category_id'); 
    $attributes = \App\Attribute::whereIn('id', $categoryIds)->get();

    return view('category', compact('attributes');
}

Your category.blade.php:

@foreach($attributes as $attribute)
    {{ $attrs->attribute_1 }} <input type="checkbox">
@endforeach
Shawdow

@august it fetches all the attribute_1 value sir i am trying if the products table attribute_id matches with attribute's table id column

august

Correct me if I'm wrong. What you trying to do is display for each Product all available attributes with checkboxes, and display it to user. In example (based on your reply https://laracasts.com/discuss/channels/laravel/trying-to-get-property-of-non-object-53/replies/400855)

Samsung:

  • Color
  • Size
  • etc.

Other Product

  • Attribute 1
  • Attribute 2
  • ...

For me it matches different DB model. Product can have many Attributes (color, size, etc.) and Attribute can be assigned to many Products (some attributes can be reused) You can find more info here: https://laravel.com/docs/5.5/eloquent-relationships#many-to-many.

But if you want to display all products like in example at beginning of this reply then with your current setup you should define relation from Product to Attribute Product.php

class Product {
    public function attributes()
    {
        return $this->belongsTo('App\Attribute');
    }
}

Controller

public function category() {
    $products = \App\Product::with('attributes')->get();

    return view('category', compact('products');
}

category.blade.php:

@foreach($products as $product)
    <p>{{ $product->name }}<p>
    {{ $product->attributes->attribute_1 }} <input type="checkbox">
    {{ $product->attributes->attribute_2 }} <input type="checkbox">
    // And so on... 
@endforeach

You have to hardcode every new "attribute_N" or if you know how much attributes have you defined then use something like this (only if there are no breaks in naming of columns):

category.blade.php:

@foreach($products as $product)
    <p>{{ $product->name }}<p>
    @for($i = 1; $i < N; $++)
        {{ $product->attributes["attribute_{$i}"] }} <input type="checkbox">
    @endforeach
@endforeach

but I really do not recommend this approach.

If you want display only attributes for one product change category function in Controller to accept $id, and replace ->get() to ->findOrFail($id) and drop first @foreach loop in category.blade.php.

Shawdow

@august i will define relationship for both attributes and products table according @tykus sir replied and work on it

since i am new to laravel above concepts does not get into my mind thank you :)

https://laracasts.com/discuss/channels/laravel/trying-to-get-property-of-non-object-53

Please sign in or create an account to participate in this conversation.