php – Update column by clicking the approve and decline button-ThrowExceptions

Exception or error:

Here is my dashboard.blade.php

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

Here is my LeaveController.php


<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Redirect;   // using redirect 
use Auth;       // import auth
use App\Models\LeaveType;
use App\Models\StatusType;
use App\Models\Leave;

class LeaveController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    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 \Illuminate\Http\Response
     */
    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  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    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 \Illuminate\Http\Response
     */
    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 \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    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

Leave Model

<?php

 namespace App\Models;

 use Illuminate\Database\Eloquent\Model;

 class Leave extends Model
 {
  protected $fillable = [
    'type_id', 'start', 'end', 'remarks', 'user_id','status_id'
  ];

  public function applied_by()
  {
    return $this->belongsTo('App\User', 'user_id');
  }

  public function type()
  {
    return $this->belongsTo('App\Models\LeaveType', 'type_id');
  }

  public function statustype()
  {
    return $this->belongsTo('App\Models\StatusType','status_id');
  }
 }

Statustype model

 <?php

  namespace App\Models;

  use Illuminate\Database\Eloquent\Model;

  class Leave extends Model
  {
    protected $fillable = [
    'type_id', 'start', 'end', 'remarks', 'user_id','status_id'
    ];

   public function applied_by()
   {
    return $this->belongsTo('App\User', 'user_id');
    }

   public function type()
   {
    return $this->belongsTo('App\Models\LeaveType', 'type_id');
    }

   public function statustype()
   {
    return $this->belongsTo('App\Models\StatusType','status_id');
   }
  }

StatusType Seeder

   <?php

    namespace App\Models;

    use Illuminate\Database\Eloquent\Model;

     class Leave extends Model
     {
       protected $fillable = [
     'type_id', 'start', 'end', 'remarks', 'user_id','status_id'
      ];

     public function applied_by()
     {
      return $this->belongsTo('App\User', 'user_id');
      }

     public function type()
     {
     return $this->belongsTo('App\Models\LeaveType', 'type_id');
     }

     public function statustype()
    {
     return $this->belongsTo('App\Models\StatusType','status_id');
    }
   }

How can I update the status column by clicking the buttons

I have to give any route yet too.I’m confused about where to put the condition whether in the update function or separately.

How to solve:

Resource routes are named differently
check the below

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',
]]);

});

In you dashboard.blade.php , you can define route by passing your corresponding ID on a button.I changed the href tag to a button.It can use the same class as that of href as you used before. On click of this button it will invoke the update function with put method.

Each IDs will have an activate and deactivate buttons and is distinguished by its name attribute as act and deact.

Note : When defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method.

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

Define the corresponding functions in your LeaveController and update accordingly.If activate/deactivate is clicked, then update the status field of that ID accordingly:

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

}

Leave a Reply

Your email address will not be published. Required fields are marked *