I can not access the property of the object. Laravel / Eloquent ORM

4

I can not access the properties of the related object. Well, I have a class named FileClass , it has the following relationship with class FileServico :

FileClass.php

public function fileServico(){
    return $this->hasMany('FileServico','id_file','id_file');
}

FileServico.php

public function file(){
    return $this->belongsTo('FileClass','id_file','id_file');
}

In my controller I retrieve the values as follows:

ReservationController.php

public function getIndex(){
    $fileClass = FileClass::with(['FileServico'])->get();
    return View::make('home')->with('fileClass',$fileClass);
}

However, in the view I can not access the objects of the relationship, here are some ways to access that I tried:

@foreach($fileClass as $f)
    $f->id_servico; //Tentei assim
    $f->file_servico->id_servico //Tentei assim
    $f->fileServico->id_servico; //Assim também
    $f->id_servico //Assim também
    $f->fileServico['id_servico'] //Como array também
    $f->fileServico[0]['id_servico'] //Assim...
@endforeach

Understanding better, a file can have several file_servico , if I simply give a print_r() to $fileClass that is the object that returns to view it does not see the related object, however if I give a print_r() on an object iterated with foreach and trying to access its separate property, it usually accesses $f->fileServico .

    
asked by anonymous 05.06.2014 / 18:59

3 answers

3

FileClass has a List of FileServico ( $f->fileServico ) in case it can not be accessed without a for or a foreach . If it were unlike FileServico for FileClass then it would work out what you report on the issue. Here's an example to lighten up.

Suggest a minimal example:

//Tabela fileclass
CREATE TABLE 'fileclass' (
  'id_file' int(11) NOT NULL AUTO_INCREMENT,
  'desc' varchar(45) DEFAULT NULL,
  PRIMARY KEY ('id_file'),
  UNIQUE KEY 'id_file_UNIQUE' ('id_file')
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
//Tabela fileservico
CREATE TABLE 'fileservico' (
  'id_servico' int(11) NOT NULL AUTO_INCREMENT,
  'id_file' int(11) DEFAULT NULL,
  'desc' varchar(45) DEFAULT NULL,
  PRIMARY KEY ('id_servico'),
  KEY 'id_file_key_idx' ('id_file'),
  CONSTRAINT 'id_file_key' FOREIGN KEY ('id_file') 
  REFERENCES 'fileclass' ('id_file') 
  ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

Data Examples

Models

class FileClass extends Eloquent 
{
    public $table      = 'fileclass'; 
    public $primaryKey = 'id_file';
    public $timestamps = false;
    public function fileServico(){
        return $this->hasMany('FileServico','id_file','id_file');
    }
}
class FileServico extends Eloquent {
    public $table      = 'fileservico'; 
    public $primaryKey = 'id_servico';
    public $timestamps = false;
    public function file(){
        return $this->belongsTo('FileClass','id_file','id_file');
    }
}

Eloquent accessing relationship with

$files = FileClass::with('FileServico')->get();
foreach ($files as $file) {
    echo $file->id_file.' '.$file->desc;
    echo '<br>';
    $servicos = $file->FileServico;         
    foreach ($servicos as $servico) 
    {
        echo $servico->id_servico.' '.$servico->desc;
        echo '<br>';
    }
    echo '<br>';
    echo '<br>';
}

Debug:

select * from 'fileclass'
Array ( [0] => 1 [1] => 2 )
select * from 'fileservico' where 'fileservico'.'id_file' in (?, ?)

//id_file = 1
1 File 1
   1 Servico1
   2 Servico2

//id_file = 2
2 File 2
  3 Servico3
    
05.07.2014 / 22:51
1

Object returned if I access $ f-> gservice, which is the relationship.

Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
    (
        [0] => FileServico Object
            (
                [table:protected] => file_servico
                [fillable:protected] => Array
                    (
                        [0] => id_servico
                    )

                [rules:protected] => Array
                    (
                    )

                [primaryKey:protected] => id_file
                [connection:protected] => 
                [perPage:protected] => 15
                [incrementing] => 1
                [timestamps] => 1
                [attributes:protected] => Array
                    (
                        [id_servico] => 4
                        //Aqui ficam os campos da tabela...
                    )

                [original:protected] => Array
                    (
                        [id_servico] => 4
                        //Aqui ficam os campos da tabela...
                    )

                [relations:protected] => Array
                    (
                    )

                [hidden:protected] => Array
                    (
                    )

                [visible:protected] => Array
                    (
                    )

                [appends:protected] => Array
                    (
                    )

                [guarded:protected] => Array
                    (
                        [0] => *
                    )

                [dates:protected] => Array
                    (
                    )

                [touches:protected] => Array
                    (
                    )

                [observables:protected] => Array
                    (
                    )

                [with:protected] => Array
                    (
                    )

                [morphClass:protected] => 
                [exists] => 1
            )

    )

)

    
05.06.2014 / 19:09
1

Access to protected property was not possible, so I had to make some modifications to get it running. Well:

In the controller I added the toArray method, so it returns everything as an array:

$fileClass = FileClass::with(['FileServico'])->get()->toArray();

Next, I retrieved in the view the values iterating with two foreach, see:

@foreach($fileClass as $file)
    @foreach($file['file_servico'] as $f)
        <tr>
            <td>{{ $f['id_servico'] }}</td>
        </tr>
    @endforeach
@endforeach

It loses a little elegance because it does not access as an object, but I got what I wanted, if you have another more practical way, I'd appreciate it if you could share it.

    
05.06.2014 / 20:52