简体   繁体   中英

Laravel hasOne Huge Array

I cant seem to pull out the cat name from another model.

My array looks fine when I get all the items as below.

  $testing = Scraper::get();
  print_r($testing);

   [items:protected] => Array
        (
            [0] => Scraper Object
                (
                    .....
                    [attributes:protected] => Array
                        (
                            [id] => 1
                            [cat_id] => 1
                            [created_at] => 2014-08-18 09:08:54
                            [updated_at] => 2014-08-18 09:08:54
                      .....
                        )

When I try the code bellow as a print_r I get a huge array of nothing related to pulling the item with it's categories name?

Scraper::first()->categorie();

Illuminate\Database\Eloquent\Relations\HasOne Object
(
    [foreignKey:protected] => categories.scraper_id
    [localKey:protected] => id
    [query:protected] => Illuminate\Database\Eloquent\Builder Object
        (
            [query:protected] => Illuminate\Database\Query\Builder Object
                ( 
                 .....

Have I done something wrong, my models are below.

Cat.php

class Cat extends Eloquent {

    protected $table = 'categories';

}

Scraper.php

class Scraper extends Eloquent {

    protected $table = 'scraper';

    public function categorie() {
        return $this->hasOne('Cat');
    }

}

Databases

categories -  | id | name

scraper - |id | cat_id | updated_at | created_at

Many Thanks,

You need to call:

Scraper::first()->categorie;

instead of:

Scraper::first()->categorie();

Explanation: http://laravel.com/docs/eloquent#dynamic-properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM