简体   繁体   English

我将如何根据与它有关系的不同表格中的值对我的查询进行排序

[英]How would i orderBy my query based on values from a diffrent tabel that it is in a relationship with

i have 2 tabels one being vehicles and another being road tax.我有 2 个表格,一个是车辆,另一个是道路税。

my 'vehicles' tabel has an id & registration field which is in relationship with my 'road tax' tabel which has id, vehicle_id, vaild from & an expires field.我的“车辆”标签有一个 id 和注册字段,它与我的“道路税”标签有关系,它有 id、vehicle_id、vaild from 和 expires 字段。 i have a one to many relationship as my vehicles will have had many years history of when i taxed them我有一对多的关系,因为我的车辆在我对它们征税时会有多年的历史

i need the most simple way to list all my vehicles in order of which will need to be re-taxed first.我需要最简单的方法来列出我所有的车辆,按照需要先重新征税的顺序。

the closest i have is getting my vehicles to list when the tax is due to expire.我最接近的是在税收到期时让我的车辆上市。 i am really strugling to get them in the order i need.我真的很努力让它们按照我需要的顺序排列。 i have a basic understanding of php and mysql so hoping someone can shine a light on where i need to focus.我对 php 和 mysql 有基本的了解,所以希望有人能指出我需要关注的地方。 i thought i could either just orderBy the expires colum, just like how i can successfully orderBy registration.我想我可以只通过过期列来订购,就像我如何通过注册成功订购一样。 is this because my expires field originate from a realtionship table?这是因为我的 expires 字段来自 realtionship 表吗?

controller controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Road_tax;
use App\Models\Vehicle;
use Carbon\Carbon;
class DashboardController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */

    public function Index()
    {
        $road_taxes = Vehicle::with('latest_Road_Tax')->get()

        return view('dashboard.index', compact('road_taxes'));
    }
}

Vehicle Model车辆 Model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Vehicle extends Model
{

    public function Road_taxes()
    {
        return $this->hasMany(Road_tax::class);
    }

    public function latest_Road_Tax()
    {
        return $this->hasOne(Road_tax::class)->latest("expires");
    }
    
}

View看法

 @foreach($road_taxes as $road_tax) 
    <div class="dashboard-item-title">
      <h6 style="font-weight:600; margin-bottom:0px;">{{$road_tax->registration}}</h6>

      <span class="dashboard-item-body" style="margin-top:-10px;">
        <small style="font-weight:300; color:grey;">Tax expires for this vehicle on</small>
        <small style="font-weight:300"> | {{$road_tax->latest_Road_Tax->expires}}</small>
      </span>
    </div>
@endforeach

You can do is a with() method and pass as a query builder.你可以做的是一个with()方法并作为查询构建器传递。

So basically you only need 1 relationship vehicle->hasMany(Road_tax::class)所以基本上你只需要 1 个关系vehicle->hasMany(Road_tax::class)

Your model should be:您的 model 应该是:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Vehicle extends Model
{

    public function road_taxes()
    {
        return $this->hasMany(Road_tax::class);
    }
    
}

So if you want every vehicle to list their latest road tax所以如果你想让每辆车都列出他们最新的道路税

You can query this using with()您可以使用with()查询此信息

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Vehicle;
use Carbon\Carbon;

class DashboardController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */

    public function Index()
    {
        $road_taxes = Vehicle::with([
            'road_taxes' => function ($query) {
                $query->lastest()->limit(1);
            }
        ])->get();

        return view('dashboard.index', compact('road_taxes'));
    }
}

This method will list 1 road taxes associated to the vehicle此方法将列出与车辆相关的 1 道路税

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM