fix: 🐛 update answer cache and cascade delete
This commit is contained in:
parent
da8074c5a4
commit
351126ebf8
6 changed files with 15 additions and 22 deletions
|
|
@ -36,7 +36,7 @@ class OptinController extends Controller
|
|||
$singleOptin = SingleOptin::query()
|
||||
->where('email', $validatedData['email'])
|
||||
->whereHas('brand', function ($q) use ($service) {
|
||||
$q->where('id', $service->brand->id);
|
||||
$q->whereKey($service->brand->getKey());
|
||||
})
|
||||
->firstOrNew();
|
||||
|
||||
|
|
|
|||
|
|
@ -154,6 +154,7 @@ class AnswerController extends Controller
|
|||
*/
|
||||
public function destroy(Question $question, Answer $answer): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$answer->singleOptins()->detach();
|
||||
if ($answer->deleteOrFail()) {
|
||||
return redirect()->route('bo.questions.answers.index', $question)
|
||||
->with('success', trans(
|
||||
|
|
|
|||
|
|
@ -31,11 +31,11 @@ class SingleOptinController extends Controller
|
|||
$query = SingleOptin::query()
|
||||
->when(
|
||||
$request->filled('brand'),
|
||||
fn($q) => $q->whereHas('brand', fn($q) => $q->where('id', $request->brand))
|
||||
fn($q) => $q->whereHas('brand', fn($q) => $q->whereKey($request->brand)),
|
||||
)
|
||||
->when(
|
||||
$request->filled('answer'),
|
||||
fn($q) => $q->whereHas('answers', fn($q) => $q->where('id', $request->answer))
|
||||
fn($q) => $q->whereHas('answers', fn($q) => $q->whereKey($request->answer)),
|
||||
);
|
||||
|
||||
if ($request->filled('search')) {
|
||||
|
|
@ -57,7 +57,7 @@ class SingleOptinController extends Controller
|
|||
$questionModels = Question::getAllModelsFromBrand($request->brand);
|
||||
|
||||
if ($request->filled('question')) {
|
||||
$answerModels = Answer::getAllModelsFromQuestion($request->question);
|
||||
$answerModels = Answer::getAllAnswersFromQuestion($request->question);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -78,6 +78,7 @@ class SingleOptinController extends Controller
|
|||
*/
|
||||
public function destroy(SingleOptin $singleoptin): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$singleoptin->answers()->detach();
|
||||
if ($singleoptin->deleteOrFail()) {
|
||||
return redirect()->route('bo.single_optins.index')
|
||||
->with('success', trans(
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ class Authenticate extends Middleware
|
|||
}
|
||||
}
|
||||
}
|
||||
return route('login');
|
||||
return route('bo.login');
|
||||
}
|
||||
|
||||
return null;
|
||||
|
|
|
|||
|
|
@ -49,19 +49,6 @@ class Answer extends Model
|
|||
|
||||
// * METHODS
|
||||
|
||||
/**
|
||||
* Get all answers associated to the band.
|
||||
*
|
||||
* @param integer $questionModelKey
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public static function getAllModelsFromQuestion(int $questionModelKey): \Illuminate\Database\Eloquent\Collection
|
||||
{
|
||||
return self::getAnswers()->filter(
|
||||
fn($model) => $model->question->getKey() === $questionModelKey
|
||||
)->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset all cache elements related to this model.
|
||||
*
|
||||
|
|
@ -72,18 +59,22 @@ class Answer extends Model
|
|||
{
|
||||
$questionModelKey = $model->question->getKey();
|
||||
Cache::forget("question-{$questionModelKey}-answers");
|
||||
self::getAnswers();
|
||||
self::getAllAnswersFromQuestion($questionModelKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all answers.
|
||||
*
|
||||
* @param integer $questionModelKey
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public static function getAnswers(): \Illuminate\Database\Eloquent\Collection
|
||||
public static function getAllAnswersFromQuestion(int $questionModelKey): \Illuminate\Database\Eloquent\Collection
|
||||
{
|
||||
return Cache::remember('answers', 86400, function () {
|
||||
return self::query()->orderBy('answer', 'ASC')->get();
|
||||
return Cache::remember("question-{$questionModelKey}-answers", 86400, function () use ($questionModelKey) {
|
||||
return self::query()
|
||||
->whereHas('question', fn($q) => $q->whereKey($questionModelKey))
|
||||
->orderBy('answer', 'ASC')
|
||||
->get();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue