所以基本来说,我有两个表“ Accounts”和“ Characters”,它们通过一个ID链接在一起 每个“帐户”都有3个“字符” 所以我想显示链接到主要“帐户”的3个字符
这是我的HomeController
{
$data = DB::table('characters')
->join('accounts', 'accounts.cUid', '=','characters.pUniqueID')->get();
return view('home', compact('data'));
}
这是我的家。刀片
@foreach($data as $per)
@if( $per->pUniqueID == Auth::user()->cUid )
....
....
@else
<script>window.location.href = '{{url("/characters")}}'; </script>
@endif
@endforeach
答案 0 :(得分:1)
配置模型中的关系:
Accounts.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Accounts extends Model
{
public function characters()
{
return $this->hasMany('App\Characters');
}
}
因此,在您的查询中:
$data = Accounts::with("characters")->get();
return view('home', compact('data'));
在您的模板中:
@foreach($data as $per)
@if($per->pUniqueID == Auth::user()->cUid)
@foreach($per->characters as $character)
{{$character->id}} //or any other character attribute
@endforeach
@else
<script>window.location.href = '{{url("/characters")}}'; </script>
@endif
@endforeach