laravel Eloquent: Relationships error to print the data in the view

-1

I'm making a query with elounque and I'm using the with () here is the code, the idea is that the field id_estado is a key forania and and that key attract more data, I make the query the problems is when I print it out error

$Solicitude = SolicitudeModel::with('id_estado')->get();
return view('Solicitude.index', [ 
"id_tipo" => $id_tipo,
"id_estado" => $id_estado,
"usuarios_id" => $usuarios_id, 
'listmysql' => $Solicitude
]);

in the model is like this:

<?php
    namespace App;
    use Illuminate\Database\Eloquent\Model;
    class SolicitudeModel extends Model
    {
        protected $table = 'solicitudes';
        protected $fillable = [
        ];
        public function id_estado()
        {
            return $this->belongsTo('App\Solicitude_estado','id_estado');   
        }
    }

and in the view I print

the query data

  {
"id":1,
"descripcion":"contando de cable de energiaw",
"id_tipo":2,
"id_estado":{"id":2,"descripcion":"en procesor","created_at":"2017-11-08 23:26:09","updated_at":"2017-10-29 07:19:04"},
"created_at":"2018-03-08 21:53:12",
"updated_at":"2018-03-09 02:53:12",
"usuarios_id":1}

{
"id":2,
"descripcion":"arbol de control 2",
"id_tipo":2,
"id_estado":{"id":1,"descripcion":"pendiente","created_at":"2017-11-08 23:26:09","updated_at":"2017-10-29 07:19:04"},
"created_at":"2018-03-06 10:30:00",
"updated_at":"2017-11-09 05:34:10",
"usuarios_id":1} 

and in the view to print the data I have it in the following way

  @foreach($listmysql as $lists)
                                    <?= $lists ?>
            <td class="col1">{{ $lists->id }}</td>
            <td class="col1">{{ $lists->descripcion }}</td>
            <td class="col1">{{ $lists->id_tipo }}</td>
            //linea donde sale el error 
            <td class="col1">{{$lists->id_estado->id }}</td>
   @endforeach
    
asked by marcos alberto saavedra sanabr 17.03.2018 в 17:30
source

1 answer

-1

I was able to solve the problem with the with, so I am going to publish my answer, the conclusion when we declare a With () has to declare it with another name that is not the same field of forey key

in the controller we made the query

    $Solicitude = SolicitudeModel::with('id_estado_pk')->get();
    return view('Solicitude.index', 
      [ 
        "id_tipo" => $id_tipo,
        "id_estado" => $id_estado,
        "usuarios_id" => $usuarios_id, 
        'listmysql' => $Solicitude
      ]);

in the model RequestModel we place the following line

 funcion public function id_estado_pk()
    {
        //archivo de modelo y el campo foreign_key 
        return $this->belongsTo('App\Solicitude_estado','id_estado');   
    } 

and in the view returns the following query

{
"id":1,
"descripcion":"contando de cable de energiaw",
"id_tipo":2,
"id_estado":2,
"created_at":"2018-03-08 21:53:12",
"updated_at":"2018-03-09 02:53:12",
"usuarios_id":1,"
id_estado_pk":
       {
       "id":2,
       "descripcion":"en procesor",
       "created_at":"2017-11-08 23:26:09",
       "updated_at":"2017-10-29 07:19:04"
       }
}
{
"id":2,
"descripcion":"arbol de control 2",
"id_tipo":2,
"id_estado":1,
"created_at":"2018-03-06 10:30:00",
"updated_at":"2017-11-09 05:34:10",
"usuarios_id":1,
"id_estado_pk":
         {
         "id":1,
         "descripcion":"pendiente",
         "created_at":"2017-11-08 23:26:09",
         "updated_at":"2017-10-29 07:19:04"
         }
} 
{
"id":7,
"descripcion":"tinoeesddeeewwww",
"id_tipo":2,
"id_estado":2,
"created_at":"2018-03-06 10:30:00",
"updated_at":"2017-11-09 05:34:10",
"usuarios_id":1,
"id_estado_pk":
         {
        "id":2,
        "descripcion":"en procesor",
        "created_at":"2017-11-08 23:26:09",
        "updated_at":"2017-10-29 07:19:04"
         }
} 

and in the view it would be like that

@foreach($listmysql as $lists)
                    <td class="col1">{{ $lists->descripcion }}</td>
                    <td class="col1">{{ $lists->id_tipo }}</td>
                    <td class="col1">
                    {{$lists->id_estado }}

                    {{$lists->id_estado_pk->descripcion }}          
                    </td>


                    <td class="col1">{{ $lists->created_at }}</td>
                    <td class="col1">{{ $lists->updated_at }}</td>
                    <td class="col1">{{ $lists->usuarios_id }}</td>
            @endforeach
    
answered by 17.03.2018 в 20:47