When I fill in a new record, instead of getting the name in question from the foreign record, its primary key comes out, I'm supposed to translate it with the relations.
MovementsController.php
public function index()
{
$movimientos = Movimiento::orderBy('codigo', 'ASC')->paginate(15);
$movimientos->each(function($movimientos){
$movimientos->producto;
$movimientos->proyecto;
$movimientos->ubicacion;
});
return view('admin.movimientos.index')->with('movimientos', $movimientos);
}
Movement.php
class Movimiento extends Model
{
public $primaryKey = 'codigo';
protected $table = "movimientos";
protected $fillable = ['fecha','hora','tipo','codigo_producto','cantidad','albaran','acronimo','ubicacion','codigo_ticket','puo','codigo_proyecto'];
public function producto()
{
return $this->belongsTo('App\Producto');
}
public function proyecto()
{
return $this->belongsTo('App\Proyecto');
}
public function ubicacion()
{
return $this->belongsTo('App\Ubicacion');
}
}
Product.php
class Producto extends Model
{
public $primaryKey = 'codigo';
protected $table = "productos";
protected $fillable = ['nombre','nombre_corto','codigo_familia','unidad','fungible','codigo_proveedor','stock_minimo','codigo_ubicacion_default'];
public function familia()
{
return $this->hasMany('App\Familia');
}
public function proveedor()
{
return $this->hasMany('App\Proveedor');
}
public function ubicacion()
{
return $this->hasMany('App\Ubicacion');
}
public function movimiento()
{
return $this->hasMany('App\Movimiento');
}
public function puo()
{
return $this->belongsTo('App\PUO');
}
public function proyectos()
{
return $this->belongsToMany('App\Proyecto');
}
}
Location.php
class Ubicacion extends Model
{
protected $table = "ubicaciones";
public $primaryKey = 'codigo';
protected $fillable = ['acronimo','DCM_Store'];
public function producto()
{
return $this->hasMany('App\Producto');
}
public function movimiento()
{
return $this->hasMany('App\Movimiento');
}
}
project.php
class Proyecto extends Model
{
public $primaryKey = 'id_proyecto';
protected $table = "proyectos";
protected $fillable = ['nombre'];
public function movimiento()
{
return $this->hasMany('App\Movimiento');
}
public function puo()
{
return $this->belongsTo('App\Puo');
}
public function productos()
{
return $this->belongsToMany('App\Producto');
}
}
Index.blade.php (Move)
@extends('admin.template.main')
@section('title', 'Listado de Movimientos')
@section('content')
<a href="{{ route('movimientos.create') }}" class="btn btn-info">Registrar nuevo Movimiento</a><hr>
<table class="table table-bordered">
<thead>
<tr>
<th>Código</th>
<th>Fecha</th>
<th>Hora</th>
<th>Tipo</th>
<th>Código de Producto</th>
<th>Cantidad</th>
<th>Albarán</th>
<th>Acrónimo</th>
<th>Ubicación</th>
<th>Código del Ticket</th>
<th>PUO</th>
<th>Código del Proyecto</th>
</tr>
</thead>
<tbody>
@foreach($movimientos as $movimiento)
<tr>
<td>{{ $movimiento->codigo }}</td>
<td>{{ $movimiento->fecha }}</td>
<td>{{ $movimiento->hora }}</td>
<td>{{ $movimiento->tipo }}</td>
<td>{{ $movimiento->codigo_producto->nombre }}</td>
<td>{{ $movimiento->cantidad }}</td>
<td>{{ $movimiento->albaran }}</td>
<td>{{ $movimiento->acronimo }}</td>
<td>{{ $movimiento->ubicacion->DCM_Store }}</td>
<td>{{ $movimiento->codigo_ticket }}</td>
<td>{{ $movimiento->puo }}</td>
<td>{{ $movimiento->proyecto->nombre }}</td>
<td>
<a href="{{ route('movimientos.edit', $movimiento->codigo) }}" class="btn btn-warning"> <span class="glyphicon glyphicon-wrench" aria-hidden="true"></span></a>
<a href="{{ route('movimientos.destroy', $movimiento->codigo) }}" class="btn btn-danger"><span class="glyphicon glyphicon-remove-circle" aria-hidden="true" onclick="return confirm('¿Seguro que quieres borrarlo')"></span></a>
</td>
</tr>
@endforeach
</tbody>
</table>
{!! $movimientos->render() !!}
@endsection
And it shows me this error:
Trying to get property of non-object (View: /var/www/html/almacen/resources/views/admin/movimientos/index.blade.php)
I am supposed to be calling the functions that contain the relations so that they can be related to each other, but of course I always get this error.
I add my attempt withwith()
public function index()
{
$movimientos = Movimiento::orderBy('codigo', 'ASC')->paginate(15);
$movimiento = Movimiento::with('proyecto')->get();
return view('admin.movimientos.index')->with('movimientos', $movimientos);
}
<td>{{ $movimiento->proyecto->nombre }}</td>
Trying to get property of non-object (View: /var/www/html/almacen/resources/views/admin/movimientos/index.blade.php)
Ok so in conclusion:
If your parent model doesn't use "id" as the primary key, or you want to bind the child model to a different column, you can pass a third argument to the belongsTo method that specifies the custom key of your parent table:
Also, try not to specify names that might interfere between methods, keys, or tables.
I'm glad you got it fixed.
In the trace you will get the specific line that fails you, but basically it says that a Model (object) has a null value (not initialized) and you are trying to access one of its attributes.
Example 1:
If movement is null, when accessing "code" it will give you the error
Example 2:
Assuming that movement is not null, but that the "product_code" is, it will give the error because it cannot access "name"