How to view videos in Laravel project?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















I need help on how to play my uploaded videos in my Laravel project. I can upload them and they show in the database but I can't play them in my view and I get this error 'The media could not be loaded, either because the server or network failed or because the format is not supported'. I'm using Video.js. Here is my code



Controller:



public function store(MovieRequest $request)
{
DB::beginTransaction();
try {
$movie = new Movie;

$movie->movie_name = $request->input('movie_name');

if ($request->hasFile('uploaded_path')) {
$filenameWithExt = $request->file('uploaded_path')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('uploaded_path')->getClientOriginalExtension();
$fileNameToStore = $filename. '_'.time().'.'.$extension;
$path = $request->file('uploaded_path')->storeAs('public/movies/', $fileNameToStore);
} else {
$fileNameToStore = 'novideo.mp4';
}
$movie->uploaded_path = $fileNameToStore;

$movie->save();

$movie->actors()->attach($request->input('actor_id'));
$movie->categories()->attach($request->input('category_id'));

DB::commit();
} catch (Exception $e) {
DB::rollBack();
}
Session::flash('success', 'A movie was successfully UPLOADED in the database!');
return redirect()->route('movies.index');
}


MovieRequest:



public function rules()
{
return [
'movie_name' => 'required|max:255',
'uploaded_path' => 'mimetypes:video/avi,video/mpeg,video/mp4|required',
'category_id' => 'required|exists:mysql.categories,id',
'actor_id' => 'required|exists:mysql.actors,id'
];
}


View:



<tbody>
@foreach ($movies as $movie)
<tr>
<td>{{ $movie->id }}</td>
<td>{{ $movie->movie_name }}</td>


<td>
<video id="my-video" class="video-js" controls preload="auto" width="200" height="100" data-setup="{}">
<source src="public/movies/{{$movie->uploaded_path}}" type='video/mp4'>
</video>
</td>


<td>
@foreach ($movie->actors as $actor)
<a href="{{ route('actors.edit', $actor->id) }}">{{$actor->actor_name}} @unless($loop->last)
,
@endunless</a>
@endforeach
</td>
<td>
@foreach ($movie->categories as $category)
<a href="{{ route('categories.edit', $category->id) }}">{{$category->category_name}} @unless($loop->last)
,
@endunless</a>
@endforeach
</td>
<td>{{ date('M j, Y', strtotime($movie->created_at)) }}</td>
<td><a href="{{ route('movies.edit', $movie->id) }}" class="btn btn-default btn-sm">Edit</a></td>
</tr>
@endforeach
</tbody>









share|improve this question




















  • 1





    src="public/movies/{{$movie->id}}" Are you really storing the filename as an integer?

    – Devon
    Nov 16 '18 at 15:46











  • I made a mistake, forgot to fix it and edit it. it is $movie->uploaded_path, but I still get the same mistake.

    – Gacho
    Nov 16 '18 at 16:00











  • If you want to serve your movie using a public path, it needs to be relative to the public folder what is the path stored in uploaded_path ?

    – adam
    Nov 16 '18 at 16:14













  • I agree with @adam, check the path to the video. Tried the same with the simple video tag and it works just fine.

    – Philipp Palmtag
    Nov 16 '18 at 16:20








  • 1





    @Gacho If I understand the documentation correctly it looks like that command would make items publicly available in your browser at /storage so try /storage/movies/{{$movie->uploaded_path}}

    – adam
    Nov 16 '18 at 19:31




















-1















I need help on how to play my uploaded videos in my Laravel project. I can upload them and they show in the database but I can't play them in my view and I get this error 'The media could not be loaded, either because the server or network failed or because the format is not supported'. I'm using Video.js. Here is my code



Controller:



public function store(MovieRequest $request)
{
DB::beginTransaction();
try {
$movie = new Movie;

$movie->movie_name = $request->input('movie_name');

if ($request->hasFile('uploaded_path')) {
$filenameWithExt = $request->file('uploaded_path')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('uploaded_path')->getClientOriginalExtension();
$fileNameToStore = $filename. '_'.time().'.'.$extension;
$path = $request->file('uploaded_path')->storeAs('public/movies/', $fileNameToStore);
} else {
$fileNameToStore = 'novideo.mp4';
}
$movie->uploaded_path = $fileNameToStore;

$movie->save();

$movie->actors()->attach($request->input('actor_id'));
$movie->categories()->attach($request->input('category_id'));

DB::commit();
} catch (Exception $e) {
DB::rollBack();
}
Session::flash('success', 'A movie was successfully UPLOADED in the database!');
return redirect()->route('movies.index');
}


