2017-10-15 116 views
0

我正在開發協作者將方法添加到我的項目管理應用程序中。這是我的合作者添加表單。 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\[email protected]', 
    'as' => 'projects.collaborators.create', 
    'middleware' => ['auth'] 
]); 

,但是當我點擊合作者加入以下顯示的錯誤消息的按鈕。

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

如何顯示控制器? –

+0

請參閱我編輯的問題我正在顯示我的所有項目Collaborator控制器 – John

+0

'addCollaborator'處理後請求。什麼控制器處理顯示錶單的GET請求? –

回答

1

在表單頁面,你正在檢查@if($collaborators)它檢查是否$collaborators變量不爲空,然後運行它下面的foreach

在您提交表單之後,您添加協作者並重新導向,但沒有collaborators。 if條件然後試圖檢查變量是否爲空。此時該變量尚未定義,因此會引發該錯誤。要修正這個錯誤,返回重定向回用collaborators這樣的:

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'); 
+0

現在出現此錯誤**未定義的變量:項目(查看:C:\ Users \ John \ Desktop \ ddd \ resources \ views \協作者\ form.blade.php)** – John

+0

頁面上是否有'project'變量? –

+0

你可以發佈jsfiddle上的整個頁面的內容嗎? –

相關問題