Search code examples
phplaravelloopseloquenthelper

Laravel add new item to foreach loop from outside


I am curious. Is Laravel giving us an easier way to add foreign items to foreach array? For example i have:

$video_url = Video::orderBy('created_at', 'desc')->first()->url;
$cut_head = str_after($video_url, 'https://www.youtube.com/watch?v=');
$cut_tail = str_before($cut_head, '&');

It works as i think. Cutting head and tail. But in my videos loop. There is no cropped video url link.

$videos = Video::orderBy('created_at', 'desc')->get();

$videos is using in my view. Inside a foreach loop. How can i entegrate the cropped url_link to the foreach loop.

Thanks an advance.


Solution

  • Created App\Helper\helper.php added

        function videoJpg($video_url){
        $cut_head = str_after($video_url, 'https://www.youtube.com/watch?v=');
        $cut_tail = str_before($cut_head, '&');
        $video_jpg = "http://i3.ytimg.com/vi/".$cut_tail."/hqdefault.jpg";
    
        return $video_jpg;}
    

    added

    "classmap": "database"
    "psr-4": {"App\\": "app/" }
    "files" : ["app/Helper/helper.php"]
    

    to composer.json

    After that composer dumpautload

    Usage in the blade is: <img src="{{ VideoJpg($video->url) }}"

    I made my own helper. This is the cleanest solution.