获得具体价值Laravel

时间:2016-10-07 07:25:58

标签: php laravel

我有带有状态字段的车牌表,并且有"可用"和"故障"。 我也有车队CRUD,一旦我添加车队,我想要只用"可用"状态。我应该添加什么?

这是我的车队控制器代码:

<?php

namespace App\Http\Controllers;

use App\Http\Requests;
use App\Models\Fleet;
use App\Models\Car;
use App\Models\Attendance;
use App\Helpers\Enums\FleetStatus;
use App\Models\AttendanceDetail;
use Illuminate\Http\Request;
use Yajra\Datatables\Datatables;
use App\Http\Controllers\Base\MasterController;
use Route;
use Illuminate\View\View;

class FleetController extends MasterController
{
protected $detailView = 'fleet.show';
protected $indexView = 'fleet.index';
protected $createView = 'fleet.form';
protected $editView = 'fleet.form';

protected $routeBindModel = 'fleet'; //Route Model Binding name in RouteServiceProvider
protected $redirectPageWhenFormSuccess = 'fleet.index'; //Route Name
protected $title = 'Fleet';

public function save(Request $request, $obj = null) {

    if (!$obj) {
        $obj = new Fleet;
    }
    return $this->saveHandler($request, $obj);
}

//Must have this method if need datatable;
public function datatableBuilder($obj = null) {
    return Fleet::query();
}

public function makeDatatable($obj) {

    return Datatables::of($obj)
    ->addColumn('action', function ($model) {
            return $this->makeActionButtonsForDatatable($model);
    })
    ->editColumn('status', function($model){
        return FleetStatus::getString($model->status);
    })
    ->make(true);
}

public function render(View $view, $route = null, $obj = null, $method = 'POST') {
    $statusList = FleetStatus::getArray();
    $carStatusList = Car::pluck('plate_no', 'id');
    $attendanceList = Attendance::pluck('driver_id', 'id');
    $workingHourList = Attendance::pluck('working_hours', 'id');
    $view->with(compact('statusList', 'carStatusList', 'attendanceList', 'workingHourList'));
    return parent::render($view, $route, $obj, $method);
}
}

这是我的添加表单代码:

<div class="col-xs-12 col-sm-12 col-md-12">
                            <div class="form-group">
                                <label>Car </label>
                                {!! Form::select('car_id', $carStatusList, null, array('class' => 'form-control')) !!}
                            </div>
                        </div>

3 个答案:

答案 0 :(得分:1)

你试试这个:

$carStatusList = Car::where('status', 'available')->pluck('plate_no', 'id')

答案 1 :(得分:1)

在您的模型中添加范围

override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 40
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

然后你需要在哪里使用它:

public function scopeAvailable($query){
    return $query->where('status', '=', 'available');
}

Car::available()->get()

此处有更多信息:https://laravel.com/docs/5.3/eloquent#query-scopes

答案 2 :(得分:0)

如果我理解你的问题,这将创建一个可用汽车列表:

Car::where('status','available')->pluck('plate_no', 'id');