单击批准和拒绝按钮更新列



这是我的dashboard.blade.php

<td>
<a href="#" class="btn btn-success">Approve</a>
</td>
<td>
<a href="#" class="btn btn-danger">Decline</a>
</td>

这是我的LeaveController.php


<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use Redirect;   // using redirect 
use Auth;       // import auth
use AppModelsLeaveType;
use AppModelsStatusType;
use AppModelsLeave;
class LeaveController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$leaves = Leave::with('type', 'applied_by','statustype')->where('user_id', Auth::id())->get();
return view('leave.index', compact('leaves'));
}
/**
* Show the form for creating a new resource.
*
* @return IlluminateHttpResponse
*/
public function create()
{
$leavetypes = LeaveType::all(); // multiple select view (LeaveType is the model)
return view('leave.create', compact('leavetypes'));
}
/**
* Store a newly created resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
$leave = $request->get('leave');
$leave['user_id'] = Auth::id();
$leave['status_id']= StatusType::find(1)->id;
$leave = Leave::create($leave);
return Redirect::to(route('leave.create'));

}
/**
* Display the specified resource.
*
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function show($id)
{
$leave = Leave::with('type', 'applied_by', 'statustype')->find($id);
return view ('leave.show', compact('leave'));

}
/**
* Show the form for editing the specified resource.
*
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function update(Request $request, $id)
{
}
/**
* Remove the specified resource from storage.
*
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function destroy($id)
{
//
}
}

index.blade.php

@extends('layouts.master')
@section('title')
User Dashboard
@endsection
@section('content')
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header"> 
<h4 class="card-title">Leave Status</h4>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">
<thead class=" text-primary">
<th>ID</th>
<th>Leave Type</th>
<th>Leave Start date</th>
<th>Leave End date</th>
<th>Status<th>
<th>Action</th>
</thead>
<tbody> 
@foreach ($leaves as $leave)          
<tr>
<td>{{$leave->id}}</td>
<td>{{$leave->type->type}}</td>
<td>{{$leave->start}}</td>
<td>{{$leave->end}}</td>
<td>{{$leave->statustype->status}}</td> 
<td>
</td>
<td>
<a href="{{ route('leave.show', $leave->id) }}" class="btn btn-warning">View</a>
</td>
</tr> 
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="col-md-12">
<div class="card card-plain">
<div class="card-header">
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table">

</table>
</div>
</div>
</div>
</div>
</div>

@endsection
@section('scripts')
@endsection

离开模型

<?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class Leave extends Model
{
protected $fillable = [
'type_id', 'start', 'end', 'remarks', 'user_id','status_id'
];
public function applied_by()
{
return $this->belongsTo('AppUser', 'user_id');
}
public function type()
{
return $this->belongsTo('AppModelsLeaveType', 'type_id');
}
public function statustype()
{
return $this->belongsTo('AppModelsStatusType','status_id');
}
}

Statustype型号

<?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class Leave extends Model
{
protected $fillable = [
'type_id', 'start', 'end', 'remarks', 'user_id','status_id'
];
public function applied_by()
{
return $this->belongsTo('AppUser', 'user_id');
}
public function type()
{
return $this->belongsTo('AppModelsLeaveType', 'type_id');
}
public function statustype()
{
return $this->belongsTo('AppModelsStatusType','status_id');
}
}

状态类型播种机

<?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class Leave extends Model
{
protected $fillable = [
'type_id', 'start', 'end', 'remarks', 'user_id','status_id'
];
public function applied_by()
{
return $this->belongsTo('AppUser', 'user_id');
}
public function type()
{
return $this->belongsTo('AppModelsLeaveType', 'type_id');
}
public function statustype()
{
return $this->belongsTo('AppModelsStatusType','status_id');
}
}

如何通过点击按钮来更新状态列

我还必须给出任何路线。我不知道把条件放在哪里,无论是在更新函数中还是单独放置。

资源路由的命名不同请检查以下

Route::group(['middleware' => ['auth']], function () {
Route::resource('leave', 'LeaveController', ['names' => [
'index' => 'leave.index',
'create' => 'leave.create',
'store' => 'leave.store',
'show' => 'leave.show',
'edit' => 'leave.edit',
'update' => 'leave.update',
'destroy' => 'leave.destroy',
]]);

});

在dashboard.blade.php中,您可以通过在按钮上传递相应的ID来定义路线。我将href标记更改为按钮。它可以使用与您以前使用的href相同的类。单击此按钮时,它将使用put方法调用更新函数。

每个ID都有一个激活和停用按钮,并通过其名称属性区分为actdeact

注意在定义从HTML表单调用的PUT、PATCH或DELETE路由时,您需要向表单添加一个隐藏的_method字段。与_method域一起发送的值将用作HTTP请求方法

{{Form::open(array('url'=>'leave/update','method'=>'post','class'=>'form-login'))}}
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button  class ="btn btn-success" name="act" value="{{$leave->id}}">activate</button>
<button  class ="btn btn-success" name="deact" value="{{$leave->id}}">deactivate</button>
{{Form::close()}}

在LeaveController中定义相应的功能并相应更新。如果点击激活/停用,则相应地更新该ID的状态字段:

public function update(Request $request, $id)
{
if(isset($request->act)) { //if you click activate button, update accordingly
$leaveId = $request->act; 
AppModelsLeave::where('id', $leaveId )->update(['status' => 1]);
}if(isset($request->deact)) { //if you click deactivate button, update accordingly
$leaveId = $request->deact;
AppModelsLeave::where('id', $leaveId )->update(['status' => 2]);
}
}

最新更新