related tables and change data in laravel view

-1

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 .

    
asked by Juan Pablo Sánchezb 06.10.2017 в 20:41
source

1 answer

0

Your models do not agree with your explanation or with your migrations, so I see you are posing the relationships upside down, because according to the migration, it is MatMayor who has a company and you explain it backwards.

You should have something like that in the Matmayor model then according to your migration:

public function Comp()
{
    return $this->belongsTo('App\Compania', 'compañia');
}

In the Compania model:

public function matmayor()
{
   return $this->belongsTo('App\Matmayor', 'compañia');
}

To get the information you want in the controller and then pass it to the view, you would make a query like this, assuming that you want to start from the machine model to get the name of the company:

$maquina = Matmayor::with('Comp')->get();

What we are doing is using Eager Loading to load the relationship.

and in the view you would have something like this:

Nombre compañía: {{ $maquina->Comp->nombre }}

I suggest you review in much more detail the Laravel documentation and also the design of your database.

    
answered by 08.10.2017 в 23:45