从关系键获取值

问题描述 投票:0回答:1

我正在开发具有以下雄辩模型的laravel应用程序:

  • App \ Models \ Dumpdb-我在这里存储所有文件信息;
  • App \ Models \ DumpDownloadHistory-在此保留所有客户端文件的下载历史记录;

这是Dumpdb表和模型-

// Database table
Schema::create('dumpdbs', function(Blueprint $table){
  $table->bigIncreaments('id');
  $table->string('hw')->nullable();
  $table->string('hwtype')->nullable();
  $table->string('hwtype2')->nullable();
  $table->integer('kw')->nullable();
  $table->string('dataset')->nullable();
  $table->string('enginetype')->nullable();
  $table->string('euro')->nullable();
  $table->timestamps();
});

// Model
<?php

namespace App\Models;

use App\Models\DumpDownloadHistory;
use Illuminate\Database\Eloquent\Model;

class Dumpdb extends Model
{
  public function DumpDownloadHistory(){
    return $this->hasMany(DumpDownloadHistory::class);
  }
}

DumpDownloadHistory表格和模型-

// Table
Schema::create('dump_download_histories', function(Blueprint $table){
  $table->bigIncreaments('id');
  $table->unsignedBigInteger('user_id');
  $table->unsignedBigInteger('file_id');
  $table->string('dataset')->nullable();
  $table->integer('downloadCost')->nullable();
  $table->timestamps();

  $table->foreign('user_id')->references('id')->on('users');
  $table->foreign('file_id')->references('id')->on('dumpdbs');
});

// Model
<?php

namespace App\Models;

use App\Models\Dumpdb;

use Illuminate\Database\Eloquent\Model;

class DumpDownloadHistory extends Model
{
    protected $fillable = ['user_id', 'dataset', 'downloadCost', 'file_id'];

    protected $table = 'dump_download_histories';

    protected $primaryKey = 'id';

    public function dumpDb(){
      return $this->belongs_to(Dumpdb::class, 'file_id');
    }
}

我需要创建一个搜索过滤器,用户可以在其中搜索他已经拥有的文件买了。例如,这是Dumpdb模型中的所有文件:enter image description here

如果用户按下“已付款”复选框,这就是我想要的样子:enter image description here

这是我得到的:enter image description here

我的控制器:

    public function index(Request $request)
    {
      $dumpDb = Dumpdb::query();
      // Search values
      $hw = Dumpdb::select('hw')->distinct()->orderBy('hw', 'asc')->get();
      $pld = Dumpdb::select('hwtype')->distinct()->get();
      $hwtype = Dumpdb::select('hwtype', 'hwtype2')->distinct()->get();
      $kw = Dumpdb::select('kw')->whereNotNull('kw')->distinct()->orderBy('kw', 'asc')->get();
      $engine = Dumpdb::select('enginetype')->distinct()->get();
      $euro = Dumpdb::select('euro')->where('euro', '!=', 'NULL')->distinct()->get();

      // Search filters
      if ($request->has('hw') && $request->input('hw') != '') {
        $dumpDb = $dumpDb->where('hw', '=', $request->input('hw'));
      }

      if ($request->has('pld') && $request->input('pld') != '') {
        $dumpDb = $dumpDb->where(function ($query) use ($request) {
            $query->where('hwtype', $request->input('pld'))
                  ->orWhere('hwtype2', $request->input('pld'));
        });
      }

      if ($request->has('kw') && $request->input('kw') != '') {
        $dumpDb = $dumpDb->where('kw', '=', $request->input('kw'));
      }

      if ($request->has('engine') && $request->input('engine') != '') {
        $dumpDb = $dumpDb->where('enginetype', '=', $request->input('engine'));
      }

      if ($request->has('euro') && $request->input('euro') != '') {
        $dumpDb = $dumpDb->where('euro', '=', $request->input('euro'));
      }

      if ($request->has('ds') && $request->input('ds') != '') {
        $dumpDb = $dumpDb->where('dataset', 'LIKE', '%'. $request->input('ds') .'%');
      }

       // THIS FILTER NOT WORKING CORRECTLY
      if ($request->input('hasPayed') == '1') {
        $downloadHistory = DumpDownloadHistory::query();
        $dumpDb = $downloadHistory->where('user_id', auth()->user()->id)->where('downloadCost', '!=', 'NULL');
      }

      $dumpDb = $dumpDb->orderBy('id', 'desc')->paginate(30);
    return view('dumpDb.index', compact('hw', 'pld', 'kw', 'engine', 'euro', 'dumpDb',  'hwtype'));

任何帮助都将非常有用。预先感谢。

php laravel search filter eloquent
1个回答
1
投票

我相信您在获取DumpDb时应包括该关系。 Laravel提供了多种自动获取方法:

Eager loading

$books = App\Book::with(['author', 'publisher'])->get();

您应该从DumpDb查询(DumpDownloadHistory)中加载$dumpDb->with('dumpDownloadHistory')。完成后,您可以通过constraining the eager loads更改过滤器以查询相关的DumpDownloadHistory:

$dumpDb = dumpDb->with(['dumpDownloadHistory' => function ($query) {
    $query->where('user_id', auth()->user()->id)->where('downloadCost', '!=', 'NULL');
}]);

如果where查询为true,则只会加载关系。但是,您也希望约束DumpDb,因此可以使用whereHas

whereHas

这只会在模型具有某种关系类型时加载它,因此您可以像这样使用它:

$posts = App\Post::whereHas('comments', function (Builder $query) {
    $query->where('content', 'like', 'foo%');
})->get();

更新


编辑您的DumpDb模型以使用这种关系:

$dumpDb = $dumpDb->whereHas('dumpDownloadHistory', function ($query) {
    $query->where('user_id', auth()->user()->id)->where('downloadCost', '!=', 'NULL');
});

您的最终控制器应如下所示:

public function DumpDownloadHistory(){
    return $this->hasMany(DumpDownloadHistory::class, 'file_id');
}

摆脱// THIS FILTER NOT WORKING CORRECTLY if ($request->input('hasPayed') == '1') { $dumpDb = $dumpDb->whereHas('dumpDownloadHistory', function ($query) { $query->where('user_id', auth()->user()->id)->where('downloadCost', '!=', 'NULL'); }); }

© www.soinside.com 2019 - 2024. All rights reserved.