我需要帮助才能在laravel中进行查询。 我有三张桌子
1)类别(包含id和name列)。
2)Geometry(包含id和name列)。
3)实用(包含id和name列以及Category_id,Geometry_id列/键)
现在我想用Category_id和Geometry_id来搜索实用的东西。 Practical.php模型文件是..
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Model;
use App\Models\Practical;
class Practical extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $table='practical';
protected $fillable = [
'name',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
public function category()
{
return $this->belongsTo('App\Models\Category');
}
public function geometry()
{
return $this->belongsTo('App\Models\Geometry');
}
}
类别和几何模型代码是
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Model;
use App\Models\Geometry;
class Geometry extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $table='geometry';
protected $fillable = [
'name',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
];
public function practical()
{
return $this->hasMany('App\Models\Practical');
}
}
我知道如何只使用一个ID.Category或几何像这样查询..
$Categories=Category::where('id',1)->firstOrFail();
但我没有通过检查类别和几何的ID来查询。
答案 0 :(得分:1)
现在我想用Category_id和Geometry_id
来搜索实用的东西
如果您想按类别ID和几何ID搜索实用内容,只需进行简单的查询:
Practical::where('category_id', $categoryId)
->where('geometry_id', $geometryId)
->get();