检索未经过修订或培训的摩托车的可用性

时间:2019-07-12 20:10:13

标签: laravel laravel-5

我正在寻找一种方法来检索未经过修订或培训的moto的可用性。

我有一个名为motorbike的表单,其中包含3个字段(matriculation, number_motorbike, status)

在这里,我有3辆摩托车和1辆不可用 number motorbike -> 000001

enter image description here

我的第二种形式是revision,有4个字段(date_revision_start, date_revision_end, garage, fk_motorbike)

我只有1辆摩托车。

enter image description here

在我的表单trainings中,我有2个字段(date_sitting, fk_motorbike)

enter image description here

在这里,当我添加例如正在修订的1辆摩托车时,没有阻塞,这是我的问题。

以create.blade.php的形式,是否有可能例如每天仅查看可用的摩托车?

enter image description here

我真的很想解决这个问题。在此先感谢您的帮助和宝贵的时间。

编辑:2019年7月13日

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Motorbike;
use App\Training;
use App\Revision;

class TrainingController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $trainings = Training::oldest()->paginate(5);
        $motorbikes = Motorbike::doesntHave('revisions')->doesntHave('trainings')->pluck('number_motorbike', 'id');
        return view('admin.trainings.index', compact('trainings'))
             ->with('i', (request()->input('page',1) -1)*5);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {   

        $motorbikes = Motorbike::all();
        $motorbikes = Motorbike::doesntHave('revisions')->doesntHave('trainings')->pluck('number_motorbike', 'id');
        return view('admin.trainings.create', compact('motorbikes', 'trainings'));

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {   

        $request->validate([
                'date_sitting' => 'required',
                'fk_motorbike' => 'required'
        ]);

        Training::create($request->all());

        return redirect()->route('trainings.index')
                ->with('success', 'new data created successfully');
    }

create.blade.php

<div class="form-group {{ $errors->has('fk_motorbike') ? 'has-error' : '' }}">
            <label for="company-content">Number motorbike </label>
            <select name="fk_motorbike" id="fk_motorbike" class="form-control" required="required" value="{{ old('fk_motorbike')}}"/>
              <option value="">Choice motorbike</option>
                @foreach($motorbikes as $motorbike)
                <option value="{{$motorbike->id}}" {{ old('fk_motorbike') == $motorbike->id ?  'selected' : '' }} >
                {{$motorbike->number_motorbike}} 
              </option>
                @endforeach
                {!! $errors->first('fk_motorbike', '<span class="help-block">:message</span>') !!}
            </select>
          </div>

enter image description here

2 个答案:

答案 0 :(得分:1)

编辑:由于您的表架构(例如外键),它尚不起作用,但是范围available满足了您的需求。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Revision extends Model
{
    public function motorbike()
    {
        return $this->belongsTo('App\Motorbike');
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Training extends Model
{
    public function motorbike()
    {
        return $this->belongsTo('App\Motorbike');
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Motorbike extends Model
{
    public function revisions()
    {
        return $this->hasMany('App\Revision');
    }

    public function trainings()
    {
        return $this->hasMany('App\Training');
    }

    public function scopeAvailable($query, $date = null)
    {
        $date = $date ?: date('Y-m-d');
        return $query
            ->whereDoesntHave('revisions', function ($query) use ($date) {
                $query
                    ->whereDate('date_revision_start', '<=', $date)
                    ->whereDate('date_revision_end', '>=', $date);
            })
            ->whereDoesntHave('trainings', function ($query) use ($date) {
                $query->whereDate('date_sitting', $date);
            });
    }
}
$date = '2019-07-12';
$availableMotorbikes = App\Motorbike::available($date)->get();

答案 1 :(得分:1)

最简单的方法是查询未修订且尚未接受培训的摩托车,并为下拉列表生成idname。像这样:

$motorbikes = Motorbike::doesntHave('revision')->doesntHave('training')->pluck("name", "id");

如果您只需要时间冲突的培训,则可以将doesntHave替换为whereDoesntHave(),并创建一个用于过滤日期的闭包。

以上内容假设您在正确设置的摩托车模型上具有关系,可以进行修订和培训。如果没有,则可以稍微老一些以获得仅针对那些没有经过培训且没有经过修改的摩托车的下拉菜单所需要的东西。

// Get all motorbike ids with trainings:
$motorbikeIdsInTraining = Training::pluck('fk_motorbike')->toArray();
// Get all motorbike ids with revisions:
$motorbikeIdsInRevision = Revision::pluck('fk_motorbike')->toArray();
// Get all motorbikes
$motorbikesAll = Motorbike::all();
// Loop on all bikes to see if they are in revision or training:
$goodBikeIdsForDropdown = [];
foreach($motorbikesAll as $bike){ 
     if(!in_array($bike->id, $motorbikeIdsInTraining) && ! in_array($bike->id, $motorbikeIdsInRevision){
         $goodBikeIdsForDropdown[$bike->id] = $bike->number_motorbike
     }
 }

该底部并不是最有效的-旨在说明一种方法,如果您没有正确建立关系。强烈建议设置它们以利用Laravel的强大功能,但这应该可以作为在当前点测试代码的一种方法。