Skip to content

Commit 4cb47c3

Browse files
committed
Polymorphic RelationShip ManyToMany between Category & Product: Used these method to query in DB attach,detach or ->categories()->sync([1,4,3]);
1 parent 4452969 commit 4cb47c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+11782
-6426
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Http\Resources\AttributeResource;
7+
use Illuminate\Http\Request;
8+
use App\Models\Admin;
9+
use App\Models\Attribute;
10+
use App\Traits\UploadAble;
11+
12+
use Illuminate\Support\Facades\Gate;
13+
use Illuminate\Support\Facades\Auth;
14+
use DB;
15+
16+
class AttributeController extends Controller
17+
{
18+
use UploadAble;
19+
20+
public function __construct()
21+
{
22+
$this->authorizeResource(Attribute::class, 'attribute');
23+
}
24+
public function index(Request $request)
25+
{
26+
return AttributeResource::collection(
27+
Attribute::whereNull('parent_id')->with('values')->latest()->get()
28+
);
29+
}
30+
public function values(Request $request)
31+
{
32+
return AttributeResource::collection(
33+
Attribute::whereNotNull('parent_id')->with('value')->latest()->get()
34+
);
35+
}
36+
37+
/**
38+
* Store a newly created resource in storage.
39+
*
40+
* @param \Illuminate\Http\Request $request
41+
* @return \Illuminate\Http\Response
42+
*/
43+
public function store(Request $request)
44+
{
45+
$this->validate($request , [
46+
'name' => 'bail|required|min:3',
47+
'slug' => $request->slug ? "required|alpha_dash|unique:attributes,slug" : "",
48+
'parent_id' => $request->parent_id ? 'required' : ''
49+
]);
50+
51+
$data = Attribute::create($request->all()); // actually subattribute created
52+
return new AttributeResource($data);
53+
}
54+
public function multiDelete(Request $request)
55+
{
56+
$this->authorize('multi_delete');
57+
try {
58+
59+
foreach ($request->all() as $attribute) {
60+
DB::beginTransaction();
61+
Attribute::find($attribute['id'])->delete();
62+
DB::commit();
63+
}
64+
return response()->json(['message' => "SUCCESS"], 200);
65+
} catch (\Throwable $th) {
66+
DB::rollback();
67+
}
68+
}
69+
/**
70+
* Display the specified resource.
71+
*
72+
* @param int $id
73+
* @return \Illuminate\Http\Response
74+
*/
75+
public function show(Attribute $attribute)
76+
{
77+
return new AttributeResource($attribute);
78+
}
79+
80+
/**
81+
* Update the specified resource in storage.
82+
*
83+
* @param \Illuminate\Http\Request $request
84+
* @param int $id
85+
* @return \Illuminate\Http\Response
86+
*/
87+
public function update(Request $request, Attribute $attribute)
88+
{
89+
$this->validate($request , [
90+
'name' => 'required',
91+
'slug' => 'required|unique:attributes,slug,'.$request->id,
92+
'parent_id' => $request->parent_id ? 'required' : ''
93+
]);
94+
// $attribute = Attribute::where('id',$request->id);
95+
return $attribute->update($request->except('id'));
96+
// return new AttributeResource($request->all());
97+
}
98+
99+
/**
100+
* Remove the specified resource from storage.
101+
*
102+
* @param int $id
103+
* @return \Illuminate\Http\Response
104+
*/
105+
public function destroy(Request $request , Attribute $attribute)
106+
{
107+
return $attribute->delete();
108+
}
109+
}

app/Http/Controllers/Admin/CategoryController.php

+28-5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Illuminate\Support\Facades\Gate;
1212
use Illuminate\Support\Facades\Auth;
1313
use DB;
14+
use Illuminate\Support\Facades\Storage;
1415

1516
use function PHPUnit\Framework\isNull;
1617

@@ -27,15 +28,37 @@ public function __construct()
2728
*
2829
* @return \Illuminate\Http\Response
2930
*/
30-
public function index(Request $request)
31+
public function all_categories(Request $request)
3132
{
32-
// these code must be efactored - we wil need this again and again
33-
return Category::where('parent_id',1)->with('subcategories')->where('id','!=',1)->latest()->get();
33+
if($request->has('with') && $request->with == 'subcategories'){
34+
return Category::with('subcategories')->latest()->get();
35+
}
36+
if($request->has('with') && $request->with == 'products'){
37+
return Category::with('products')->latest()->get();
38+
}
39+
if($request->has('with') && $request->with == 'subcategories' || $request->with == 'products' ) {
40+
return Category::with('subcategories')->with('products')->latest()->get();
41+
}
42+
return Category::latest()->get()->map(function($cat)
43+
{
44+
return [
45+
'id' => $cat->id,
46+
'name' => $cat->name,
47+
'slug' => $cat->slug,
48+
'icon' => $cat->icon ? url($cat->icon) : Storage::url('categories/default.png')
49+
];
50+
});
3451
}
3552
public function subcategories(Request $request)
3653
{
3754
// these code must be efactored - we wil need this again and again
38-
return Category::where('parent_id','!=',1)->with('category')->filter($request);
55+
return Category::where('parent_id','!=',1)->with('category')->with('products')->filter($request);
56+
}
57+
58+
public function index()
59+
{
60+
// these code must be efactored - we wil need this again and again
61+
return Category::where('parent_id',1)->with('subcategories')->with('products')->where('id','!=',1)->latest()->get();
3962
}
4063

4164
/**
@@ -99,7 +122,7 @@ public function multiDelete(Request $request)
99122
*/
100123
public function show(Category $category)
101124
{
102-
return $category;
125+
return $category->products;
103126
}
104127

105128
/**

app/Http/Controllers/Admin/SubCategoryController.php

-86
This file was deleted.

app/Http/Controllers/OrderController.php

-85
This file was deleted.

0 commit comments

Comments
 (0)