Skip to content

Commit b40fe80

Browse files
committed
Algolia pushToIndex is now triggered manually, because event is fired before pivot table sync.
1 parent 0185b03 commit b40fe80

File tree

3 files changed

+73
-55
lines changed

3 files changed

+73
-55
lines changed

app/Http/Controllers/Admin/PhotoController.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public function getGrid()
4848
}
4949

5050

51+
// Creation methods are exactly the same as the PhotoController@getNew and postNew, so no need to copy-paste here.
52+
5153
public function getEdit($id)
5254
{
5355
$photo = Photo::with('user', 'tags')->find($id);
@@ -93,7 +95,7 @@ public function postEdit($id, Request $request)
9395
if ($isFileUploaded) {
9496

9597
//First, delete the old photos and all sizes using Croppa
96-
Croppa::delete(config('whatthetag.uploads_folder'). '/' . $photo->image);
98+
Croppa::delete(config('whatthetag.uploads_folder') . '/' . $photo->image);
9799

98100
//Then Upload the image and return the filename
99101
$upload = Photo::upload($request->file('photo'));
@@ -132,6 +134,9 @@ public function postEdit($id, Request $request)
132134
//Now, sync all the associated tags
133135
$photo->tags()->sync($tagIds);
134136

137+
// Push to Algolia, auto-index disabled because event is fired before pivot table sync.
138+
$photo->pushToIndex();
139+
135140
return back()
136141
->withSuccess('Photo updated successfully!');
137142
}
@@ -146,14 +151,19 @@ public function getDelete($id)
146151
}
147152

148153
// First delete the photo
149-
Croppa::delete(config('whatthetag.uploads_folder'). '/' . $photo->image);
154+
Croppa::delete(config('whatthetag.uploads_folder') . '/' . $photo->image);
150155

151156
//If foreign keys were not added to pivot table as on delete cascade, we also needed to delete the tags beforehand
152157
//I'm leaving this here just for reference
153158
//$photo->tags()->detach();
154159

155160
$photo->delete();
156161

162+
// Remove From Algolia
163+
// Commented-out, because event can handle this well enough,
164+
// unlike upon creation and update which also has to mess with pivot table stuff
165+
// $photo->removeFromIndex();
166+
157167
return back()
158168
->withSuccess('Photo deleted successfully!');
159169
}

app/Http/Controllers/PhotoController.php

+54-49
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@
1313
use Str;
1414
use Auth;
1515

