当某些用户要附加文件时,我需要在文件表中打印task_id。
这是我的form.blade.php文件操作

文件/ form.blade.php

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


这是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();
    }


这是我的文件附件路由

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


在视图目录的任务文件夹中,show.blade.php文件中包含我的文件/form.blade.php

任务/ show.blade.php

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


但不幸的是我得到以下错误

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


如何解决这个问题?

已编辑
这是My TaskController的show方法

 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);
 }


完整的form.blade.php代码

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

最佳答案

如果得到您的问题。您可能正在寻找这种..试试这个:-

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

希望能帮助到你!

关于php - 在Laravel中 undefined variable :任务( View :C:\Users\13\Desktop\TDT\resources\views\files\form.blade.php),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47529140/

10-10 18:35
查看更多