繁体   English   中英

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

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

我有 2 个表格,一个是车辆,另一个是道路税。

我的“车辆”标签有一个 id 和注册字段,它与我的“道路税”标签有关系,它有 id、vehicle_id、vaild from 和 expires 字段。 我有一对多的关系,因为我的车辆在我对它们征税时会有多年的历史

我需要最简单的方法来列出我所有的车辆,按照需要先重新征税的顺序。

我最接近的是在税收到期时让我的车辆上市。 我真的很努力让它们按照我需要的顺序排列。 我对 php 和 mysql 有基本的了解,所以希望有人能指出我需要关注的地方。 我想我可以只通过过期列来订购,就像我如何通过注册成功订购一样。 这是因为我的 expires 字段来自 realtionship 表吗?

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

车辆 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");
    }
    
}

看法

 @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

你可以做的是一个with()方法并作为查询构建器传递。

所以基本上你只需要 1 个关系vehicle->hasMany(Road_tax::class)

您的 model 应该是:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Vehicle extends Model
{

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

所以如果你想让每辆车都列出他们最新的道路税

您可以使用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'));
    }
}

此方法将列出与车辆相关的 1 道路税

暂无
暂无

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

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