【发布时间】: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