16-
class PhotoController extends Controller {
17-
16+
class PhotoController extends Controller
17+
{
18+
1819
//Only users can access this route
1920
public function __construct()
2021
{
2122
$this->middleware('auth', ['only' => ['getNew', 'postNew']]);
2223
//I wish we could pass parameters to middlewares
2324
$this->middleware('validSlugFirstParameter', ['only' => ['getTagged', 'getDetail', 'getUser']]);
2425
}
25-
26+
2627
/**
2728
* Display a listing of the resource.
2829
*
@@ -34,112 +35,113 @@ public function getIndex()
3435
->orderByRandom()
3536
->take(12)
3637
->get();
37-
38+
3839
return view('photos.list')
3940
->withPhotos($photos);
4041
}
41-
42+
4243
public function getSearch()
4344
{
4445
return view('photos.search')
4546
->withTitle('Search for a Photo!');
46-
47+
4748
}
48-
49+
4950
public function getRecents()
5051
{
5152
$photos = Photo::with('tags')
5253
->take(12)
5354
->orderBy('id', 'desc')
5455
->paginate(config('whatthetag.pagination_count'));
55-
56+
5657
return view('photos.list')
5758
->withTitle('Recent Photos')
5859
->withPhotos($photos);
5960
}
60-
61+
6162
public function getTagged($tagSlug)
6263
{
6364
$tag = Tag::with([
64-
'photos' => function($q){
65-
$q->orderBy('id', 'desc');
66-
},
67-
'photos.tags',
68-
])
69-
->whereSlug($tagSlug) //I didn't like findBySlug() provided with Sluggable package
65+
'photos' => function ($q) {
66+
$q->orderBy('id', 'desc');
67+
},
68+
'photos.tags',
69+
])
70+
->whereSlug($tagSlug)//I didn't like findBySlug() provided with Sluggable package
7071
->first();
7172

72-
if(!$tag) {
73+
if (!$tag) {
7374
return redirect('/')
74-
->withError('Tag '.$tagSlug.' not found');
75+
->withError('Tag ' . $tagSlug . ' not found');
7576
}
7677

7778
return view('photos.list')
78-
->withTitle('Photos Tagged With: '.$tagSlug)
79+
->withTitle('Photos Tagged With: ' . $tagSlug)
7980
->withPhotos($tag->photos()->paginate(config('whatthetag.pagination_count')));
8081
}
81-
82+
8283
public function getUser($userSlug)
8384
{
8485
//Let's find the user first
8586
//I don't like findBySlug() method
8687
$user = User::with('photos', 'photos.tags', 'photos.user')
8788
->whereSlug($userSlug)->first();
88-
89-
if(!$user) {
89+
90+
if (!$user) {
9091
return redirect('/')
9192
->withError('User not found');
9293
}
93-
94+
9495
return view('photos.list')
95-
->withTitle('All Photos of: '.$user->name)
96+
->withTitle('All Photos of: ' . $user->name)
9697
->withPhotos($user->photos()->paginate(config('whatthetag.pagination_count')));
9798

9899
}
99-
100-
public function getDetail($photoSlug) {
101-
100+
101+
public function getDetail($photoSlug)
102+
{
103+
102104
$photo = Photo::with('tags', 'user')
103105
->whereSlug($photoSlug)
104106
->first();
105-
106-
if(!$photo) {
107+
108+
if (!$photo) {
107109
return redirect('/')
108110
->withError('Photo not found');
109111
}
110-
112+
111113
return view('photos.detail')
112114
->withPhoto($photo);
113115
}
114-
116+
115117

116118
public function getNew()
117119
{
118120
return view('photos.new');
119121
}
120-
122+
121123
public function postNew(Request $request)
122124
{
123-
125+
124126
$validation = Validator::make($request->all(), [
125-
'title' => 'required|min:2',
126-
'photo' => 'required|image',
127-
'tags' => 'required'
127+
'title' => 'required|min:2',
128+
'photo' => 'required|image',
129+
'tags' => 'required'
128130
]);
129-
130-
if($validation->fails()) {
131+
132+
if ($validation->fails()) {
131133
return back()
132134
->withInput()
133135
->withErrors($validation);
134136
}
135-
137+
136138
//Upload the image and return the filename and full path
137-
$upload = Photo::upload($request->file('photo'));
139+
$upload = Photo::upload($request->file('photo'));
138140

139141
//Tag Stuff
140142
//First, create(if needed) and return IDs of tags
141-
$tagIds = Tag::createAndReturnArrayOfTagIds($request->get('tags'));
142-
143+
$tagIds = Tag::createAndReturnArrayOfTagIds($request->get('tags'));
144+
143145
/*//If user wants to read the tags (keywords) from the file, then we need to fetch them from uploaded file.
144146
if($request->has('read_tags_from_file')) {
145147
$exif = exif_read_data($upload['fullpath'], 'ANY_TAG', true);
@@ -157,19 +159,22 @@ public function postNew(Request $request)
157159
}
158160
}*/
159161
//Tag Stuff end
160-
161-
$photo = new Photo;
162-
$photo->user_id = Auth::id();
163-
$photo->title = $request->get('title');
164-
$photo->image = $upload['filename'];
162+
163+
$photo = new Photo;
164+
$photo->user_id = Auth::id();
165+
$photo->title = $request->get('title');
166+
$photo->image = $upload['filename'];
165167
$photo->save();
166-
168+
167169
//Now attach the tags, since this is creating method, attach() is okay
168170
$photo->tags()->attach($tagIds);
169-
171+
172+
// Push to Algolia, auto-index disabled because event is fired before pivot table sync.
173+
$photo->pushToIndex();
174+
170175
return back()
171176
->withSuccess('Photo Created Successfully!');
172-
177+
173178
}
174179

175180
}

app/Photo.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public function sluggable()
3434

3535

3636
public $indices;
37-
public static $autoIndex = true;
37+
38+
// $autoIndex is set to false, because there is a pivot relationship and this doesn't fire the event
39+
// Delete can still be done automatically
40+
public static $autoIndex = false;
3841
public static $autoDelete = true;
3942

4043
public function __construct($attributes = [])
@@ -127,9 +130,9 @@ public static function upload(UploadedFile $file)
127130
// Set visibility to public
128131
'ACL' => 'public-read',
129132

130-
//Let's add a year for cache headers:
131-
'CacheControl' => 'max-age=31536000, public',
132-
'Expires' => gmdate("D, d M Y H:i:s", time() + 31536000) . " GMT",
133+
//Let's add a decade for cache headers:
134+
'CacheControl' => 'max-age=315360000, public',
135+
'Expires' => gmdate("D, d M Y H:i:s", time() + 315360000) . " GMT",
133136
]
134137
);
135138

0 commit comments

Comments
 (0)