如何用Laravel雄辩的Orm中的两列查询



我需要帮助在Laravel中进行查询。我有三张桌子

1)类别(包含ID和名称列)。

2)几何(包含ID和名称列)。

3)实用(包含ID和名称列以及category_id,geometry_id列/键)

现在,我想根据类别_id和geometry_id搜索实践。ractial.php模型文件是..

    <?php
namespace AppModels;
use IlluminateFoundationAuthUser as Model;
use AppModelsPractical;
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('AppModelsCategory');
    }
    public function geometry()
    {
        return $this->belongsTo('AppModelsGeometry');
    }
}

类别和几何模型代码是

    <?php
namespace AppModels;
use IlluminateFoundationAuthUser as Model;
use AppModelsGeometry;
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('AppModelsPractical');
    }

}

我知道如何仅使用一个ID。类别或类似的几何形状。

$Categories=Category::where('id',1)->firstOrFail();

但是我没有通过检查类别和几何形状的ID来查询。

现在,我想根据category_id和geometry_id

搜索实践

如果您想按类别ID和几何ID搜索实践,请进行简单的查询:

Practical::where('category_id', $categoryId)
         ->where('geometry_id', $geometryId)
         ->get();

最新更新