I am trying to display information from the database and attach images to it - I want to follow the mvc template.

// Контроллер $events = Event::where('show_or_not', '=', 'show') ->get(); // А здесь мне нужно подцепить картинку для каждого id из перебора foreach ($events as $event) { if (file_exists(public_path( 'img/'.$event->id.'.jpg' ))) { $img = '<img src="public_path( img/'.$event->id.'.jpg )">' } } 

How to do it all right? Perhaps you need to register as a function in the model? And how then to display in the submission?

  • one
    If this is the option, then of course it should be done in the template. - Maxim Stepanov
  • The only thing you can of course do is 2 functions hasImg and getSrc - Orange_shadow
  • one
    Rendering the html element from the model is wrong. When viewing the template code, it will not be clear what is hidden in the method. And to climb into the model to add a class to the image is even somehow sad. - xEdelweiss

1 answer 1

As a result, made through a function in the model. Thank you all for the discussion.

 // Контроллер $events = Event::where('show_or_not', '=', 'show') ->get(); // Модель public function eventImage() { $img = '<img src="public_path( img/'.$this->id.'.jpg )">'; return $img; } // Представление @foreach ($events as $event) {{ $event->eventTitle }} {{ $event->eventImage() }} @endforeach 
  • very sad. Can you take a look at the entire code that deals with this issue? - newbie
  • @newbie actually the whole point outlined above. - Vedenin