BRING VALUE FROM A REFERENCED TABLE FROM ANOTHER IN LARAVEL

0

Good afternoon for everyone,

I have a view where it shows me the values of my table orders, this table is referenced with another patient call, and the latter with a table called cities. the problem that I have is that the cities field does not show it to me with the city but with the id of the city.

for example for the fields: name, company, position, city ...

shows me ...

juan | Google | IT | 1

and I would like you to show me ...

juan | Google | TI | Bogota

Thanks in advance.

Order Control

<?php

namespace App\Http\Controllers;

use App\Http\Requests\CreateOrdenRequest;
use App\Http\Requests\UpdateOrdenRequest;
use App\Repositories\OrdenRepository;
use App\Http\Controllers\AppBaseController;
use Illuminate\Http\Request;
use Flash;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;
use App\Models\Ciudad;
use App\Models\Orden;
use App\Models\Paciente;

class OrdenController extends Controller
{
    /** @var  OrdenRepository */
    private $ordenRepository;

    public function __construct(OrdenRepository $ordenRepo)
    {
        $this->ordenRepository = $ordenRepo;
        $this->middleware('auth');
    }

    public function index(Request $request)
    {
        $this->ordenRepository->pushCriteria(new RequestCriteria($request));
        $ordenes = $this->ordenRepository->all();
        return view('ordenes.index')->with('ordenes', $ordenes)->with('paciente_Orden'); 
      /*return view('ordenes.index')->with('ordenes', $ordenes);**/    
    }

    public function create(){

           $ciudad = ['' => ''] + Ciudad::pluck('nombre','id')->toArray();

    return view('ordenes.create', compact('ciudad','orden'));

        if (empty($orden)) {
            Flash::error('Orden no encontrada');

            return redirect(route('ordenes.index'));
        }
    }



}

OrdenModel

<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Orden extends Model
{

    public $table = 'ordenes';

      public function paciente_Orden(){
        return $this->hasOne('App\Models\Paciente','id','id_paciente');
    }

    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    public $fillable = [
        'id',
        'id_paciente',
        'empresa',
        'cargo'    
    ];

    protected $casts = [
        'id' => 'integer',
        'id_paciente' => 'integer',
        'empresa' => 'string',
        'cargo'=> 'string'
    ];

    public static $rules = [

    ];
}

table.blade

<table class="table" id="orden-tabla">
    <thead>
        <tr>
            <th>Nombre</th>
            <th>Empresa</th>
            <th>Cargo</th>
            <th>Ciudad</th>
        </tr>
    </thead>
    <tbody>
    @foreach($ordenes as $orden)
        <tr>
            <td>{!! $orden->paciente_Orden->nombre !!}</td>
            <td>{!! $orden->empresa !!}</td>
            <td>{!! $orden->cargo !!}</td>
            <td>{!! $orden->paciente_Orden->id_ciudad !!}</td>

            <td>
                {!! Form::open(['route' => ['ordenes.destroy', $orden->id], 'method' => 'delete','style'=>'margin-left: 14%;']) !!}
                <div class='btn-group'>
            </div>
                {!! Form::close() !!}
            </td>
        </tr>
    @endforeach
    </tbody>
</table>
    
asked by Wilmer Sanchez 10.12.2018 в 22:33
source

2 answers

0

In case the relationships are registered in your order, patient and city models instead of:

<td>{!! $orden->paciente_Orden->id_ciudad !!}</td>

Write this:

<td>{!! $orden->paciente_Orden->ciudad !!}</td>

Or as the name of the "city" field is specified in your database.

If not, establish the relationships in the patient model, something like:

public funcion ciudad() {
    return $this->belongsTo('App\Ciudad');
}

And the inverse function for the cities model:

public function paciente() {
    return $this->hasMany('App\Paciente');
}

In doing this, you should allow yourself to see the relationship between the tables as:

{!! $orden->paciente_Orden->ciudad->ciudad !!}

Greetings.

    
answered by 11.12.2018 в 00:02
0

Thank you Jesus for your answer, you clarified several doubts I had, I solved it in the following way:

In my Patient Model

public function ciudad_paciente(){
    return $this->hasOne('App\Models\Ciudad','id','id_ciudad');
}
   public function ingreso_paciente(){
        return $this->hasOne('App\Models\Ingreso','id_paciente','id');
    }

and on my table.blade

 <td>{!! $orden->paciente_Orden->ciudad_paciente->nombre !!}</td>
    
answered by 11.12.2018 в 15:08