I want to display Size Attribute value list beside Size using laravel

For Attribute i can make two table
1 is attributes and another is attribute_values

For attributes table

Schema::create('attributes', function (Blueprint $table) {
      $table->bigIncrements('id');
      $table->string('attributeName')->unique();
      $table->timestamps();
  });

For attribute_values table

Schema::create('attribute_values', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('attributeId');
    $table->string('value')->unique();
    $table->timestamps();
});

I want to show like:

Under Size: L, S, M, XL etc

Under Color: Red, Green etc

What is the query ? and how can i apply ?

I want to display Size Attribute value list beside Size using laravel

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

values is hasMany relationship

$attributes = Attribute::with('values')->get();

https://laravel.com/docs/8.x/eloquent-relationships#one-to-many

In the view

@foreach ($attributes as $attribute)
    {{ $attribute->values->pluck('value')->join(', ') }}
@endforeach

https://laravel.com/docs/8.x/collections#method-pluck

https://laravel.com/docs/8.x/collections#method-join


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x