代码之家  ›  专栏  ›  技术社区  ›  John

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

  •  0
  • John  · 技术社区  · 7 年前

    我正在开发合作伙伴,为我的项目管理应用程序添加方法。这是我的合作者添加表单。 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 }}"
    

    路线

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

    但是,当我点击collaborators时,会在显示的错误消息之后添加按钮。

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

    已编辑

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

    查看表单的其他部分

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

    合作者表单路由

    Route::get('/collaborators', function(){ 
       return view('collaborators.form'); 
    })->name('collaborators.form');
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Norris Oduro    7 年前

    在表单页面中,您正在检查 @if( $collaborators) $collaborators 变量不为空,然后运行 foreach 在它下面。

    提交表单后,添加协作者并重定向回,没有 collaborators . if条件然后尝试检查变量是否为空。此时变量尚未定义,因此会引发该错误。要修复此错误,请使用 合作者 这样地:

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

    with 方法将数据放入会话中,我建议您手动重定向到视图并刷新 message 这一观点。

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

    编辑2

    return redirect()->back()

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

    然后改变你的 routes

    Route::get('/project/{project}/collaborators', function($id){ 
           $collaborators = Collaboration::all();
           $project = Project::findOrFail($id);
           return view('collaborators.form',compact('collaborators','project')); 
        })->name('collaborators.form');