【问题标题】:How to get value from different table in blade laravel?如何从刀片 laravel 中的不同表中获取价值?
【发布时间】:2020-05-25 13:57:12
【问题描述】:

我使用 laraver 生成器 (infyom)。 'companies' 表中有 partner_id,但没有合作伙伴的名称。我想要在另一个名为 partners 的表中显示合作伙伴名称。我该怎么做?

公司 (table.blade.php)

<div class="table-responsive">
    <table class="table" id="companies-table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Partner ID</th>
                <th>Type</th>
                <th>Nip</th>
                <th>Street</th>
                <th>Building Num</th>
                <th>Apartment Num</th>
                <th>Postal Code</th>
                <th>Place</th>
                <th>Floor</th>
                <th colspan="3">Action</th>
            </tr>
        </thead>
        <tbody>
        @foreach($companies as $company)
            <tr>
                <td>{{ $company->name }}</td>
                <td>{{ $company->partner_id }}</td>
                <td>{{ $company->type }}</td>
                <td>{{ $company->NIP }}</td>
                <td>{{ $company->street }}</td>
                <td>{{ $company->building_num }}</td>
                <td>{{ $company->apartment_num }}</td>
                <td>{{ $company->postal_code }}</td>
                <td>{{ $company->place }}</td>
                <td>{{ $company->floor }}</td>
                <td>
                    {!! Form::open(['route' => ['companies.destroy', $company->id], 'method' => 'delete']) !!}
                    <div class='btn-group'>
                        <a href="{{ route('companies.show', [$company->id]) }}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-eye-open"></i></a>
                        <a href="{{ route('companies.edit', [$company->id]) }}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-edit"></i></a>
                        {!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
                    </div>
                    {!! Form::close() !!}
                </td>
            </tr>
        @endforeach
        </tbody>
    </table>
</div>

【问题讨论】:

标签: php laravel infyom


【解决方案1】:

您可以使用 Eloquent 提供的关系功能,通过 Company 上的 partner_id 字段将 Company 模型与 Partner 模型相关联:

class Company ...
{
    public function partner()
    {
        return $this->belongsTo(Partner::class);
    }
}

在您的控制器中,您可以预先加载传递给视图的所有公司模型的关系

public function index()
{
    return view('companies.index', [
        'companies' => Company::with('partner')->paginate(...),
    ]);
}

在视图中,您可以访问每个公司的合作伙伴:

@foreach ($companies as $company)
    ...
    {{ $company->partner->name ?? 'none'}}
    ...
@endforeach

Laravel 7.x Docs - Eloquent - Relationships - One To Many (Inverse)belongsTo

Laravel 7.x Docs - Eloquent - Relationships - Eager Loadingwith

Laravel 7.x Docs - Eloquent - Relationships - Dynamic Properties

【讨论】:

    【解决方案2】:

    将此功能添加到您的公司模型中

    public function partners(){
         return $this->hasMany(Partner::class, 'partner_id', 'id');
    }
    
    /*
    using in your foreach => $company->partners->name
    
    Don't forget to import Partner model to your Companies model
    */
    

    【讨论】:

      猜你喜欢
      • 2020-12-02
      • 2019-09-11
      • 1970-01-01
      • 2014-12-04
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 2019-04-17
      • 2015-08-06
      相关资源
      最近更新 更多