0

i need print task_id in file table when some user is going to attach files. this is My form.blade.php file action

files/form.blade.php

action="{{ route('projects.files', ['projectId'=> $project->id, 'taskId'=>$task->id])}}"> //and this is line 39

and this is FilesController

 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;


        $file->save();
    }

and this is My file attachment routes

Route::post('projects/{projects}/files', [
     'uses' => 'FilesController@uploadAttachments',
     'as'   => 'projects.files',
     'middleware' => ['auth']
]);

and My file/form.blade.php is include with show.blade.php file in task folder in view directory

tasks/show.blade.php

{{$task->task_name}}
<hr>
{!!$task->body!!}
<hr>
@include('comments.form')
@include('files.form') // form.blade.php include

but unfortunately I got this following errors

ErrorException in ae0a86ab95cb7f092eb44a17fd000e94f21b305d.php line 39:
Undefined variable: task (View: C:\Users\13\Desktop\ddd\resources\views\files\form.blade.php)

how can fix this problem?

Edited this is My 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);
 }

full form.blade.php codes

<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', $project->id, $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>
John
  • 135
  • 1
  • 3
  • 16
  • 1
    the problem is probably in the show action where you pass the task variable, can you show us the code in your show action ? – teeyo Nov 28 '17 at 10:26
  • actually No any action in show.blade.php file? – John Nov 28 '17 at 10:32
  • did you mean show.blade.php ? – John Nov 28 '17 at 10:37
  • No, I meant the action the returns the view `show.blade.php`, here you are showing us the route `Route::post` to `uploadAttachments` action, but the form is displayed using a `Route::get` that displays the show.blade.php, and the problem is there – teeyo Nov 28 '17 at 10:40
  • 1
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – urfusion Nov 28 '17 at 10:45

2 Answers2

0

The error means that $task is not being passed to your blade. In your Controller Code which loads tasks/show.blade.php, you need to make sure you're passing in the $task variable.

Update your code from:

return view('tasks.show')->withProject($project)->withTask($task);

To:

return view('tasks.show', ['task' => $task, 'project' => $project]);

You also need to update:

@include('files.form')

to:

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

See if clearing you cache helps resolve the issue. Run the following artisan command:

php artisan cache:clear

If you get permission errors, you may need to run:

sudo php artisan cache:clear

Or you can manually clear the cache my deleting all the files in the /storage/framework/views folder.

Niraj Shah
  • 15,087
  • 3
  • 41
  • 60
0

If get your problem. you migh be looking for this kind of..Try this :-

public function show($project_id,$task_id)
{
$project = Project::find($project_id);
$task = Task::find($task_id);

return view('files.form')->with(comapct('task','project')); //update return line only!
}

Hope it helps!

kunal
  • 4,122
  • 12
  • 40
  • 75