MovieRequest:



public function rules()
{
return [
'movie_name' => 'required|max:255',
'uploaded_path' => 'mimetypes:video/avi,video/mpeg,video/mp4|required',
'category_id' => 'required|exists:mysql.categories,id',
'actor_id' => 'required|exists:mysql.actors,id'
];
}


View:



<tbody>
@foreach ($movies as $movie)
<tr>
<td>{{ $movie->id }}</td>
<td>{{ $movie->movie_name }}</td>


<td>
<video id="my-video" class="video-js" controls preload="auto" width="200" height="100" data-setup="{}">
<source src="public/movies/{{$movie->uploaded_path}}" type='video/mp4'>
</video>
</td>


<td>
@foreach ($movie->actors as $actor)
<a href="{{ route('actors.edit', $actor->id) }}">{{$actor->actor_name}} @unless($loop->last)
,
@endunless</a>
@endforeach
</td>
<td>
@foreach ($movie->categories as $category)
<a href="{{ route('categories.edit', $category->id) }}">{{$category->category_name}} @unless($loop->last)
,
@endunless</a>
@endforeach
</td>
<td>{{ date('M j, Y', strtotime($movie->created_at)) }}</td>
<td><a href="{{ route('movies.edit', $movie->id) }}" class="btn btn-default btn-sm">Edit</a></td>
</tr>
@endforeach
</tbody>









share|improve this question




















  • 1





    src="public/movies/{{$movie->id}}" Are you really storing the filename as an integer?

    – Devon
    Nov 16 '18 at 15:46











  • I made a mistake, forgot to fix it and edit it. it is $movie->uploaded_path, but I still get the same mistake.

    – Gacho
    Nov 16 '18 at 16:00











  • If you want to serve your movie using a public path, it needs to be relative to the public folder what is the path stored in uploaded_path ?

    – adam
    Nov 16 '18 at 16:14













  • I agree with @adam, check the path to the video. Tried the same with the simple video tag and it works just fine.

    – Philipp Palmtag
    Nov 16 '18 at 16:20








  • 1





    @Gacho If I understand the documentation correctly it looks like that command would make items publicly available in your browser at /storage so try /storage/movies/{{$movie->uploaded_path}}

    – adam
    Nov 16 '18 at 19:31
















-1












-1








-1


0






I need help on how to play my uploaded videos in my Laravel project. I can upload them and they show in the database but I can't play them in my view and I get this error 'The media could not be loaded, either because the server or network failed or because the format is not supported'. I'm using Video.js. Here is my code



Controller:



public function store(MovieRequest $request)
{
DB::beginTransaction();
try {
$movie = new Movie;

$movie->movie_name = $request->input('movie_name');

if ($request->hasFile('uploaded_path')) {
$filenameWithExt = $request->file('uploaded_path')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('uploaded_path')->getClientOriginalExtension();
$fileNameToStore = $filename. '_'.time().'.'.$extension;
$path = $request->file('uploaded_path')->storeAs('public/movies/', $fileNameToStore);
} else {
$fileNameToStore = 'novideo.mp4';
}
$movie->uploaded_path = $fileNameToStore;

$movie->save();

$movie->actors()->attach($request->input('actor_id'));
$movie->categories()->attach($request->input('category_id'));

DB::commit();
} catch (Exception $e) {
DB::rollBack();
}
Session::flash('success', 'A movie was successfully UPLOADED in the database!');
return redirect()->route('movies.index');
}


MovieRequest:



public function rules()
{
return [
'movie_name' => 'required|max:255',
'uploaded_path' => 'mimetypes:video/avi,video/mpeg,video/mp4|required',
'category_id' => 'required|exists:mysql.categories,id',
'actor_id' => 'required|exists:mysql.actors,id'
];
}


View:



<tbody>
@foreach ($movies as $movie)
<tr>
<td>{{ $movie->id }}</td>
<td>{{ $movie->movie_name }}</td>


<td>
<video id="my-video" class="video-js" controls preload="auto" width="200" height="100" data-setup="{}">
<source src="public/movies/{{$movie->uploaded_path}}" type='video/mp4'>
</video>
</td>


<td>
@foreach ($movie->actors as $actor)
<a href="{{ route('actors.edit', $actor->id) }}">{{$actor->actor_name}} @unless($loop->last)
,
@endunless</a>
@endforeach
</td>
<td>
@foreach ($movie->categories as $category)
<a href="{{ route('categories.edit', $category->id) }}">{{$category->category_name}} @unless($loop->last)
,
@endunless</a>
@endforeach
</td>
<td>{{ date('M j, Y', strtotime($movie->created_at)) }}</td>
<td><a href="{{ route('movies.edit', $movie->id) }}" class="btn btn-default btn-sm">Edit</a></td>
</tr>
@endforeach
</tbody>









