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