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

Published 10 months 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('')->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?


any idea?


I'm guessing that you're not sending task through to the view from the controller. I can't tell since you haven't posted the create() method code.

Replace this code:

<form class="form-vertical" role="form"
                                    action="{{ route('projects.files', ['projectId'=> $project->id, 'taskId'=>$task->id])}}">

With this:

<form class="form-vertical" role="form"
                                    action="{{ route('projects.files', ['projectId'=> $project->id, 'taskId'=>$taskId])}}">

And make sure to explicitly pass taskId from the create() method in the controller.


@jalawes actually I have not create method in My controller. any way did your comments and occurred following error.

Undefined variable: taskId (View: C:\Users\Nalaka\Desktop\acxian\resources\views\files\form.blade.php)

Can you post the code for the controller that is responsible for showing the view form.blade.php?

  1. On which URL you are accessing the form?
  2. Show the route you have written for that form?

You passing $project variable to the view and not passing the $task variable or no task exists in the table.


Please put the function for display form.blade.php view

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