As far as I know you only have to say that a user has an account level and not both! It's all in the documentation
Thanks blackbirddev, that's the documentation I've been using to port my SQL statements over.
I can see how I can find 1 relational record by using a foreign key but not how to call a record and it's associated relational data.
example:
the doc shows me how to pull a record from 'phone' that is related to a 'user'
my need:
I need to pull a 'user' record and also the associated 'accountlevel' record related from the user record
This is where I'm getting a logic disconnect with Eloquent. It looks like I've structured my models correctly but when I try to add
->with('accountlevel')
it generates a Laravel error instead of returning the user record + associated data from accountlevel as designed.
I built this in Laravel's Query Builder as a work around.
public function showUser($id) {
$user = DB::table('users')
->leftJoin('accountlevel', 'users.accountlevel', '=', 'accountlevel.id')
->select('users.id as user_id', 'users.emailaddress as emailaddress', 'users.nickname as nickname', 'users.password as password', 'accountlevel.id as accountlevel_id', 'accountlevel.name as accountlevel_name')
->where('users.id', '=', $id)
->first()
return View::make('profile')->with('user', $user);
}
views/profile.blade.php
@extends('layout')
@section('title', ucfirst($user->nickname))
@section('content')
<p><strong>users</strong><br />
id: <span>{{ $user->user_id }}</span><br />
emailaddress: <span>{{ $user->emailaddress }}</span><br />
nickname: <span>{{ ucfirst($user->nickname) }}</span><br />
password: <span>{{ $user->password }}</span><br />
accountlevel: <span>{{ ucfirst($user->accountlevel_name) }} ({{ $user->accountlevel_id }})</span>
</p>
@stop
Which works but still doesn't use Eloquent.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community