0

Hi all,

I am using Ardent as a standalone package, together with Laravels Database package.

Things are set up, and I can query just fine - But when I define my relations, I get a white screen when I try to dump the expected data.

I have double checked and the data + relations are set up just as they should in the DB.

This is my two test models:

PAGES MODEL:

<?php namespace App\models;

use LaravelBook\Ardent\Ardent as Ardent;

class page extends Ardent
{
    # Table name for model
    protected $table = 'generator_pages';

    # Relationships
    public static $relationsData = array( 'menu' => array( self::HAS_ONE, '\App\models\menu', 'foreignKey' => 'ID' ) );

}

MENUS MODEL:

<?php namespace App\models;

use LaravelBook\Ardent\Ardent as Ardent;

class menu extends Ardent
{
    # Table name for model
    protected $table = 'generator_menus';

    # Relationships
    public static $relationsData = array('pages' => array(self::HAS_MANY, '\App\models\page', 'foreignKey' => 'menu_id'));
}

No errors are thrown. If I try extending the models with eloquent I still dont get any related data returned.

This should get me the pages for a specific menu id, but I get no data returned:

$pages_for_menu = menu::find( 1 )->pages;   

foreach ( $pages_for_menu as $page ) {
    echo $page->page_name . '<br />';
}

What am I missing here?

Regards, Klemme

2
Contributors
6
Replies
36
Views
2 Years
Discussion Span
Last Post by diafol
0

Haven't used Ardent - still blundering away with Eloquent. However, are the relationships right? In Eloquent the relationships are: hasOne/belongsTo or hasMany/belongsTo or belongsToMany/belongsToMany - there are other more complicated ones. I haven't seen hasOne/hasMany.

0

Have time to look into it again now, sorry diafol for the late reply on your feedback.

I have changed ardent, and am now trying to use eloquent for these two related models:

PAGE MODEL:

<?php namespace App\models;

use \Illuminate\Database\Eloquent\Model;

class page extends Model
{
    # Table name for model
    public $table = 'generator_pages';

    # Relationships

    public function menu()
    {
        return $this->belongsTo('App\models\menu', 'menu_id');
    }
}

MENU MODEL:

<?php namespace App\models;

use \Illuminate\Database\Eloquent\Model;

class menu extends Model
{
    # Table name for model
    protected $table = 'generator_menus';

    # Relationships

    public function pages()
    {
        return $this->hasMany('App\models\page', 'menu_id', 'id');
    }
}

A page can only belong to one menu.

A menu can have many pages.

The pages table has a menu_id foreign key to associate to the menu table.

So when I query the page model, I am able to access the menu like this:

var_dump( page::find(1)->menu ) // Works fine

When I query the menu model, and wants to retrieve the pages that is related, I get an empty collection:

$menu = menu::find( 1 );
var_dump( $menu->pages );

// Result in browser:
object(Illuminate\Database\Eloquent\Collection)[24]
  protected 'items' => 
    array (size=0)
      empty

var_dump( $menu->pages()->first() ); 
    // Returns null

var_dump( $menu->pages() );
// Result in browser

object(Illuminate\Database\Eloquent\Relations\HasMany)[14]
  protected 'foreignKey' => string 'generator_pages.menu_id' (length=23)
  protected 'query' => 
