File: /var/www/api.ayokah.co.uk/app/Http/Controllers/Seller/AttributeController.php
<?php
namespace App\Http\Controllers\Seller;
use App\Models\Attribute;
use Illuminate\Http\Request;
use App\Models\AttributeValue;
use App\Models\ProductVariation;
use App\Http\Controllers\Controller;
class AttributeController extends Controller
{
public function getAttribute()
{
$attributes = Attribute::latest()->get();
return response()->json(['status' => 'success', 'message' => 'successfully fetched', 'data' => $attributes], 200);
}
public function saveAttribute(Request $request)
{
try {
$attributes = Attribute::updateOrCreate(
['name' => $request->input('name')],
$request->all()
);
return response()->json([
'status' => 'success',
'message' => $attributes->wasRecentlyCreated ? 'Successfully added' : 'Successfully updated',
'data' => $attributes
], 200);
} catch (\Throwable $th) {
return response()->json(['status' => 'success', 'message' => 'successfully fetched', 'data' => $th->getMessage()], 500);
}
}
public function saveAttributeValue(Request $request)
{
try {
$attributes = AttributeValue::updateOrCreate(
['value' => $request->input('value')],
$request->all()
);
return response()->json([
'status' => 'success',
'message' => $attributes->wasRecentlyCreated ? 'Successfully added' : 'Successfully updated',
'data' => $attributes
], 200);
} catch (\Throwable $th) {
return response()->json(['status' => 'success', 'message' => 'successfully failed', 'data' => $th->getMessage()], 500);
}
}
public function getAttributeValue()
{
try {
$attributes = AttributeValue::latest()->get();
return response()->json([
'status'=> 'success',
'message'=> 'fetched successfully',
'data'=> $attributes,
],200);
} catch (\Throwable $th) {
return response()->json(['status' => 'success', 'message' => 'successfully failed', 'data' => $th->getMessage()], 500);
}
}
public function saveProductAttributeValue(Request $request)
{
try {
$attributes = ProductVariation::updateOrCreate(
['product_id'=> $request->input('product_id')],
$request->all()
);
return response()->json([
'status' => 'success',
'message' => $attributes->wasRecentlyCreated ? 'Successfully added' : 'Successfully updated',
'data' => $attributes
], 200);
} catch (\Throwable $th) {
return response()->json(['status' => 'success', 'message' => 'successfully failed', 'data' => $th->getMessage()], 500);
}
}
}