Andrew Vanusi
Andrew Vanusi

Reputation: 499

Get specific value Laravel

I have car table with status field and there's "available", and "breakdown". I also got fleet CRUD, once I add fleet I want to show car with only "available" status. what should I add ?

here's my fleet controller code :

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

here's my add form code :

<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>

Upvotes: 2

Views: 1181

Answers (3)

Indra
Indra

Reputation: 702

In your model add a scope

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

And then wherever you need to use it:

Car::available()->get()

or

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

More info here: https://laravel.com/docs/5.3/eloquent#query-scopes

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

If I understand your question correctly, this will create a list of available cars:

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

Upvotes: 0

Filip Koblański
Filip Koblański

Reputation: 9988

Have you try this:

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

Upvotes: 1

Related Questions