mysql - 如何从LARAVEL中同一数据库下的另一个表中获取值?
问题描述
我想问如何从同一个数据库下的另一个表中调用值?
数据库表名:bit_app_policy_category
在数据库下我有这些列:
- ID
- 代码
- 描述
- parent_id
- 地位
同一数据库下的另一个数据库表名称:company_policy
在数据库下我有这些列:
- ID
- policy_category_id
- 政策标题
- 版本号
- 政策详情
- 到期日
- 文件路径
至于现在我想链接表bit_app_policy_category
并将 id 值的列放入company_policy
- policy_category_id
。我不知道如何编写代码。
这是我当前的代码:
<form method="post" action="{{route('policy.store')}}">
{{csrf_field()}}
@csrf
<div class="form-group">
<label for="bit_app_policy_category_parent">Parent Category</label>
<select id="bit_app_policy_category_parent" name="parent_id" class="form-control">
<option value=" {{"$parents->id"}} </option>n>
</select>
</div>
<div class="form-group">
<label for="company_policy_policy_title">Policy Title<span class="required">*</span></label>
<input id="company_policy_policy_title" type="text" name="policy_title" class="form-control" placeholder="Please Enter the policy title" />
</div>
<div class="form-group">
<label for="company_policy_version-no">Version-no<span class="required">*</span></label>
<input id="company_policy_version-no" type="text" name="version_no" class="form-control" placeholder="Please Enter the Version-no" />
</div>
<div class="form-group">
<label for="company_policy_policy_details">Policy Details<span class="required">*</span></label>
<input id="company_policy_policy_details" type="text" name="policy_details" class="form-control" placeholder="Please Enter the Policy Details" />
</div>
<div class="form-group">
<label for="company_policy_expiry_date">Expiry Date<span class="required">*</span></label>
<input id="company_policy_expiry_date" type="datetime-local" name="expiry_date" class="form-control" placeholder="Please Enter the Expiry Date time" />
</div>
<div class="form-group">
<label for="company_policy_file_path">Policy File Path<span class="required">*</span></label>
<input id="company_policy_file_path" type="text" name="file_path" class="form-control" placeholder="Please Enter the file path" />
</div>
<div class="form-group">
<input type="submit" class="btn btn btn-primary" />
<a href="{{url('/policy')}}" ><span class="mj btn btn btn-danger">Back</span></a>
<a href="{{url('/')}}" ><span class="mj btn btn btn-danger">Back to home</span></a>
</div>
</form>
</div>
</div>
@endsection
解决方案
首先创建两个模型 1. PolicyCategory并将代码放在模型中
public function companyPolicies()
{
return $this->hasMany('App\CompanyPolicy');
}
2.公司政策
public function policyCategory()
{
return $this->belongsTo('App\PolicyCategory',policy_category_id);
}
然后在您的控制器中,您可以获得相关表的所有信息。比如说,您在 policy_category 下有一个/两个公司,其 id 为 1。现在您可以获得所有公司信息,如下所示
$policy_category = PolicyCategory::find(1);
dd($policy_category->companyPolicies);
试试这个,让我知道这真的是你想要的还是不想要的
推荐阅读
- graphql - 使用 apollo angular GraphQL 在 Drupal8 中添加用户
- c++ - 插入元素 (TinyXml)
- angular - 角度 6 中左侧菜单之前的页面加载
- python - 计算欧几里得距离时 sklearn.metrics.pairwise_distances_argmin_min 的奇怪结果
- kubernetes - Ignite-Kubernetes 集成中的异常
- bash - 带有多个参数的 Bash shell 脚本“while”循环
- r - 使用字符对象替换列中的所有数据
- java - 如何获取字符串数组并将它们进一步分成对象?
- javascript - 检测包含 http 请求的 forEach 循环的结尾?
- amazon-web-services - 为什么使用 SNS 触发 lambda 函数,而不是 API 网关?