how to fix Undefined variable: task (View: C:\Users\LIlan\Desktop\acxian\resources\views\files\form.blade.php) in Laravel 5.2

Posted 1 year ago by Flex

I am developing project management tool in laravel 5.2. In my application user can create a project and one project has many tasks and one task may have many files attachments. I am using cloudder to save My files. Currently I have assigned My project files to relevant projects only. Now, I need to display the files on each tasks of the relevant projects.


class FilesController extends Controller

        public function uploadAttachments(Request $request, $id,$taskId) 
             $this->validate($request, [
                'file_name'     => 'required|mimes:jpeg,bmp,png,pdf|between:1,7000',

            $filename     = $request->file('file_name')->getRealPath();

            Cloudder::upload($filename, null);
            list($width, $height) = getimagesize($filename);

            $fileUrl = Cloudder::show(Cloudder::getPublicId(), ["width" => $width, "height" => $height]);
            $this->saveUploads($request, $fileUrl, $id,$taskId);

            return redirect()->back()->with('info', 'Your Attachment has been uploaded Successfully');

        private function saveUploads(Request $request, $fileUrl, $id,$taskId)
            $file = new File;
            $file->file_name  = $request->file('file_name')->getClientOriginalName();
            $file->file_url   = $fileUrl;
            $file->project_id = $id;
            $file->task_id = $taskId;


        return redirect()->route('projects.show')->with('info', 'File deleted successfully');


Route::post('projects/{projects}/tasks/{tasks}/files', [
     'uses' => '[email protected]',
     'as'   => 'projects.files',
     'middleware' => ['auth']

and file form is files/form.blade.php

<div class="row" style="border:1px solid #ccc;margin-left:5px;width:100%;padding:15px;">
     @foreach($project->files as $file)
                    <div><i class="fa fa-check-square-o"></i>

                            <a href="{{ $file->file_url }}" target="_blank">{{ $file->file_name }}</a>

        <form class="form-vertical" role="form"
                                    action="{{ route('projects.files', ['projectId'=> $project->id, 'taskId'=>$task->id])}}">//this is line 39
            <div class="form-group{{ $errors->has('file_name') ? ' has-error' : '' }}">
                <input type="file" name="file_name" class="form-control" id="file_name">
                @if ($errors->has('file_name'))
                    <span class="help-block">{{ $errors->first('file_name') }}</span>

            <div class="form-group">
                <button type="submit" class="btn btn-info">Add Files</button>

now I got this error

ErrorException in 88ff5154a46f749c29024e0c9f84c577f7c5025e.php line 39: Undefined variable: task (View: C:\Users\Lilan\Desktop\acxian\resources\views\files\form.blade.php)

how to fix this problem?

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.