Previously I was using raw SQL statements in a non-Laravel environment so this is a port from that to Laravel.
In reading up on JOINs I read that Eloquent uses models and relationships to link them together. That's where I'm having a hard time, I grasp the concept but for whatever reason I'm not getting the desired results.
Here's a snippet of what I'm working with.
users
id, emailaddress, nickname, password, accountlevel
accountlevel
id, name
all users records have a FKey on accountlevel to accountlevel.id
my previous SQL was as follows:
SELECT id as users_id, emailaddress as email, nickname as nickname, password as password, accountlevel.id as accountlevel_id, accountlevel.name as accountlevel_name FROM users LEFT JOIN accountlevel ON users.accountlevel = accountlevel.id WHERE users.id = X
in Eloquent I've created the following:
models/User.php
class User extends Eloquent {
protected $table = 'users';
public function accountlevel() {
return $this->hasOne('AccountLevel', 'id', 'user.accountlevel');
}
}
models/AccountLevel.php
class AccountLevel extends Eloquent {
protected $table = 'accountlevel';
public function user() {
return $this->belongsTo('User', 'accountlevel', 'accountlevel.id');
}
}
controllers/UsersController.php
class UsersController extends \BaseController {
public function showUser($id) {
return View::make('profile')
->with('user', User::find($id));
}
}
The expected result is that whenever I query users for a specific user the user model will automatically query the AccountLevel table for the associated record in the users.accountlevel foreign key link. Instead I'm getting Laravel errors. Obviously I'm implementing this wrong.
Can anyone shed some light on what I'm not doing right implementing what should be very basic LEFT JOIN statements within Eloquent?
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