fix: 🐛 update answer cache and cascade delete

This commit is contained in:
Alexis Gousseau 2025-08-07 16:18:47 +02:00
parent da8074c5a4
commit 351126ebf8
No known key found for this signature in database
GPG key ID: 4A614CB3DBBA2406
6 changed files with 15 additions and 22 deletions

View file

@ -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();

View file

@ -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(

View file

@ -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(

View file

@ -31,7 +31,7 @@ class Authenticate extends Middleware
}
}
}
return route('login');
return route('bo.login');
}
return null;

View file

@ -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();
});
}