So the error is here 'task'=>$task->id
Did you pass $task to the form?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am going to submit file in My Laravel application. this is My fileController
private function saveUploads(Request $request, $fileUrl, $id)
{
//dd($request->only('task'));
$file = new File;
$file->file_name = $request->file('file_name')->getClientOriginalName();
$file->file_url = $fileUrl;
$file->project_id = $id;
$file->task_id = $request->only('task')['task'];
$file->save();
}
and this is form.blade.php form acctions
action="{{ route('projects.files', ['project'=> $project->id, 'task'=>$task->id])}}">
<div class="form-group{{ $errors->has('file_name') ? ' has-error' : '' }}"> //this is line line 39
and route
Route::post('projects/{projects}/files', [
'uses' => 'FilesController@uploadAttachments',
'as' => 'projects.files',
'middleware' => ['auth']
]);
but got following error
ErrorException in 88ff5154a46f749c29024e0c9f84c577f7c5025e.php line 39: Undefined variable: task (View: C:\Users\john\Desktop\ddd\resources\views\files\form.blade.php)
how can I fix this problem?
So the error is here 'task'=>$task->id
Did you pass $task to the form?
@Snapey actually what is the way to pass $task to the form? I did everything what I know?
Could you show the method witch returns form.blade.php? You should have something like this
$projectId = # logic to get project id
$taskId = # logic to get task id
return view('form', compact('projectId', 'taskId');
@RamjithAp see My controller methods to save file data
private function saveUploads(Request $request, $fileUrl, $id)
{
//dd($request->only('task'));
$file = new File;
$file->file_name = $request->file('file_name')->getClientOriginalName();
$file->file_url = $fileUrl;
$file->project_id = $id;
$file->task_id = $request->only('task')['task'];
$file->save();
}
here $id is project_id , and in My form.blade.php file when I print {{$task->id}} it is printing correct task id also. but I cant understand how can I pass $task to the form.blade.php file...do you have any idea here....
@Flex by seeing your error title it says task variable not defined at form.blade.php but you are saying its printing on form page. I would suggest you to paste your whole controller file codes including the controller which shows form page as well.
@RamjithAp this is My FileController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Cloudder;
use App\File as File;
use App\Task;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class FilesController extends Controller
{
/**
* Displays the index page of the app
*
* @return Response
*/
public function uploadAttachments(Request $request,$id)
{
// dd($request->only('id'));
$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);
return redirect()->back()->with('info', 'Your Attachment has been uploaded Successfully');
}
private function saveUploads(Request $request, $fileUrl, $id,$task)
{
//dd($request->only('task'));
$file = new File;
$file->file_name = $request->file('file_name')->getClientOriginalName();
$file->file_url = $fileUrl;
$file->project_id = $id;
// $file->task_id = $task;
$file->task_id = $task;//$request->only('task')['task'];
//$taskId = Task::find($task_id);
$file->save();
}
public function deleteOneProjectFile($fileUrl, $id)
{
DB::table('files')
->where('file_url', $fileUrl)
->where('project_id', $id)
->delete();
return redirect()->route('projects.show')->with('info', 'File deleted successfully');
}
public function getTasks($id)
{
$taskId = Task::with('project')->get();
return $taskId;
}
}
Where is the route and controller method to show the form (the place where you show view('form')) ?
@Snapey my form.blade.php file is show in task/show.blade.php
<h2>{{$task->project->project_name}}</h2>
<hr>
{{$task->task_name}}
<hr>
{!!$task->body!!}
<hr>
@include('comments.form')
@include('files.form') // this is form.blade.php file in
You understand what is wrong, yes?
this line in your form
action="{{ route('projects.files', ['project'=> $project->id, 'task'=>$task->id])}}">
says it needs the value of $task in order to create the action.
What I don't understand though is that based on the code you shared $task SHOULD be visible inside the form.blade.php file since an included file has access to all the same data as the parent.
You don't do something nasty with $task in the comments form do you (i.e., change what it contains)?
@RamjithAp I have task folder in My view file and in the task folder I have show.blade.php file this is the show.blade.php file
<h2>{{$task->project->project_name}}</h2>
<hr>
{{$task->task_name}}
<hr>
{!!$task->body!!}
<hr>
@include('comments.form')//this file in comments file in the view folder
@include('files.form') //this file in files folder in the view folder
route.php for file attachments
Route::post('projects/{projects}/files', [
'uses' => 'FilesController@uploadAttachments',
'as' => 'projects.files',
'middleware' => ['auth']
]);
Show me your controller function which has this line
return view('task.show').... etc
it is in TaskController
public function show($id)
{
$project = Project::find($id);
$task = Task::find($id);
return view('tasks.show')->withProject($project)->withTask($task);//withComments($comments);
}
So, here is your mistake you supposed to pass the task variable in your include function
<h2>{{$task->project->project_name}}</h2>
<hr>
{{$task->task_name}}
<hr>
{!!$task->body!!}
<hr>
@include('comments.form', array('project'=>$project,'task'=>$task)) //this file in comments file in the view folder
@include('files.form', array('project'=>$project,'task'=>$task)) //this file in files folder in the view folder
For more details read https://laravel.com/docs/5.5/blade#including-sub-views
@RamjithAp it is not success for me. please can you say me what is the way to sent $task variable data to the form.blade.php
Try this
<h2>{{$task->project->project_name}}</h2>
<hr>
{{$task->task_name}}
<hr>
{!!$task->body!!}
<hr>
@include('comments.form', array('project_id'=>$project->id,'task_id'=>$task->id)) //this file in comments file in the view folder
@include('files.form', array('project_id'=>$project->id,'task_id'=>$task->id)) //this file in files folder in the view folder
And then in your form replace $task->id with $task_id
action="{{ route('projects.files', ['project'=> $project_id, 'task'=>$task_id])}}">
<div class="form-group{{ $errors->has('file_name') ? ' has-error' : '' }}">
same thing here....
@RamjithAp check https://laravel.com/docs/5.5/blade#including-sub-views
Even though the included view will inherit all data available in the parent view, you may also pass an array of extra data to the included view:
emphasised here to call out the relevant part
so currently chasing the wrong issue
@flex. why dont you dump($task) at different points in your views to find out why it is missing
I agree @Snapey but its strange he says $task->task_name printing the value but form throwing the error for task id variable on the same view. He surely missing something that we cannot figure out unless he shows all relevant codes.
@RamjithAp witch codes do you need to solve this problem?
consider this when I am going to attach files in the form My local host url is like this way
http://localhost:8000/projects/1/tasks/5
I need submit this task number 5 to the file table. but when I publish $task in the form.blade.php file above error coming and not displaying file attach form. without $task in the blade file My form is displaying correctly. consider My comment adding form is same but it is working without any problem. file attachment form is got this problem. please tell me what codes do you need to solve this problem.
Okay so here is the issue I think, your are passing task variable but its not defined in route so change your route like below
Route::post('projects/{project}/files/{task}', [
'uses' => 'FilesController@saveUploads',
'as' => 'projects.files',
'middleware' => ['auth']
]);
Then your form as below
<form action="{{ route('projects.files', ['project'=> $project->id, 'task'=>$task->id])}}" method="POST" enctype='multipart/form-data' >
And then in your controller
private function saveUploads(Request $request, $project, $task)
{
$fileUrl = 'save/path';
$file = new File;
$file->file_name = $request->file('file_name')->getClientOriginalName();
$file->file_url = $fileUrl;
$file->project_id = $project;
$file->task_id = $task;
$file->save();
}
@RamjithAp did it but same error here I cannot guess this problem.
ok, lets do this the ugly way
<h2>{{$task->project->project_name}}</h2>
<hr>
{{$task->task_name}}
<hr>
{!!$task->body!!}
<hr>
@include('comments.form')
<?php dd($task) ?> /////////////// add this line
@include('files.form')
my betting is you never hit it
@Snapey it is generated following massage
Task {#219 ▼
#fillable: array:6 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:9 [▶]
#original: array:9 [▶]
#relations: array:1 [▶]
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
but I can see this massage My files/form.blade.php file action as following
action="{{ route('projects.files', ['projectId'=> $project->id])}}">
without $task variable....
I see you've changed your code several times. Can you please display all your current code for that is related to this issue in one place here? Route,Full Controller, View
@divanoli ok sure this is My current route, controller and view files here
files/form.blade.php
<div class="col-md-5">
<h4 class="page-header">
Files
</h4>
<div class="row" style="border:1px solid #ccc;margin-left:5px;width:100%;padding:15px;">
@foreach($project->files as $file)
<div>
<div><i class="fa fa-check-square-o"></i>
<span>
<a href="{{ $file->file_url }}" target="_blank">{{ $file->file_name }}</a>
</span>
</div>
</div>
<hr/>
@endforeach
<form class="form-vertical" role="form"
enctype="multipart/form-data"
method="post"
action="{{ route('projects.files', ['projectId'=> $project->id, 'taskId'=>$task->id])}}">
<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>
@endif
</div>
<div class="form-group">
<button type="submit" class="btn btn-info">Add Files</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
</div>
FilesController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Cloudder;
use App\File as File;
use App\Task;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class FilesController extends Controller
{
/**
* Displays the index page of the app
*
* @return Response
*/
public function uploadAttachments(Request $request, $id)
{
$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);
return redirect()->back()->with('info', 'Your Attachment has been uploaded Successfully');
}
private function saveUploads(Request $request, $fileUrl, $id,$task)
{
$file = new File;
$file->file_name = $request->file('file_name')->getClientOriginalName();
$file->file_url = $fileUrl;
$file->project_id = $id;
$file->task_id = $task;
//$taskId = Task::find($task_id);
$file->save();
}
public function deleteOneProjectFile($fileUrl, $id)
{
DB::table('files')
->where('file_url', $fileUrl)
->where('project_id', $id)
->delete();
return redirect()->route('projects.show')->with('info', 'File deleted successfully');
}
public function getTasks($id)
{
$taskId = Task::with('project')->get();
return $taskId;
}
}
routes for file attachments
Route::post('projects/{projects}/files', [
'uses' => 'FilesController@uploadAttachments',
'as' => 'projects.files',
'middleware' => ['auth']
]);
tasks/show.blade.php
<h2>{{$task->project->project_name}}</h2>
<hr>
{{$task->task_name}}
<hr>
{!!$task->body!!}
<hr>
@include('comments.form')
@include('files.form')
TaskController show methods
public function show($project_id,$task_id)
{
$project = Project::find($project_id);
$task = Task::find($task_id);
return view('tasks.show')->withProject($project)->withTask($task);
Task Model
<?php
namespace App;
use App\User;
use App\Auth;
use App\Project;
use Illuminate\Database\Eloquent\Model;
class Task extends Model
{
protected $fillable = ['task_name', 'body', 'assign','priority','duedate','project_id'];
public function scopeProject($query, $id)
{
return $query->where('project_id', $id);
}
public function project()
{
return $this->belongsTo('App\Project');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
public function subtasks(){
return $this->hasMany(Subtask::class);
}
public function files(){
return $this->hasMany('App\File');
}
//
}
Files Model
<?php
namespace App;
use Auth;
use Illuminate\Database\Eloquent\Model;
class File extends Model
{
public function scopeProject($query, $id)
{
return $query->where('project_id', $id);
}
public function scopeTask($query, $taskId)
{
return $query->where('task_id', $taskId);
}
public function task(){
return $this->belongsTo(Task::class);
}
}
Project Model
<?php
namespace App;
use Auth;
use App\Task;
use App\Comment;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
protected $fillable = ['project_name','project_notes','project_status','color','group'];
//
public function tasks(){
return $this->hasMany('App\Task');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
public function files()
{
return $this->hasMany('App\File');
}
}
We shall try one by one. First of all
Route::post('projects/{projects}/files', [
'uses' => 'FilesController@uploadAttachments',
'as' => 'projects.files',
'middleware' => ['auth']
]);
You have {projects} in the route and this parameter is not passed at all action="{{ route('projects.files', ['projectId'=> $project->id, 'taskId'=>$task->id]) }}". Why is that? ['projectId'=> $project->id, 'taskId'=>$task->id] will be passed as get parameters
What is the error you get now when you submit the form?
You are not passing task variable and project variable as defined in the route change your form and route like below
{{ Form::open(['route' => ['projects.files', $project->id, $task->id], 'files'=>true]) }}
and your route
Route::post('projects/{project_id}/files/{task_id}', [
'uses' => 'FilesController@uploadAttachments',
'as' => 'projects.files',
'middleware' => ['auth']
]);
And in your controller
public function uploadAttachments(Request $request, $project_id,$task_id)
{
//So here your have $project_id and $task_id do whatever you want
}
@divanoli when I try to go view file this error massage encounterd
Undefined variable: task (View: C:\Users\Desktop\acxian\resources\views\files\form.blade.php)
@RamjithAp occurred following error massage
according to your latest comment now clear Undefined variable: task (View: C:\Users\Desktop\acxian\resources\views\files\form.blade.php) but occurred new massage
Class 'Form' not found
Please or to participate in this conversation.