ziben69
145
5
Laravel

Photo validation in relationship

Posted 2 months ago by ziben69

Hello guys,

I have two models in relationship - one to many:

Model: Business and Model Photo. Everything works nicely, only when I try to display using the method only the first photo for a given business pops up an error.

My function:

public function singlePhotoForSingleEvent() {
        return $this->photos()->first();
    }

How can I validate this function, because if I don't have a photo in the database, it throw an error.

The above function is in the Business model, not in the controller.

I should write something like:

if($request->has('photos')) or something?

or maybe can I validate it in view?

 <div class="post-thumb">
       <a href="{{ URL::to('business') }}"><img src="/storage/app/public/{{ $bus->singlePhotoForSingleEvent()->filename }}" alt="{{ $bus->title }}"></a>
</div>

Thanks for help!

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.