Deleting an entry causes 500 error unless entry is destroyed
Been struggling with this for a bit and I'm out of ideas. I've got a browser that contains a list of other entries called 'sponsors'. When you delete the 'sponsor' it goes into the trash and is removed from the browser in the admin, but causes a 500 error when viewing the final page. If you destroy the 'sponsor' the page works fine.
I've tried querying with ->get() using withTrashed() and then iterating over the query to filter out deleted 'partners' and still get a 500 error. Also tried whereNull('deleted_at') and have the same issue. Also tried other random stack overflow answers that still didn't work.
Is there some form for querying related browsers that filters out trashed and unpublished entries that I'm unaware of?
5 Replies
Can you show us full error?
Hi @Christfister how is your browser saved? In a pivot table you created or using the
related
table?
And how are you accessing them?I believe using the related table. I have singleton called partners page with "HasRelated" that has several browsers to allow the user to place selections in predefined locations.
Heres the code for my PartnerspageRepository
<?php
namespace App\Repositories;
use A17\Twill\Repositories\Behaviors\HandleBlocks;
use A17\Twill\Repositories\Behaviors\HandleMedias;
use A17\Twill\Repositories\Behaviors\HandleFiles;
use A17\Twill\Repositories\Behaviors\HandleBrowsers;
use A17\Twill\Repositories\ModuleRepository;
use App\Models\Partnerspage;
class PartnerspageRepository extends ModuleRepository
{
use HandleBlocks, HandleMedias, HandleFiles, HandleBrowsers;
protected $relatedBrowsers = ['partners', 'presenting', 'premier', 'supporting', 'associate'];
public function construct(Partnerspage $model)
{
$this->model = $model;
}
public function getAllContent() {
return $this->model->first();
}
}
Here's the code and how I've been trying to retrieve the partners in the partnersRepository
<?php
namespace App\Repositories;
use A17\Twill\Repositories\Behaviors\HandleMedias;
use A17\Twill\Repositories\ModuleRepository;
use App\Models\Partner;
class PartnerRepository extends ModuleRepository
{
use HandleMedias;
public function construct(Partner $model)
{
$this->model = $model;
}
public function getAllPartners() {
$partners = $this->model->published()->get(); $partnersFiltered = [];
foreach( $partners as $partner ) { if ( $partner->deleted_at == "" ) { array_push($partnersFiltered, $partner); } }
//return $this->model->published()->get(); return $partnersFiltered; } } @ifox
$partners = $this->model->published()->get(); $partnersFiltered = [];
foreach( $partners as $partner ) { if ( $partner->deleted_at == "" ) { array_push($partnersFiltered, $partner); } }
//return $this->model->published()->get(); return $partnersFiltered; } } @ifox
@kalle Standard 500 Internal Server Error. If you visit the URL I restored the deleted entry as this a staging site for the client and don't want them seem any more errors than is necessary.
500 error will be logged or shown on development env, you can check your logs or some error handler like bugsnag if you use it on production/staging envs.
Per default, Laravel will exclude all soft deleted records from your query, so its weird why it returns soft deleted record too. Ref https://laravel.com/docs/9.x/eloquent#querying-soft-deleted-models
For what is
getAllPartners
is used for? Browser handle this behind the 'scenes', so Im confused somewhat here.