1

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.

Oops
  • 1,373
  • 3
  • 16
  • 46
Archaana
  • 83
  • 1
  • 2
  • 8
  • By simply clicking on the buttons will not do any actions. You need to pass the respective ID along with the button action and need to update it accordingly. – Oops Mar 16 '20 at 06:38
  • yeahh but m stuck in passing the ID and give button action – Archaana Mar 16 '20 at 06:42
  • Can you share your routes? – Oops Mar 16 '20 at 06:48
  • ` 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' ]); }); ` – Archaana Mar 16 '20 at 06:51

1 Answers1

0

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

}
Oops
  • 1,373
  • 3
  • 16
  • 46
  • hmm i have dont something like this but this doesnt work, can update using resource controller ?? – Archaana Mar 16 '20 at 07:58
  • yes correct this is how it should work .. but i am having error saying the form is not declare.why it is ? – Archaana Mar 17 '20 at 03:32
  • ```Facade\Ignition\Exceptions\ViewException Class 'Form' not found (View: /Applications/MAMP/htdocs/blog/resources/views/admin/dashboard.blade.php) http://127.0.0.1:8000/dashboard – Archaana Mar 17 '20 at 03:33
  • install laravelcollective/html : Follow the steps specified here https://stackoverflow.com/a/47056600/5101460 – Oops Mar 17 '20 at 04:57
  • Hii i installed it and i am getting this :( The PUT method is not supported for this route. Supported methods: GET, HEAD. – Archaana Mar 17 '20 at 06:58
  • i am so sorry i am new to laravel – Archaana Mar 17 '20 at 06:58
  • Please check my updated answer. HTML forms do not support PUT, PATCH or DELETE actions. So, 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. Let me know the result. – Oops Mar 17 '20 at 07:11
  • Hope you have given POST method here : {{Form::open(array('url'=>'leave/update','method'=>'post','class'=>'form-login'))}}. – Oops Mar 17 '20 at 07:58
  • i did but still i cannot. can we do it without the form class ? – Archaana Mar 17 '20 at 08:19