share|improve this question
















I need help on how to play my uploaded videos in my Laravel project. I can upload them and they show in the database but I can't play them in my view and I get this error 'The media could not be loaded, either because the server or network failed or because the format is not supported'. I'm using Video.js. Here is my code



Controller:



public function store(MovieRequest $request)
{
DB::beginTransaction();
try {
$movie = new Movie;

$movie->movie_name = $request->input('movie_name');

if ($request->hasFile('uploaded_path')) {
$filenameWithExt = $request->file('uploaded_path')->getClientOriginalName();
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
$extension = $request->file('uploaded_path')->getClientOriginalExtension();
$fileNameToStore = $filename. '_'.time().'.'.$extension;
$path = $request->file('uploaded_path')->storeAs('public/movies/', $fileNameToStore);
} else {
$fileNameToStore = 'novideo.mp4';
}
$movie->uploaded_path = $fileNameToStore;

$movie->save();

$movie->actors()->attach($request->input('actor_id'));
$movie->categories()->attach($request->input('category_id'));

DB::commit();
} catch (Exception $e) {
DB::rollBack();
}
Session::flash('success', 'A movie was successfully UPLOADED in the database!');
return redirect()->route('movies.index');
}


MovieRequest:



public function rules()
{
return [
'movie_name' => 'required|max:255',
'uploaded_path' => 'mimetypes:video/avi,video/mpeg,video/mp4|required',
'category_id' => 'required|exists:mysql.categories,id',
'actor_id' => 'required|exists:mysql.actors,id'
];
}


View:



<tbody>
@foreach ($movies as $movie)
<tr>
<td>{{ $movie->id }}</td>
<td>{{ $movie->movie_name }}</td>


<td>
<video id="my-video" class="video-js" controls preload="auto" width="200" height="100" data-setup="{}">
<source src="public/movies/{{$movie->uploaded_path}}" type='video/mp4'>
</video>
</td>


<td>
@foreach ($movie->actors as $actor)
<a href="{{ route('actors.edit', $actor->id) }}">{{$actor->actor_name}} @unless($loop->last)
,
@endunless</a>
@endforeach
</td>
<td>
@foreach ($movie->categories as $category)
<a href="{{ route('categories.edit', $category->id) }}">{{$category->category_name}} @unless($loop->last)
,
@endunless</a>
@endforeach
</td>
<td>{{ date('M j, Y', strtotime($movie->created_at)) }}</td>
<td><a href="{{ route('movies.edit', $movie->id) }}" class="btn btn-default btn-sm">Edit</a></td>
</tr>
@endforeach
</tbody>






php laravel video video.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 16:00







Gacho

















asked Nov 16 '18 at 15:15









GachoGacho

198




198








  • 1





    src="public/movies/{{$movie->id}}" Are you really storing the filename as an integer?

    – Devon
    Nov 16 '18 at 15:46











  • I made a mistake, forgot to fix it and edit it. it is $movie->uploaded_path, but I still get the same mistake.

    – Gacho
    Nov 16 '18 at 16:00











  • If you want to serve your movie using a public path, it needs to be relative to the public folder what is the path stored in uploaded_path ?

    – adam
    Nov 16 '18 at 16:14













  • I agree with @adam, check the path to the video. Tried the same with the simple video tag and it works just fine.

    – Philipp Palmtag
    Nov 16 '18 at 16:20








  • 1





    @Gacho If I understand the documentation correctly it looks like that command would make items publicly available in your browser at /storage so try /storage/movies/{{$movie->uploaded_path}}

    – adam
    Nov 16 '18 at 19:31
















  • 1





    src="public/movies/{{$movie->id}}" Are you really storing the filename as an integer?

    – Devon
    Nov 16 '18 at 15:46











  • I made a mistake, forgot to fix it and edit it. it is $movie->uploaded_path, but I still get the same mistake.

    – Gacho
    Nov 16 '18 at 16:00











  • If you want to serve your movie using a public path, it needs to be relative to the public folder what is the path stored in uploaded_path ?

    – adam
    Nov 16 '18 at 16:14













  • I agree with @adam, check the path to the video. Tried the same with the simple video tag and it works just fine.

    – Philipp Palmtag
    Nov 16 '18 at 16:20








  • 1





    @Gacho If I understand the documentation correctly it looks like that command would make items publicly available in your browser at /storage so try /storage/movies/{{$movie->uploaded_path}}

    – adam
    Nov 16 '18 at 19:31