object(Illuminate\Database\Eloquent\Builder)[21]
  protected 'query' => 
    object(Illuminate\Database\Query\Builder)[23]
      protected 'connection' => 
        object(Illuminate\Database\MySqlConnection)[15]
          ...
      protected 'grammar' => 
        object(Illuminate\Database\Query\Grammars\MySqlGrammar)[17]
          ...
      protected 'processor' => 
        object(Illuminate\Database\Query\Processors\Processor)[18]
          ...
      protected 'bindings' => 
        array (size=0)
          ...
      public 'aggregate' => null
      public 'columns' => null
      public 'distinct' => boolean false
      public 'from' => string 'generator_pages' (length=15)
      public 'joins' => null
      public 'wheres' => 
        array (size=1)
          ...
      public 'groups' => null
      public 'havings' => null
      public 'orders' => null
      public 'limit' => null
      public 'offset' => null
      public 'unions' => null
      protected 'cacheKey' => null
      protected 'cacheMinutes' => null
      protected 'operators' => 
        array (size=16)
          ...
      protected 'model' => 
    object(App\models\page)[13]
      public 'table' => string 'generator_pages' (length=15)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=0)
          ...
      protected 'original' => 
        array (size=0)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      public 'exists' => boolean false
      protected 'softDelete' => boolean false
  protected 'eagerLoad' => 
    array (size=0)
      empty
  protected 'passthru' => 
    array (size=11)
      0 => string 'toSql' (length=5)
      1 => string 'lists' (length=5)
      2 => string 'insert' (length=6)
      3 => string 'insertGetId' (length=11)
      4 => string 'pluck' (length=5)
      5 => string 'count' (length=5)
      6 => string 'min' (length=3)
      7 => string 'max' (length=3)
      8 => string 'avg' (length=3)
      9 => string 'sum' (length=3)
      10 => string 'exists' (length=6)
  protected 'parent' => 
    object(App\models\menu)[22]
  protected 'table' => string 'generator_menus' (length=15)
  protected 'connection' => null
  protected 'primaryKey' => string 'id' (length=2)
  protected 'perPage' => int 15
  public 'incrementing' => boolean true
  public 'timestamps' => boolean true
  protected 'attributes' => 
    array (size=9)
      'ID' => int 1
      'menu_rel_id' => null
      'menu_level' => int 1
      'menu_name' => string 'Main navigation' (length=15)
      'menu_system_name' => string 'main_navigation' (length=15)
      'menu_prefix' => null
      'menu_family' => string 'main_navigation' (length=15)
      'has_sub_level' => string '1' (length=1)
      'display_file' => string 'main_navigation_page' (length=20)
  protected 'original' => 
    array (size=9)
      'ID' => int 1
      'menu_rel_id' => null
      'menu_level' => int 1
      'menu_name' => string 'Main navigation' (length=15)
      'menu_system_name' => string 'main_navigation' (length=15)
      'menu_prefix' => null
      'menu_family' => string 'main_navigation' (length=15)
      'has_sub_level' => string '1' (length=1)
      'display_file' => string 'main_navigation_page' (length=20)
  protected 'relations' => 
    array (size=0)
      empty
  protected 'hidden' => 
    array (size=0)
      empty
  protected 'visible' => 
    array (size=0)
      empty
  protected 'appends' => 
    array (size=0)
      empty
  protected 'fillable' => 
    array (size=0)
      empty
  protected 'guarded' => 
    array (size=1)
      0 => string '*' (length=1)
  protected 'dates' => 
    array (size=0)
      empty
  protected 'touches' => 
    array (size=0)
      empty
  protected 'with' => 
    array (size=0)
      empty
  public 'exists' => boolean true
  protected 'softDelete' => boolean false
  protected 'related' => 
    object(App\models\page)[13]
  public 'table' => string 'generator_pages' (length=15)
  protected 'connection' => null
  protected 'primaryKey' => string 'id' (length=2)
  protected 'perPage' => int 15
  public 'incrementing' => boolean true
  public 'timestamps' => boolean true
  protected 'attributes' => 
    array (size=0)
      empty
  protected 'original' => 
    array (size=0)
      empty
  protected 'relations' => 
    array (size=0)
      empty
  protected 'hidden' => 
    array (size=0)
      empty
  protected 'visible' => 
    array (size=0)
      empty
  protected 'appends' => 
    array (size=0)
      empty
  protected 'fillable' => 
    array (size=0)
      empty
  protected 'guarded' => 
    array (size=1)
      0 => string '*' (length=1)
  protected 'dates' => 
    array (size=0)
      empty
  protected 'touches' => 
    array (size=0)
      empty
  protected 'with' => 
    array (size=0)
      empty
  public 'exists' => boolean false
  protected 'softDelete' => boolean false

What am I missing here?

Regards, Klemme

0

If I do this:

var_dump( menu::find(1)->take(1)->toSql() );

The produced SQL is:

string 'select * from `generator_menus` limit 1' (length=39)

Shouldnt I get a where clause in this query too? ::find(1)

Im using Eloquent outside Laravel, for info.

0

I tried your code on my models, but I didn't get the where clause either. The toSql() method isn't available on just the model::find(x)

The records were found and limited though - so seem to work.

Hmmm looks ok, can't see an issue - but "relationships" is one aspect of Eloquent that I hate. Not intuitive and it gives me a nosebleed.

0

Hmm thanks for testing.

Its a really simple relationship, and I cant see anything wrong with it either, nor the table structure.

Any ideas on a tool/way to debug this?

The nosebleed has arrived here too ;-)

0

Not that I know of. The laravel debug interface is a pig. At least native php tells you where in your own code you've gone wrong, but references to some core file at line 10000 is absolutely no help.

I would imagine that to retrieve your data it would be like this...

CONTROLLER (e.g. MenuController)

public function someMethod($id)
{
    $menuArray = Menu::find($id);
    //check to see if there's any data before proceeding...
    $menu = $menuArray[0];

    return View::make('menus.someView')
        ->with('menu', $menu)
        ->with('title', 'Meta title text');
}

VIEW (menus/someView.blade.php)

@foreach($menu->pages as $value)
{
    <p>{{ $value->id }}</p>
}
@endforeach

The "pages" should be added automatically as you know without any need to pass them with a 'with'.

Edited by diafol

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.