John
John

Reputation: 145

is this Laravel Relationship error undefined variable $task

I am developing project management app in Laravel 5.2. in My application I have one project many tasks and one task have many file attachments. this is My file attachment view file

@foreach($task->files as $file) //line 14
                <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>

and My FileController is this

use Cloudder;
use App\File as File;
use App\Task;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class FilesController extends Controller
{
public function uploadAttachments(Request $request,$id,$taskId)
    {
         $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,$taskId);

and route is this

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

but got this error

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

how can fix this problem?

file Model

use Auth;
use App\Task;
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);
}

Upvotes: 0

Views: 636

Answers (3)

timod
timod

Reputation: 595

public function show($project_id,$task_id) { 

$project = Project::find($project_id); 
$task = Task::find($task_id); 

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

}

Upvotes: 0

Bhupendra Mistry
Bhupendra Mistry

Reputation: 628

You have to pass the $task collection to your blade view as shown below

$task = Task::all() //collect task collection as per your logic
return view('files.form', compact('task'));

Upvotes: 1

Bora Erbasoglu
Bora Erbasoglu

Reputation: 107

You have to pass $task data to your view (blade file) like this

return view('files.form', $task);

Upvotes: 0

Related Questions