Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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

http://laravel.com/docs/eloquent#one-to-one

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

DrFark drfark Joined 25 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.