Flex

How to fix Undefined variable: collaborators

Posted 1 year ago by Flex

I am developing collaborators adding methods to my project management application. this is my collaborators add form

collaborators/form.blade.php
 <div class="col-md-4" style="border:1px solid #ccc;margin-left:15px;padding:10px;">
        <h4 class="page-header">
            Collaborators
        </h4>
        @if( $collaborators)
           @foreach( $collaborators as $collaborator)
                <div>
                    <div>
                        <span>
                            <img src="{{ $collaborator->user()->first()->getAvatarUrl() }}" />
                        </span>
                    </div>
                    <button class="btn btn-sm btn-danger delete" style="margin-top:5px;padding:4px;width:35px;"
                      data-action="/projects/{{ $project->id }}/collaborators/{{ $collaborator->collaborator_id }}"
                      data-token="{{csrf_token()}}">
                    <i class="fa fa-trash-o"></i>
                    </button>
                </div>
                <hr/>
           @endforeach
        @endif
        <form class="form-vertical" role="form" method="post" action="{{ route('projects.collaborators.create', $project->id) }}">
        <div class="form-group{{ $errors->has('collaborator') ? ' has-error' : '' }}">
            <label> Add New </label>
            {!! mention()->asText('collaborator', old('collaborator'), 'users', 'username', 'form-control') !!}
            @if ($errors->has('collaborator'))
                <span class="help-block">{{ $errors->first('collaborator') }}</span>
            @endif
        </div>

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

CollaboratorController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;
use App\User;
use App\Project;
use App\Collaboration;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ProjectCollaboratorsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function addCollaborator(Request $request, $id, Collaboration $collaboration)
    {
       $this->validate($request, [
            'collaborator'     => 'required|min:5',
        ]);

       $collaborator_username           = substr(trim($request->input('collaborator')),1);
       $collaboration->project_id       = $id;
       if( is_null($this->getId($collaborator_username)))
       {
            return redirect()->back()->with('warning', 'This user does not exist');
       }

       $collaborator = $this->isCollaborator($id, $this->getId($collaborator_username));
       if(! is_null($collaboration))
       {
            return redirect()->back()->with('warning', 'This user is already a collaborator on this project');
       }

       $collaboration->collaborator_id  = $this->getId($collaborator_username);
       $collaboration->save();

       return redirect()->back()->with('info', "{$collaborator_username} has been added to your project successfully");
    }

    /**
     * Get id of the user
     * @param  string $username
     * @return mixed  null | integer
     */
    private function getId($username)
    {
        $result = User::where('username', $username)->first();

        return (is_null($result)) ? null : $result->id;
    }

    /**
     * Check if the user about to be added as a collaborator is already one on the project
     * @param  int  $projectId
     * @param  int  $collaboratorId
     * @return boolean
     */
    private function isCollaborator($projectId, $collaboratorId)
    {
        return Collaboration::where('project_id', $projectId)
                            ->where('collaborator_id', $collaboratorId)
                            ->first();
    }

}

routes

Route::post('projects/{projects}/collaborators', [
    'uses' => 'Project\Collaborators\[email protected]',
    'as'   => 'projects.collaborators.create',
    'middleware' => ['auth']
]);

but when I click to collaborators adding buttons following error messages displayed.

Undefined variable: collaborators (View: C:\Users\Flex\Desktop\ddd\resources\views\collaborators\form.blade.php)

how can I fix this problem

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.