首页 > 解决方案 > 如何从LARAVEL中同一数据库下的另一个表中获取值?

问题描述

我想问如何从同一个数据库下的另一个表中调用值?

数据库表名:bit_app_policy_category

在数据库下我有这些列:

  1. ID
  2. 代码
  3. 描述
  4. parent_id
  5. 地位

同一数据库下的另一个数据库表名称:company_policy 在数据库下我有这些列:

  1. ID
  2. policy_category_id
  3. 政策标题
  4. 版本号
  5. 政策详情
  6. 到期日
  7. 文件路径

至于现在我想链接表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

标签: mysqlsqllaravel

解决方案


首先创建两个模型 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);

试试这个,让我知道这真的是你想要的还是不想要的


推荐阅读