Search code examples
phplaraveleloquentlaravel-3

Laravel Eloquent outputs c


I'm using Laravel's Eloquent, most of the times without any problems. But now i'm running in to a problem and can't seem to fix it. Google doesn't come with a solution...

$global_data['credentials']     = Company::find(Auth::user()->company_id)->credentials()->first();

View::share('data', $global_data);

The relation in Company model:

public function credentials(){
  return $this->has_one('credentials');
}

The credentials echos "c" to every page. The "c" is outputted before the document type. When I remove the credentials line the "c" disappears.

Page source:

c<!DOCTYPE html>
<html lang="nl">
<head>

Anyone?


Solution

  • I honestly doubt Eloquent has anything to do with this, but probably an unfortunate "c" somewhere on your code. This happens quite usually and the solution is a nasty mouse chasing (or "c" in this case).

    One possible cause for this is a file that starts with c<?php or ends with ?>c, usually files that are loaded through Autoloader (models, libraries and bundles).

    I would bet it's at the beginning of your Credential model file, but may be elsewhere either. If it's not at Credential.php, a possible way to find it is to do a step-by-step addition of anything involved on building that $global_data['credentials'] = .... line.

    1. Try a hardcoded credential, if no "c" appears, it's probably coming from Company or Auth files.
    2. Then try doing a Company::find($1)->credentials(), if no "c" appears, it's probably on your Auth or User (it's included with Auth).
    3. Keep doing that until you find it.

    Edit:

    This happens because when Autoloader includes a file, PHP parses it like it would do on old-syle PHP, or should I say, like any view.

    If your file starts with <?php and ends with ?> not blanklines or characters before or after them, nothing is output'ed until Laravel's view rendering. But, if there's an output on any of those files, it gets into PHP's output buffer and gets rendered before your views.

    It's a good practice to always open those non-outputting files with <?php at the first line and never close it with ?>.