In this video we are going to learn about Add Attribute Option on Edit Product Page.
So let see how can we Add Attribute Option on Edit Product Page.
First of lets open the Product Model
So go inside the app directory then models and from here lets open Product.php file
Now here lets add a function here



public function attributeValues()
{
return $this->hasMany(AttributeValue::class,'product_id');
}


Now go to the AdminEditProductComponent.php class file and here
Lets create the properties


public $attr;
public $inputs = [];
public $attribute_arr = [];
public $attribute_values=[];


Now inside the mount method add the following code.


public function mount($product_slug)
{
$product = Product::where('slug',$product_slug)->first();
$this->name = $product->name;
$this->slug = $product->slug;
$this->short_description = $product->short_description;
$this->description = $product->description;
$this->regular_price = $product->regular_price;
$this->sale_price = $product->sale_price;
$this->SKU = $product->SKU;
$this->stock_status = $product->stock_status;
$this->featured = $product->featured;
$this->quantity = $product->quantity;
$this->image = $product->image;
$this->images = explode(",",$product->images);
$this->category_id = $product->category_id;
$this->scategory_id = $product->subcategory_id;
$this->product_id = $product->id;
$this->inputs = $product->attributeValues->where('product_id',$product->id)->unique('product_attribute_id')->pluck('product_attribute_id');
$this->attribute_arr = $product->attributeValues->where('product_id',$product->id)->unique('product_attribute_id')->pluck('product_attribute_id');

foreach($this->attribute_arr as $a_arr)
{
$allAttributeValue = AttributeValue::where('product_id',$product->id)->where('product_attribute_id',$a_arr)->get()->pluck('value');
$valueString ='';
foreach($allAttributeValue as $value)
{
$valueString = $valueString . $value . ',';
}
$this->attribute_values[$a_arr] = rtrim($valueString,",");
}
}


Alright, now inside the render method lets fetch here all attributes and return this to the component view file as following


public function render()
{
$categories = Category::all();
$scategories = Subcategory::where('category_id',$this->category_id)->get();
$pattributes = ProductAttribute::all();
return view('livewire.admin.admin-edit-product-component',['categories'=>$categories,'scategories'=>$scategories,'pattributes'=>$pattributes])->layout('layouts.base');
}



Now lets open the admin-add-product-component.blade.php file
And from here lets copy these line of code from here
And in this edit product view file just after this paste here.
Now go to the class file and here inside the update method just write add the following code



AttributeValue::where('product_id',$product->id)->delete();
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();
}
}



Alright now add here add function for adding the attribute and also create a function for delete attribute



public function add()
{
if(!$this->attribute_arr->contains($this->attr))
{
$this->inputs->push($this->attr);
$this->attribute_arr->push($this->attr);
}
}

public function remove($attr)
{
unset($this->inputs[$attr]);
}


Now its done so lets check it
So switch to the browser and refresh the page
Now lets edit this product and here
Inside the attribute lets add one more colour here
And in size just remove this size and add another size here
Now click on update
And you can see the message product updated successfully
And if refresh the page
Here you see the updated value.
If I check in database table
So go the phpMyAdmin and open the database now browser the table and here you can see the
updated attriubtes values.
So in this way you can Add Attribute Option on Edit Product Page.