I have three tables:
Schema::create('comp', function (Blueprint $table) {
$table->increments('id');
$table->string('nombre');
$table->string('direccion');
$table->string('n_com');
$table->string('comuna');
$table->timestamps();
});
public function up()
{
Schema::create('tipo', function (Blueprint $table) {
$table->increments('id');
$table->string('nombre');
$table->string('descripcion');
$table->string('codigo');
$table->timestamps();
});
}
Schema::create('maquinas', function (Blueprint $table) {
$table->increments('id');
$table->string('codigo');
$table->foreign('codigo')->references('codigo')->on('tipo');
$table->string('marca');
$table->string('modelo');
$table->string('chasis');
$table->date('ano_fab');
$table->integer('compañia')->unsigned();
$table->foreign('compañia')->references('id')->on('comp');
$table->timestamps();
<?php
Matmayor Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Matmayor extends Model
{
protected $table = 'mat_mayor';
public function Comp () {
return $this->hasOne('App\Compania','n_com'); // Le indicamos que se va relacionar con el atributo id
}
public function tipo () {
return $this->belongsTo('App\Tipo','codigo'); // Le indicamos que se va relacionar con el atributo id
}
}
Type Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tipo extends Model
{
// asignamos la tabla
protected $table = 'tipo';
//
public function Matmayor(){
return $this->hasMany('App/Matmayor');
}
}
Company Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Compania extends Model
{
protected $table = 'comp';
public function matmayor()
{
return $this->belongsTo('App\Matmayor');
}
}
I need the company name in the view to give me the name of the company (currently in the machines table I keep only id
of company and id
of code.
in codigo_id
I need to see the code in the view (ex B) and not the id
.