1




1





src="public/movies/{{$movie->id}}" Are you really storing the filename as an integer?

– Devon
Nov 16 '18 at 15:46





src="public/movies/{{$movie->id}}" Are you really storing the filename as an integer?

– Devon
Nov 16 '18 at 15:46













I made a mistake, forgot to fix it and edit it. it is $movie->uploaded_path, but I still get the same mistake.

– Gacho
Nov 16 '18 at 16:00





I made a mistake, forgot to fix it and edit it. it is $movie->uploaded_path, but I still get the same mistake.

– Gacho
Nov 16 '18 at 16:00













If you want to serve your movie using a public path, it needs to be relative to the public folder what is the path stored in uploaded_path ?

– adam
Nov 16 '18 at 16:14







If you want to serve your movie using a public path, it needs to be relative to the public folder what is the path stored in uploaded_path ?

– adam
Nov 16 '18 at 16:14















I agree with @adam, check the path to the video. Tried the same with the simple video tag and it works just fine.

– Philipp Palmtag
Nov 16 '18 at 16:20







I agree with @adam, check the path to the video. Tried the same with the simple video tag and it works just fine.

– Philipp Palmtag
Nov 16 '18 at 16:20






1




1





@Gacho If I understand the documentation correctly it looks like that command would make items publicly available in your browser at /storage so try /storage/movies/{{$movie->uploaded_path}}

– adam
Nov 16 '18 at 19:31







@Gacho If I understand the documentation correctly it looks like that command would make items publicly available in your browser at /storage so try /storage/movies/{{$movie->uploaded_path}}

– adam
Nov 16 '18 at 19:31














1 Answer
1






active

oldest

votes


















0














Create an endpoint to serve your movie:



Route::get('/movie/{id}', ['uses' => 'MovieController@downloadMovie']);


Add a method in your controller:



public function downloadMovie($id)
{
$movie = Movie::findOrFail($id);
// download the movie
return Response::download($movie->uploaded_path);
}


Then in your view:



 <video id="my-video" class="video-js" controls preload="auto" width="200" height="100" data-setup="{}">
<source src="/movie/{{$movie->getKey()}}" type='video/mp4'>
</video>





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340594%2fhow-to-view-videos-in-laravel-project%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Create an endpoint to serve your movie:



    Route::get('/movie/{id}', ['uses' => 'MovieController@downloadMovie']);


    Add a method in your controller:



    public function downloadMovie($id)
    {
    $movie = Movie::findOrFail($id);
    // download the movie
    return Response::download($movie->uploaded_path);
    }


    Then in your view:



     <video id="my-video" class="video-js" controls preload="auto" width="200" height="100" data-setup="{}">
    <source src="/movie/{{$movie->getKey()}}" type='video/mp4'>
    </video>





    share|improve this answer




























      0














      Create an endpoint to serve your movie:



      Route::get('/movie/{id}', ['uses' => 'MovieController@downloadMovie']);


      Add a method in your controller:



      public function downloadMovie($id)
      {
      $movie = Movie::findOrFail($id);
      // download the movie
      return Response::download($movie->uploaded_path);
      }


      Then in your view:



       <video id="my-video" class="video-js" controls preload="auto" width="200" height="100" data-setup="{}">
      <source src="/movie/{{$movie->getKey()}}" type='video/mp4'>
      </video>





      share|improve this answer


























        0












        0








        0







        Create an endpoint to serve your movie:



        Route::get('/movie/{id}', ['uses' => 'MovieController@downloadMovie']);


        Add a method in your controller:



        public function downloadMovie($id)
        {
        $movie = Movie::findOrFail($id);
        // download the movie
        return Response::download($movie->uploaded_path);
        }


        Then in your view:



         <video id="my-video" class="video-js" controls preload="auto" width="200" height="100" data-setup="{}">
        <source src="/movie/{{$movie->getKey()}}" type='video/mp4'>
        </video>





        share|improve this answer













        Create an endpoint to serve your movie:



        Route::get('/movie/{id}', ['uses' => 'MovieController@downloadMovie']);


        Add a method in your controller:



        public function downloadMovie($id)
        {
        $movie = Movie::findOrFail($id);
        // download the movie
        return Response::download($movie->uploaded_path);
        }


        Then in your view:



         <video id="my-video" class="video-js" controls preload="auto" width="200" height="100" data-setup="{}">
        <source src="/movie/{{$movie->getKey()}}" type='video/mp4'>
        </video>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 '18 at 16:10









        adamadam

        949811




        949811
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53340594%2fhow-to-view-videos-in-laravel-project%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine