【问题标题】:How to query in terms of two columns in laravel eloquent orm如何根据 laravel eloquent orm 中的两列进行查询
【发布时间】:2017-07-17 05:09:56
【问题描述】:

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

1)Category(包含 id 和 name 列)。

2) 几何(包含 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();

但我无法通过检查 Category 和 Geometry 的 id 来查询。

【问题讨论】:

    标签: php sql laravel orm eloquent


    【解决方案1】:

    现在我想根据 Category_id 和 Geometry_id 搜索实用程序

    如果您想按类别 ID 和几何 ID 搜索实用程序,只需进行简单查询:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-16
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2015-02-08
      • 1970-01-01
      • 2016-06-13
      相关资源
      最近更新 更多