hey all,
my actual situation.
controller:
public function show($id)
{
$account = Account::with('characters')->find($id);
return view('accounts.show')->with('account', $account);
}
view:
<p>
@if ($account->user != NULL)
Email: {{$account->user->name}}@example.com
@else
no User assigned
@endif
</p>
<td>UK</td>
@for ($i=2;$i<=4;$i++)
<td>
@if ($account->characters->where('server',$i)->where('country',"UK")->first() != NULL)
{{$account->characters->where('server',$i)->where('country',"UK")->first()->charid}}
@endif
</td>
@endfor
</tr>
I have got three tables, users, accounts, characters, with a 1 on 1 relation between users,accounts(foreign key) and a 1 to many between accounts,characters(foreign key).
I am looking for a better way as it seems that it runs quite a lot database querys. In addition it is not really convenient to use 'if-clauses' in the view.
i tried as followed:
$account = DB::table('accounts as a')->where('a.id',$id)->join('characters as c', 'c.account_id', '=','a.id')->select('a.accid as accid', 'c.charid as charid', 'c.server as server', 'c.country as country')->get();
but now i am missing the username and I still need to control if the character exists, as there is not always a character on the server.
Thank you in advance.