Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Bossino's avatar

Failed to load resource: the server responded with a status of 404 (Not Found)

Hello, I'm doing a video looping with auto-play. My video looping was working before and the videos were play. But after I put some adjustments on my page and my logic, it was broken and the videos were not play. I got the error Failed to load resource: the server responded with a status of 404 (Not Found)

js code

<script type="text/javascript">

var vidElement = document.getElementById('video');
var vidSources = [
    @foreach($videos as $video)
    "{{ $video->video_file }}",
    @endforeach
  ];
var activeVideo = Math.floor((Math.random() * vidSources.length));
vidElement.src = vidSources[activeVideo];
vidElement.addEventListener('ended', function(e) {
  // update the active video index
  activeVideo = (++activeVideo) % vidSources.length;
  if(activeVideo === vidSources.length){
    activeVideo = 0;
  }

  // update the video source and play
  vidElement.src = vidSources[activeVideo];
  vidElement.play();
});

</script>

I'm finding away to solve that problem

0 likes
27 replies
Nakov's avatar

You cannot use blade syntax in JavaScript.. this code does not work:

var vidSources = [
    @foreach($videos as $video)
    "{{ $video->video_file }}",
    @endforeach
  ];
Sinnbeck's avatar

@nakov I think it will work as long as that code is inside a blade file? I could be wrong :)

@bosspogs If you press f12 and select network, can you then see the url it is trying to call. Can you perhaps see what is wrong with it?

Bossino's avatar

okay sir. But even before I change into blade syntax when I put the files like this

var vidSources = [
    "videos/laughsone.mp4",
    "videos/fly.mp4"
];
Bossino's avatar

Yes I see it sir on the network tab. It said Failed to response data. And by the way, I see in the console tab that I use the blade syntax, in the console it provides the video files so I think it is not a problem if you use a blade syntax file on the script code

Sinnbeck's avatar

In the network tab you can rightclick the failed link and select Open in new tab. Now you can see the link How does it differ from the correct one?

iyashpal's avatar

@bosspogs This will work for you:

<script >
        var vidElement = document.getElementById('video');
        var videos = JSON.parse('@json($videos)');
        
        var vidSources = [];
        for (let i in videos) {
            vidSources.push(videos[i].video_file)
        }
        
        var activeVideo = Math.floor((Math.random() * vidSources.length));
        vidElement.src = vidSources[activeVideo];
        vidElement.addEventListener('ended', function(e) {
            // update the active video index
            activeVideo = (++activeVideo) % vidSources.length;
            if (activeVideo === vidSources.length) {
                activeVideo = 0;
            }

            // update the video source and play
            vidElement.src = vidSources[activeVideo];
            vidElement.play();
        });
    </script>
Sinnbeck's avatar

Does the file queue/videos/GJpq2NfwNONwl0SeYhQ1uSXKu6vhcmW6CVT9sPSS.mp4 exist?

Bossino's avatar

yes in my database sir in my videos table

Sinnbeck's avatar

It should also exist on your computer. You cannot play a video based on a link in your database if that video does not exist on you harddrive :)

Bossino's avatar

It works now sir @cyberkingyash. How about if you want to play the videos from the database, it would still work?

Bossino's avatar

Okay sir I will try to figure out that one sir

Sinnbeck's avatar

@bosspogs How does the videos get into the database? It might be worth looking into saving the file in the correct place with the correct name, when you add them to the database :)

iyashpal's avatar

@bosspogs You mean to say you want to store the video only on database? If yes then you can't do this.

Bossino's avatar

Here sir @sinnbeck

VideoController@store

public function store(Request $request)
    {
        if (request()->has('video_file'))
        {
            Video::create([
                'video_file' => $request->file('video_file')->store('videos', 'public')
            ]);
        }
      
        return redirect()->route('video.index')->with('success' , 'Video uploaded successfully');
    }
Bossino's avatar

oh so sadly I cannot autoplay loop my videos from my database. So sir is it not possible to autoplay loop videos from db?

Sinnbeck's avatar

You are saving the videos in /videos but you try to find them in /queue/videos

$request->file('video_file')->store('queue/videos')
Sinnbeck's avatar

Wrap your links in url

{{ url('storage/'. $video->video_file) }}
Bossino's avatar

Like this one sir?

var vidSources = [
  @foreach($videos as $video)
  "{{ url('storage/'. $video->video_file) }}",
  @endforeach
];

Please or to participate in this conversation.