dscizpq790832708 2017-10-15 13:16
浏览 36
已采纳

如何修复“未定义变量:协作者”?

I am developing collaborators adding methods to my project management application. this is my collaborators add form. colllaborators/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 }}"

routes

Route::post('projects/{projects}/collaborators', [
    'uses' => 'Project\Collaborators\Controller@addCollaborator',
    '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\dddesources\views\collaborators\form.blade.php)

how can I fix this problem edited

class ProjectCollaboratorsController extends Controller
{

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

    private function getId($username)
    {
        $result = User::where('username', $username)->first();

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


    private function isCollaborator($projectId, $collaboratorId)
    {
        return Collaboration::where('project_id', $projectId)
                            ->where('collaborator_id', $collaboratorId)
                            ->first();
    }

}

see My other part of the form

<form class="form-vertical" role="form" method="post" action="{{ route('projects.collaborators.create', $project->id) }}">

collaborator form route

Route::get('/collaborators', function(){ 
   return view('collaborators.form'); 
})->name('collaborators.form');
  • 写回答

1条回答 默认 最新

  • dqwyghl0649 2017-10-15 14:37
    关注

    In your form page, you are checking @if( $collaborators) which checks if the $collaborators variable is not empty and then runs the foreach below it.

    After you submit your form, you add the collaborator and redirect back with no collaborators. The if condition then tries to check if the variable is empty. At this point the variable has not been defined and hence it throws that error. To fix this error, return redirect back with the collaborators like this:

     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();
    //Get all collaborators 
      $collaborators = Collaboration::all(); //if this is how you get all collaborators
    //Get the project too
     $project = Project::findOrFail($id);
           return redirect()->back()->with(['collaborators'=>$collaborators,'project'=>$project,'info'=> "{$collaborator_username} has been added to your project successfully"]);
        }
    

    EDIT:

    Using the with method puts the data in the session, i would suggest that you manually redirect to the view and flash the message to that view.

    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();
        //Get all collaborators 
          $collaborators = Collaboration::all(); //if this is how you get all collaborators
         //Get the project too
           $project = Project::findOrFail($id);
               return redirect()->route('collaborators.form',['collaborators'=>$collaborators,'project'=>$project])->with('info',"{$collaborator_username} has been added to your project successfully");
            }
    

    Edit 2

    I have changed all the return redirect()->back()'s

    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;
    
           //Get the project too
           $project = Project::findOrFail($id);
    
           if( is_null($this->getId($collaborator_username)))
           {
                return redirect()->route('collaborators.form',['project'=>$project])->with('warning', 'This user does not exist');
           }
    
           $collaborator = $this->isCollaborator($id, $this->getId($collaborator_username));
           if(! is_null($collaboration))
           {
                return redirect()->route('collaborators.form',['project'=>$project])->with('warning', 'This user is already a collaborator on this project');
           }
    
           $collaboration->collaborator_id  = $this->getId($collaborator_username);
           $collaboration->save();
    
           return redirect()->route('collaborators.form',['project'=>$project])->with('info',"{$collaborator_username} has been added to your project successfully");
        }
    

    And change your routes to

    Route::get('/project/{project}/collaborators', function($id){ 
           $collaborators = Collaboration::all();
           $project = Project::findOrFail($id);
           return view('collaborators.form',compact('collaborators','project')); 
        })->name('collaborators.form');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题