I can't access the property of the object. Laravel/Eloquent ORM
I can't access the properties of the related object. Well, I have a class called FileClass
, it has the following relationship with the class FileServico
:
FileClass.php
public function fileServico(){
return $this->hasMany('FileServico','id_file','id_file');
}
Fileservice.php
public function file(){
return $this->belongsTo('FileClass','id_file','id_file');
}
In my controller I retrieve the values as follows:
ReservaController.php
public function getIndex(){
$fileClass = FileClass::with(['FileServico'])->get();
return View::make('home')->with('fileClass',$fileClass);
}
However, in the view I cannot access the objects of the relationship, below I list 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()
in $fileClass
which is the object Q returns to view it does not view the related object, but if I give a print_r()
in an object iterated with foreach
and try to access its separate property, it normally accesses $f->fileServico
.
3 answers
FileClass
has a list of FileServico
($f->fileServico
) in this case it cannot be accessed without a for
or a foreach
. If it were contrary to FileServico
for FileClass ai would work out what you report in the question. Below is an example for lightening.
Suggesting 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
Given 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 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
Object returned if I go to $f - >fileServico, 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
)
)
)
Access to protected property was not possible, so I had to make some modifications to be able to run. Well:
In the controller I made the inclusion of the toArray method, so it returns everything to me as 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
Loses some of the elegance by not accessing as an object, but I got what I wanted, anyone who has another more practical means, would appreciate if I could share.