Reputation: 3
Need help, how do I display the name of the driver instead of the id? I have more tables but if only someone can help me with one i'll be more that glad, the table show me the number of the ID 1, 2, 3... i have tried to change the model, Im learning php and lavarel an still this part do not get clear for me. Thx for the help
register model;
class register extends Model
{
use SoftDeletes;
use HasFactory;
public $table = 'registers';
protected $dates = ['deleted_at'];
public $fillable = [
'driver_id',
'inOut',
'plates',
'carrier_id',
'tractorNumber',
'process_id',
'location_id',
'customer',
'drayage_id',
'containertype_id',
'containerstatus_id',
'containerPlate',
'containerNumber',
'comments'
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'driver_id' => 'integer',
'inOut' => 'string',
'plates' => 'string',
'carrier_id' => 'integer',
'tractorNumber' => 'string',
'process_id' => 'integer',
'location_id' => 'integer',
'customer' => 'string',
'drayage_id' => 'integer',
'containertype_id' => 'integer',
'containerstatus_id' => 'integer',
'containerPlate' => 'string',
'containerNumber' => 'string',
'comments' => 'string'
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'driver_id' => 'required',
'inOut' => 'required',
'carrier_id' => 'required',
'process_id' => 'required',
'location_id' => 'required',
'drayage_id' => 'required',
'containertype_id' => 'required',
'containerstatus_id' => 'required'
];
}
driver model
class driver extends Model
{
use SoftDeletes;
use HasFactory;
public $table = 'drivers';
protected $dates = ['deleted_at'];
public $fillable = [
'driverName',
'licence'
];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'driverName' => 'string',
'licence' => 'string'
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'driverName' => 'required',
'licence' => 'required'
];
}
@foreach($registers as $register)
<tr>
<td>{{ $register->driver_id }}</td>
<td>{{ $register->inOut }}</td>
<td>{{ $register->plates }}</td>
<td>{{ $register->carrier_id }}</td>
<td>{{ $register->tractorNumber }}</td>
Upvotes: 0
Views: 489
Reputation: 81
If there is a relationship between driver and register models, you can write that relationship in the register model class.
public function driver()
{
return $this->belongsTo(driver::class, 'driver_id');
}
Then you can get driver name by typing,
@foreach($registers as $register)
<tr>
<td>{{ $register->driver->driverName}}</td>
<td>{{ $register->inOut }}</td>
<td>{{ $register->plates }}</td>
<td>{{ $register->carrier_id }}</td>
<td>{{ $register->tractorNumber }}</td>
</tr>
@endforeach()
Upvotes: 2