What is the type of $attribue_values ? I see the plural, is it an array ?
If yes, if(is_integer($this->attribute_values)) can only return false.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi everyone, l am try laravel/livewire e-commerce project . l created attribute value table to save product attribute, but that table do not work ;please need help.
<?php
namespace App\Livewire\Admin;
use Livewire\Component;
use Illuminate\Support\Str;
use App\Models\Category;
use Livewire\WithPagination;
use Livewire\WithFileUploads;
use App\Models\Product;
use App\Models\AttributeValue;
use App\Models\ProductAttribute;
use Carbon\Carbon;
class AdminAddProductComponent extends Component
{
use WithFileUploads;
public $name;
public $slug;
public $short_description;
public $description;
public $regular_price;
public $sale_price;
public $sku;
public $stock_status = 'instock';
public $image;
public $inputs = [];
public $attribute_arr = [];
public $attribute_values;
public $product_attribute_id;
public function generateSlug()
{
$this->slug = Str::slug($this->name);
}
public function add()
{
if(!in_array($this->attr,$this->attribute_arr))
{
array_push($this->inputs,$this->attr);
array_push($this->attribute_arr,$this->attr);
}
}
public function remove($attr)
{
unset($this->inputs[$attr]);
}
public function addProduct()
{
$this->validate([
'name'=> 'required',
'slug'=> 'required',
'short_description'=> 'required',
'description'=> 'required',
// 'images'=> 'required',
'category_id'=> 'required',
]);
$product = new Product();
$product->name = $this->name;
$product->slug = $this->slug;
$product->featured = $this->featured;
$product->quantity = $this->quantity;
$imageName = Carbon::now()->timestamp.'.'.$this->image->extension();
$this->image->storeAs('products',$imageName);
$product->image = $imageName;
$product->category_id = $this->category_id;
if(is_integer($this->attribute_values)){
foreach($this->attribute_values as $key=>$attribute_value)
{
$avalues = explode(",",$attribute_value);
foreach($avalues as $avalue)
{
$attr_value = new AttributeValue();
$attr_value->product_attribute_id = $key;
$attr_value->value = $avalue;
$attr_value->product_id = $product->id;
$attr_value->save();
}
}
}
$product->save();
// if(is_array($this->attribute_values)){
//AttributeValue::where('product_id',$product->id)->delete();
// $product->attr_value = $this->attr_value;
session()->flash('message','Product has been added !');
}
public function render()
{
$categories = Category::orderBy('name','ASC')->get();
$pattributes= ProductAttribute::all();
return view('livewire.admin.admin-add-product-component',['categories'=>$categories,'pattributes'=>$pattributes]);
}
}
My Model
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ProductAttribute extends Model
{
use HasFactory;
protected $fillable = [
'name',
];
public function product()
{
return $this->belongsTo(Product::class);
}
public function attributeValues()
{
return $this->hasMany(AttributeValue::class);
}
}
PRoduct attribute table
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class AttributeValue extends Model
{
use HasFactory;
protected $fillable = [
'value',
'product_id',
'product_attribute_id',
];
public function product()
{
return $this->belongsTo(Product::class);
}
public function pattributes()
{
return $this->hasMany(ProductAttribute::class);
}
}
Table
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('attribute_values', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('product_attribute_id')->nullable();
$table->string('value');
$table->unsignedBigInteger('product_id')->nullable();
$table->foreign('product_attribute_id')->references('id')->on('product_attributes')->onDelete('cascade');
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('attribute_values');
}
};
you needs to refactor the code in your form.
each time user click add attr build two inputs inside @foreach($inputs as $key => $value)
one for input type hidden, the value for this hidden inputs is attr id
and second input is for the value
with this technique you will have the array data you needs.
and for the controller the foreach task is more like this foreach ($this->inputs as $input)
Please or to participate in this conversation.