How to work correctly with entities that are related in laravel?

1

I have a model called Inmueble , its attributes are:

  • Property Number
  • Location (in flame database id_ubicacion )
  • Type of Property (in flame database id_tipo_inmueble )
  • Area
  • Description
  • This model has its respective driver (it should be noted that this driver only brings the CRUD methods by default) and their respective views.

    I also have a model Ubicacion , whose attributes are:

  • Id (this is autonumerica, so it does not appear in the model)
  • Name (the name of the location)
  • Also with its respective driver and view.

    In the same way with the model, controller and view for TipoInmueble (this model has the same attributes as Location)

    The problem I have is that I do not know how to relate the models correctly, since, as I mentioned earlier, the location and the type of property are attributes of the property.

    I had read something about methods hasOne and hasMany , but I did not know how they should be used, if they are accessors or modifiers, in general, I have no idea where and how to use them, and I did not understand the documentation that laravel has .

    In addition to this, I am making the registration form of Inmueble in laravel collective, and I want to make a selection that shows me the available options in location and type of property, as that information is something that comes from the base of data, in the method that the form shows me, I am also sending this data in an array in the following way:

    public function create() {
        $ubicaciones = Ubicacion::pluck('nombre', 'id');
        $tipoInmueble= TipoInmueble::pluck('nombre', 'id');
        return view('properties.create', compact('ubicaciones'), compact('tiposInmueble'));
    }
    

    But I'm not sure if it's the right way to do it, I mean the structure I'm using, I'm programming in Java, and it's not usually done that way, so I wanted to ask if this should be done, I'm using 2 models that do not correspond to my controller (Models TipoInmueble and Ubicacion when my controller is ControladorInmueble ), I do not know if I should work like this, or use the driver of each model, or work it in another way, I would appreciate it if Could you solve my doubts?

        
    asked by Santiago Bueno 13.12.2018 в 15:39
    source

    1 answer

    1

    The use of hasOne, belongsTo, hasMany or belongsToMany depends on how the tables in your database are related.

    Assuming that a property can have only one location and only one type of property, the way to relate it would be like this:

    Model Property.php:

    class Inmueble extends Model
    {
        protected $table = 'inmueble'; //nombre de la tabla en tu base de datos
    
        //Otras configuraciones, parámetros, etc
    
    
    
        // Esta es la relación con el modelo Ubicacion. Ojo que la función se puede llamar de cualquier forma
        public function Ubicacion()
        {
            return $this->belongsTo('Ubicacion','id_ubicacion');
        }
    
        // Esta es la relación con el modelo TipoInmueble
        public function TipoInmueble()
        {
            return $this->belongsTo('TipoInmueble','id_tipo_inmueble');
        }
    }   
    

    If you notice, those two functions are added. The advantage is that after you can call it in the following way:

    $todos_los_inmuebles = Inmueble::with("Ubicacion")->with("TipoInmueble")->get();
    

    This will generate you a Collection of Objects of type Property and each one will contain two Objects: one of type Location and another of type TypeFocus.

    You can search only for a property with its relations in the following way:

    $un_inmueble = Inmueble::find($id_a_buscar)->with("Ubicacion")->with("TipoInmueble")->get();
    

    This will return you an Object of type Property that will contain two Objects: one of type Location and another of type TypeFocus. To access the Location object of that object Property we would do it this way:

    $ubicacion_del_inmueble = $un_inmueble->Ubicacion;
    

    Remember that at any time you can transform an object to an array with -> toArray ()

    NOTE: In the model you must make sure to put the namespaces. I put only the names, but if, for example, you have your Models under the namespace APP, the relationship should be:

    public function getUbicacion()
    {
         return $this->hasOne('App\Ubicacion','id_ubicacion');
    }
    

    I hope to contribute

        
    answered by 13.12.2018 / 16:28
    source