【发布时间】:2020-08-11 22:42:10
【问题描述】:
我正在开发一个具有以下雄辩模型的 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 模型中的所有文件:
我的控制器:
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