Summer Sale! All accounts are 50% off this week.

Flex's avatar
Level 4

how to fix Undefined variable: task (View: C:\Users\Lilan\Desktop\ddd\resources\views\files\form.blade.php) in laravel

in my laravel project management application I have project and one project can create many tasks and one task have publish many files regarding to the task. so, in my file attachment form is this in files folder in view file

files/form.blade.php

@foreach ($task->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', ['projects' => $project->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>

and file model relationship with Task model is this

public function task(){
    return $this->belongsTo(Task::class);
}

Task model relationship with File model is this

public function files(){
    return $this->hasMany('App\File');
}

and now I included files/form.blade.php file with tasks/show.blade.php file as this

tasks/show.blade.php

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

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

{!!$task->body!!}

<hr>
@include('files.form')

then when I try to view task/show.blade.php file this following error is coming

Undefined variable: task (View: C:\Users\Lilan\Desktop\ddd\resources\views\files\form.blade.php)

how can fix this problem?

0 likes
11 replies
crnkovic's avatar

Show us your controller and a corresponding method. You didn't pass the $task variable to the view.

Flex's avatar
Level 4

@crnkovic 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)
    {
         $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)
    {
        $file = new File;
        $file->file_name  = $request->file('file_name')->getClientOriginalName();
        $file->file_url   = $fileUrl;
        $file->project_id = $id;
       // $file->task_id = $taskId;
        $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');
    }


    
    
}
crnkovic's avatar

No, I am asking about the controller that is rendering the files.form view.

Flex's avatar
Level 4

I did not use Controller to include(files.form) as My another include files....

crnkovic's avatar

Can you try to pass the task to the child view? However, Laravel should include all variables from the parent view to the child view automatically.

@include ('files.form', ['task' => $task])

Corban's avatar

If a project has many task then you are passing a collection. Have you tried using a foreach?

Flex's avatar
Level 4

@Carban yes please see My files/form.blade.php it is using foreach did you mean it?

Corban's avatar

@Flex I was looking at tasks/show.blade.php.

Either way I think your foreach has an error. I remember having a similar issue.

Try it this way in form.blade.php

@foreach ($task->project as $projects)
        <div>
             <div><i class="fa fa-check-square-o"></i>
                <span>
            <a href="{{ $projects->files->file_url }}" target="_blank">{{ $projects->files->file_name }}</a>
</span>
                    </div>
                </div>
                <hr/>
 @endforeach 
Flex's avatar
Level 4

see My @include('comments.form') in same blede file is working properly, but only @include('files.form') is occurred this error

see 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') // problem with this


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', $task->project->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>

and relationship with comments and project

Project Model

public function comments()
{
   return $this->hasMany('App\Comment');
}

Comment Model

 public function project()
    {
        return $this->belongsTo('App\Project');
    }

do you have any idea to solve this problem I am using Laravel 5.2

Please or to participate in this conversation.