0
I am trying to use relationships to show data on screen but I get the following error:
Trying to get Property 'name' of non-object
In this command
$recebimento->planoconta()->get()->first()->nome
MODEL PLAN COUNTS
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Planoconta extends Model
{
//
protected $table = 'plano_contas';
protected $fillable = [
'tipo', 'nome',
];
public function pagamento():hasMany{
return $this->hasMany('App\Pagamento', 'plano_contas_id');
}
public function recebimento():hasMany{
return $this->hasMany('App\Recebimento', 'plano_contas_id');
}
}
MODEL RECEIPTS
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Cliente;
use Planoconta;
class Recebimento extends Model
{
//
protected $table = 'recebimentos';
protected $fillable = [
'data_emissao','data_venci','documento','parcela', 'valor', 'cliente_id','plano_contas_id', 'descricao',
];
public function cliente():BelongsTo{
return $this->BelongsTo('App\Cliente');
}
public function planoconta():BelongsTo{
return $this->BelongsTo('App\Planoconta');
}
}
HTML
@foreach($recebimentos as $recebimento)
<tr class="itemRecebimento{{$recebimento->id}}">
<td>{{$recebimento->id}}</td>
<td>{{$recebimento->data_receb}}</td>
<td>{{$recebimento->valor}}</td>
<td>{{$recebimento->cliente()->get()->first()->nome}}</td>
<td>{{$recebimento->planoconta()->get()->first()->nome}}</td>
<td><button class="btnOpenUpdateRecebimento btn btn-info" data-id="{{$recebimento->id}}" data-data_receb="{{$recebimento->data_receb}}" data-valor="{{$recebimento->valor}}" data-cliente_nome="{{$recebimento->cliente()->get()->first()->nome}}" data-cliente_id="{{$recebimento->cliente()->get()->first()->id}}" data-plano_contas="{{$recebimento->plano_contas}}" > <span class="fa fa-pencil"></span></button>
<button class="delete-recebimento btn btn-danger" data-id="{{$recebimento->id}}" data-data_receb="{{$recebimento->data_receb}}" data-valor="{{$recebimento->valor}}" data-cliente_nome="{{$recebimento->cliente()->get()->first()->nome}}" data-cliente_id="{{$recebimento->cliente()->get()->first()->id}}" ><span class="fa fa-trash"></span></button>
</td>
</tr>
@endforeach
CUSTOMER MODEL THAT IS WORKING AND DID EXACTLY THE SAME
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\hasMany;
use Recebimento;
class Cliente extends Model
{
////
protected $table = 'clientes';
protected $fillable = [
'nome', 'cpf', 'endereco', 'numero', 'cep', 'complemento', 'bairro', 'celular', 'telefone', 'cidade', 'estado', 'email',
];
public function recebimento():hasMany{
return $this->hasMany('App\Recebimento', 'cliente_id');
}
}
So far you must be returning
null
->$recebimento->planoconta()->get()->first()
!!! look for the return before accessing the property is this problem.– novic
@Virgilionovic How should I proceed? In the bank the relationship is working, all receipts are registered with account plan, there is no reason to return null
– Gabriel Alves
if no result returns null
– novic
gives a
var_dump($recebimento->planoconta()->get())
is checks what returns– novic
@Virgilionovic really, is returning NULL, but I don’t understand why!!
– Gabriel Alves
Take a look at this answer and follow the instructions: missing configuration because you must not have done it in the correct convention: then it has to be configured item by item: https://answall.com/questions/173966/howto use-related-hasmany-no-laravel-5-2/173976#173976 , but as it was said if no data returns null even
– novic
Another post that explains item by item: https://answall.com/questions/152089/problemas-related-um-para-muitos-laravel/152108#152108
– novic
@Virgilionovic followed these steps N:1 $this->belongsTo(relation, local foreign key, relation key Primary); 1:N $this->hasMany(relation, foreign relation key, local key Primary); public Function planoconta():Belongsto{ Return $this->Belongsto('App Planoconta', 'id', 'id'); } public Function receiving():hasMany{ Return $this->hasMany('App Receipt', 'plano_contas_id', 'id'); } and now it’s worked, thank you very much!
– Gabriel Alves