首页 > 解决方案 > 如何解决未定义的变量

问题描述

查看页面:

 @if(count($applications)>0)
<table class= "table table-striped">
 <tr>
    <th>Email</th>
     <th>Date Applied</th>
     <th>Leave Type</th>
     <th>Leave Status</th>
     <th></th>


 </tr>
 @foreach($applications as $application )
 <tr>
    <td>{{$application ->user->email}}</td>
    <td>{{$application ->dateApplied}}</td>
    <td>{{$application ->leaveType}}</td>
    <td>{{$application ->leaveStatus}}</td>
    <td>
     {!!Form::open(['action'=>['ApplicationAdminController@destroy', $application ->appId], 'method'=>'DELETE'])!!}
     {{Form::hidden('_method', 'DELETE')}}
     {{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
     {!!Form::close()!!}
    </td>


</tr>
 @endforeach
</table>

控制器:

 public function  destroy($appId)


{

    $application = Application::find( $appId);
    $application ->delete();

 $this->deleteLeave($appId);
    return redirect('/applicationAdminHistory')->with ('success', 'Application Removed');
}

public function deleteLeave($appId){

    $user_id = Auth::id();
    $req = DB::table('leave_summaries')
    ->where('user_id',$user_id)
    ->increment($leaveType,$day);

    }    

我想要做的是当单击删除按钮时,应用程序将被删除,并将在 leave_summaries 表中进行增量。leave_summaries 的增量基于已删除的应用程序中的日期和休假类型的值。运行此代码后,出现未定义变量错误,如下所示:

运行此代码后的结果

标签: phplaravel

解决方案


您没有定义这三个变量:$user_id、$leaveType、$day

您需要将这些值作为参数传递给 deleteLeave() 方法,或者必须在调用方法之前定义这些值。

public function deleteLeave($user_id, $leaveType, $day){
    $req = DB::table('leave_summaries')
        ->where('user_id',$user_id)
        ->increment($leaveType, $day);
}   

推荐阅读