如果在laravel 5.4中记录存在禁用按钮

时间:2017-05-22 03:20:07

标签: javascript php laravel laravel-5 laravel-5.3

如果我在我的用户表中注册了一个用户,我试图禁用一个按钮,但是我使用的方法不会停用我想要处于非活动状态的按钮,它会将其停用到所有寄存器。

这是我的用户迁移:

public function up()
{ 
    Schema::create('users', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('id_employee')->unsigned();
        $table->string('username')->unique();//Cedula
        $table->string('name')->nullable();
        $table->string('password', 60)->nullable();
        $table->string('email', 60)->nullable();
        $table->rememberToken();
        $table->timestamps();                  
        $table->foreign('id_employee')->references('id')->on('employees')
              ->onUpdate('cascade')->onDelete('cascade');
    });
}

这是我的用户模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    protected $table = 'employees';

    protected $fillable = ['doc_number', 'user_code', 'name', 'last_name', 'settlement', 'address', 'city' , 'zip_code', 'email', 'phone', 'position', 'departments_id', 'hire_date'];

    protected $guarded = ['id'];

    public function departments()
    {
        return $this->belongsTo('App\Department');
    }

    public function user()
    {
        return $this->hasOne('App\User');
    }

    //Query para buscador
    public function scopeName($query, $name)
    {
      $query->leftJoin('departments','employees.departments_id','=','departments.id')
            ->select('employees.*','departments.id as deptID','departments.name as department')
            ->where('employees.name','like',"%$name%")
            ->orWhere('employees.last_name','like',"%$name%")
            ->orWhere('employees.position','like',"%$name%")
            ->orWhere('employees.email','like',"%$name%")
            ->orWhere('employees.doc_number','like',"%$name%")              
            ->orWhere('departments.name','like',"%$name%"); 
    }
}

这是我的索引方法控制器:

public function index(Request $request)
    {
        $employees = Employee::name($request->get('criteria'))->orderBy('name','asc')->paginate(6);

        $departments = Department::orderBy('id', 'desc')->pluck('name', 'id');

        $users = User::orderBy('id', 'desc')->pluck('username', 'id');

        return view('employees.index', compact('employees', 'departments', 'users'))
        ->with('i', ($request->input('page', 1) - 1) * 6);
    }

这是我的带有按钮的刀片视图:

<td>
    <div class="btn-group">                                     
        @if(count($users) === 0)
        <a href="{{ URL::route('padron.create', $employee->id) }}" type="submit" class="btn btn-success btn-sm" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Confirmar votante">Confirmar</a>

        @else
            <a type="submit" class="btn btn-success btn-sm" data-toggle="tooltip" rel="tooltip" data-placement="top" disabled="disabled" title="Confirmar votante">Confirmar</a>

        @endif
    </div>
</td>

2 个答案:

答案 0 :(得分:1)

简单地说,如果没有用户,你的逻辑会显示按钮,否则禁用按钮。看来你有users,因此if总是失败,而其他人也会参与其中。

如果要为单个按钮禁用“确认”,则必须在某处使用用户的ID。像

这样的东西
@if($employee->id == ...) 
// depending on your logic

答案 1 :(得分:1)

  1. 使用控制器功能
  2. 中的成功返回语句发回一个值
  3. 并使用此
  4. 检查您的视图中是否存在该值