【问题标题】:Update column by clicking the approve and decline button通过单击批准和拒绝按钮更新列
【发布时间】:2020-03-16 06:28:56
【问题描述】:

这是我的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 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

离开模型

<?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');
  }
 }

状态类型模型

 <?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 播种器

   <?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');
    }
   }

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

我还必须给出任何路线。我对在更新函数中或单独放置条件的位置感到困惑。

【问题讨论】:

  • 通过简单的点击按钮不会做任何动作。您需要将相应的 ID 与按钮操作一起传递,并需要相应地对其进行更新。
  • 是的,但是我卡在传递 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' ]); }); `

标签: php laravel crud


【解决方案1】:

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

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

}

【讨论】:

  • 嗯我没有这样的东西,但这不起作用,可以使用资源控制器更新吗??
  • 是的,正确的是它应该如何工作..但我有错误说表单不是声明。为什么它是?
  • ```Facade\Ignition\Exceptions\ViewException Class 'Form' not found(查看:/Applications/MAMP/htdocs/blog/resources/views/admin/dashboard.blade.php)@987654321 @
  • 安装 laravelcollective/html :按照此处指定的步骤进行操作 stackoverflow.com/a/47056600/5101460
  • 嗨,我安装了它,我得到了这个:(这条路线不支持 PUT 方法。支持的方法:GET、HEAD。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 2020-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多