Php Laravel试图查询关系模型有很多
我的申请表如下:Php Laravel试图查询关系模型有很多,php,laravel,laravel-5,Php,Laravel,Laravel 5,我的申请表如下: 项目 projects\u plot\u type 项目可以有多种打印类型。我在项目模型中设置了以下关系 /** * The plot types that belong to the project. * * @return Object */ public function plotTypes() { return $this->hasMany('App\Models\Project\ProjectsPlotTypes'); } 我希望能够通过在项目
项目
projects\u plot\u type
项目可以有多种打印类型。我在项目模型中设置了以下关系
/**
* The plot types that belong to the project.
*
* @return Object
*/
public function plotTypes()
{
return $this->hasMany('App\Models\Project\ProjectsPlotTypes');
}
我希望能够通过在项目模型上查询绘图类型的名称来获取绘图类型
我尝试过这个,但不起作用:
$project->with('plotTypes')->whereHas('plotTypes', function ($query) use ($row) {
$query->where('name', $row->plot_name);
})->first()->plotTypes->first()->id;
有人能给我指出正确的方向吗
根据以下注释,$result
的输出为:
Project {#705 ▼
#table: "projects"
#fillable: array:7 [▶]
+timestamps: true
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:10 [▶]
#original: array:10 [▶]
#relations: array:1 [▼
"plotTypes" => Collection {#769 ▼
#items: array:1 [▼
0 => ProjectsPlotTypes {#774 ▼
#table: "projects_plot_types"
#fillable: array:2 [▶]
+timestamps: false
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
#attributes: array:4 [▼
"id" => "2"
"project_id" => "1"
"name" => "TYPE 3 - VENTILATION"
"budget" => "245.69"
]
#original: array:4 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
]
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
也许这就是你想要的:
$result = $project->with(['plotTypes' => function($query) use ($row) {
return $query->where('name', $row->plot_name)->first();
}])->first();
dd($result); // print the result and die
更新1
从集合中获取具有BelongsTo
关系的第一项:
$plotTypeResult = $result->plotTypes[0]->id;
也许这就是你想要的:
$result = $project->with(['plotTypes' => function($query) use ($row) {
return $query->where('name', $row->plot_name)->first();
}])->first();
dd($result); // print the result and die
更新1
从集合中获取具有BelongsTo
关系的第一项:
$plotTypeResult = $result->plotTypes[0]->id;
您也可以像下面这样使用连接查询
$result = Illuminate\Support\Facades\DB::table('projects')
->join('projects_plot_types', 'projects_plot_types.id', '=', 'projects.project_plot_id')
->where('projects_plot_types.name', '=', $row->plot_name)
->first();
print_r($result);
您也可以像下面这样使用连接查询
$result = Illuminate\Support\Facades\DB::table('projects')
->join('projects_plot_types', 'projects_plot_types.id', '=', 'projects.project_plot_id')
->where('projects_plot_types.name', '=', $row->plot_name)
->first();
print_r($result);
谢谢,但是如何访问plotTypes id
$result->id
大概会打印项目id?$result->plotTypes()->first()->id
应该可以得到你想要的。这段代码似乎仍然无法获取我需要的行。当你执行dd($result)时会得到什么代码>?使用输出更新您的问题。无论是否与绘图名称匹配,它似乎总是在绘图类型
表中获得第一个结果?谢谢,但是我如何访问绘图类型id$result->id
大概会打印项目id?$result->plotTypes()->first()->id
应该可以得到你想要的。这段代码似乎仍然无法获取我需要的行。当你执行dd($result)时会得到什么代码>?用输出更新您的问题。它似乎总是在绘图类型
表中获得第一个结果,而不管它是否与绘图名称匹配?