david001's avatar

code to upload audio

This code given below uploads the image but when i try to upload audio(mp3) it gives me error undefined variable audio


    public function store(Request $request)
    {
        if($request->hasFile('audio')){
            $audio = $request->file('audio')->store('music');
        }
        Music::create([
            'title'=>$request->title,
            'description'=>$request->description,
            
            'audio'=>$audio


        ]);
        return redirect()->back();
    }

Can anybody help to upload audio. Thanks

0 likes
2 replies
deansatch's avatar

That code doesn't upload an image - you need to show more code

yoeriboven's avatar

If it says variable undefined it means the $audio = ... line is never triggered.

You should check why the condition in the if statement is returning false.

Also, if I understand correctly this code could be triggered when there is no audio file uploaded. In that case you should pass something else to the create method since $audio is currently undefined.

Please or to participate in this conversation.