ideas icon indicating copy to clipboard operation
ideas copied to clipboard

Adding #findOrFail on the different reposities

Open MariusSpring opened this issue 3 years ago • 0 comments

Hey!

I am missing being able to use "findOrFail" one different repositories such as for taxonomy terms and collection entries. Like the "fail" method, it will take an id, however, will render the custom 404 page if the item was not found. This can be achieved pretty easily:

public function findOrFail(mixed $id)
{
  if (! $entry = $this->find($id)) {
    return redirect()->to('/404', 404);
  }

  return $entry;
}

Then in controller:

public function show(mixed $id) {
  $entry = Entry::findOrFail($id);
  
  return (new View)->template('entry')->with($entry->toAugmentedArray());
}

Also consider adding the following methods:

  • findBySlug(mixed $slug)
  • findBySlugOrFail(mixed $slug)

MariusSpring avatar Jun 16 '22 07:06 MariusSpring