每个行业都有很多项目
每个项目仅属于一个行业
行业表的列如下:id, industry_name, industry_code, created_at, updated_at
项目表的列如下:id, industry_id (foreign key), user_id, project_name, project_start_date, project_end_date, created_at, updated_at
我想在我的index.blade.php
中显示项目名称和行业名称(查看)
我已经创建了项目和行业模型。如何在我的视图中显示结果?
Industry.php model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Industry extends Model
{
public function project()
{
return $this->hasMany('App\Models\Project');
}
}
Project.php model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
public function industry()
{
return $this->belongsTo('App\Models\Industry');
}
}
最佳答案
像这样 ...
$Industry = App\Industry ::find(1);
echo $Industry ->Project ->project_name;
https://laravel.com/docs/5.5/eloquent-relationships#one-to-many