Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RamjithAp's avatar

This happening because you have not installed html/form packages in your laravel. So change your form line to this

 <form class="form-vertical" role="form"
                                    enctype="multipart/form-data"
                                    method="post"
                                    action="{{ route('projects.files', $project->id, $task->id)}}">
divanoli's avatar

@Flex

Class 'Form' not found

is occurring because you need to install laravel collective package. You could fix this issue without using Form class. Where is the controller code which returns this file views\files\form.blade.php

Flex's avatar
Level 4

@RamjithAp according to your latest comment again occured this error massage

Undefined variable: task (View: C:\Users\Desktop\acxian\resources\views\files\form.blade.php)
Flex's avatar
Level 4

@divanoli files/form.blade.php file is include with tasks/show.blade.php file

tasks/show.blade.php

<h2>{{$task->project->project_name}}</h2>
<hr>

{{$task->task_name}}
<hr>

{!!$task->body!!}

<hr>


@include('comments.form')



@include('files.form')


show file view controller is TasksController

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);
divanoli's avatar

try this instead @include('files.form',['task'=>$task]) tell me the result please.

Flex's avatar
Level 4

@divanoli it is generated same error here

Undefined variable: task (View: C:\Users\Desktop\acxian\resources\views\files\form.blade.php)
Flex's avatar
Level 4

@RamjithAp, @Snapey, @divanoli Dear all collaborated with this thered see My comment box same to file attachment is is working fine.

comments/form.blade.php

@foreach ($task->project->comments as $comment)

   
    <div>
                    <div><i class="fa fa-check-square-o"></i>
                    <span>{{ $comment->comments }} by
                       <span style="font-style: italic;color: #09f;">
                       {{ ($comment->user()->first()->username === auth()->user()->username) ? 'You' : $comment->user()->first()->username }} 
                       </span>
                    </span></div>
                    <a href="/projects/{{ $task->project->id }}/comments/{{ $comment->id }}/edit">Edit</a>
                    <button class="btn btn-danger delete pull-right"
                      data-action="/projects/{{ $task->project->id }}/comments/{{ $comment->id }}"
                      data-token="{{csrf_token()}}">
                    <i class="fa fa-trash-o"></i>Delete
                    </button>
                </div>
                <hr/>

@endforeach

   
    <form class="form-vertical" role="form" method="post" action="{{ route('projects.comments.create', ['projectId'=> $project->id, 'taskId'=>$task->id])}}">
        <div class="form-group{{ $errors->has('comments') ? ' has-error' : '' }}">
            <textarea name="comments" class="form-control" style="width:80%;" id="comment" rows="5" cols="5"></textarea>
            @if ($errors->has('comments'))
                <span class="help-block">{{ $errors->first('comments') }}</span>
            @endif
        </div>

        <div class="form-group">
            <button type="submit" class="btn btn-info">Add Comment</button>
        </div>
        <input type="hidden" name="_token" value="{{ csrf_token() }}">
    </form>
</div>

CommentController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;
use App\Comment;
use App\Task;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CommentsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function postNewComment(Request $request, $id, Comment $comment,$taskId)
    {
       $this->validate($request, [
            'comments'     => 'required|min:5',
        ]);

       $comment->comments       = $request->input('comments');
       $comment->project_id     = $id;
       $comment->user_id        = Auth::user()->id;
       $comment->task_id        = $taskId;
       $comment->save();

       return redirect()->back()->with('info', 'Comment posted successfully');
    }
}

routes

Route::post('projects/{projects}/tasks/{tasks}/comments', [
    'uses' => 'CommentsController@postNewComment',
    'as'   => 'projects.comments.create',
    'middleware' => ['auth']
]);

and include file tasks/show.blade.php

<h2>{{$task->project->project_name}}</h2>
<hr>

{{$task->task_name}}
<hr>

{!!$task->body!!}

<hr>


@include('comments.form') // this is working fine



@include('files.form')


My comment include file is working fine but problem with file form. can you anybody guess the problem with file form

Flex's avatar
Level 4

@RamjithAp now error massage do not display. but My addFiles button do not work. how to configure My addFiles button. this is My new form

 <form class="form-vertical" role="form"
                                    enctype="multipart/form-data"
                                    method="post"
                                    action="Form::open(['route' => ['projects.files', $project->id, $task->id], 'files'=>true]) ">
                <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>
djdiramio's avatar

@Flex your form's action is not correct. The Form::open(...) would replace your entire opening <form> tag. Instead, change your action back to:

action="{{ route('projects.files', $project->id, $task->id)}}">

Snapey's avatar

Why are you getting distracted with using Form collective. It has nothing to do with your issue and people who should know better are just confusing matters.

The problem is that $task is not making it to your form, or you are looking at the wrong code entirely.

The dd that we did before (and got an instance of task) put it in the form itself.

<?php dd($task); ?>

 <form class="form-vertical" role="form"
 

// etc, your form as you had it before.
Flex's avatar
Level 4

I change My form action as this

action=" route('projects.files', ['taskId'=>$task->id])">

now did not come undefined variable task error but how now I can send $task->id to Filecontroller method SaveUpload ????

Snapey's avatar

If your route is still like this;

Route::post('projects/{projects}/tasks/{tasks}/comments', [
    'uses' => 'CommentsController@postNewComment',
    'as'   => 'projects.comments.create',
    'middleware' => ['auth']
]);

then you need to pass tasks not taskId in the route function

action=" route('projects.files', ['tasks'=>$task->id])">
Flex's avatar
Level 4

@Snapey did like your comments. it is working but have some problem. that means in my table column task_id save this type values

route('projects.files', 'tasks'=>$task->id)

not save task_id as 1,2,3,....etc

how can solve this problem....

Flex's avatar
Level 4

@Snapey @djdiramio @RamjithAp amazing it is working without form.blade.php form action

 <form class="form-vertical" role="form"
                                    enctype="multipart/form-data"
                                    method="post"
                                   action=" ">

            <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>

but I dont know how is it working......can you guess it?

Snapey's avatar

If you don't specify action then the form will be POST back to the same URL as the form itself.

Previous

Please or to participate in this conversation.