如何从 blade laravel 中的不同 table 获取值?
How to get value from different table in blade laravel?
我使用 laraver 生成器 (infyom)。在 table 'companies' 中有 partner_id 但没有合作伙伴的名字。我想要在另一个名为 partners 的 table 中显示合作伙伴名称。我该怎么做?
公司 (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>
将此功能添加到您的公司模型中
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
*/
您可以使用 Eloquent 提供的关系功能,通过公司上的此 partner_id
字段将公司模型关联到合作伙伴模型:
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 Loading with
Laravel 7.x Docs - Eloquent - Relationships - Dynamic Properties
我使用 laraver 生成器 (infyom)。在 table 'companies' 中有 partner_id 但没有合作伙伴的名字。我想要在另一个名为 partners 的 table 中显示合作伙伴名称。我该怎么做?
公司 (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>
将此功能添加到您的公司模型中
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
*/
您可以使用 Eloquent 提供的关系功能,通过公司上的此 partner_id
字段将公司模型关联到合作伙伴模型:
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 Loading with
Laravel 7.x Docs - Eloquent - Relationships - Dynamic Properties