How to change the laravel relationship keyword

1

I have the following problem, I logged into a project where the database already existed and is in production, so all the foreign keys of this database are under the nomenclature fk_ and Eloquent of Laravel , related the% of% with the tables from the keyword models , is it possible to change within the settings of _id the search of Laravel to _id ?

    
asked by anonymous 05.03.2018 / 18:06

1 answer

2

The is very flexible in this regard, as the bank of data already existed and consequently did not follow the same nomenclature of Laravel , there are ways to pass the names of the keys in the configurations, examples:

1: 1 relationship

A person has an address and an address belongs to a person:

class Peoples extends Model
{   
    //Relacionamento.
    public function address()
    {
        //     $this->hasOne(relacao, chave estrangeira, primary key);
        return $this->hasOne('App\Address', 'peopleid', 'id');
    }
}

class Address extends Model
{    

    //Relacionamento.
    public function people()
    {
        //     $this->belongsTo(relação, chave estrangeira local, primary key da relação);
        return $this->belongsTo('App\Peoples', 'peopleid', 'id');
    }
}

Relationship 1: N

A person can have one or more phones:

class Peoples extends Model
{   
    //Relacionamento
    public function phones()
    {
        //     $this->hasMany(relação, chave estrangeira da relação, primary key local);
        return $this->hasMany('App\Phones', 'peopleid', 'id');
    }

}

class Phones extends Model
{    
    //Relacionamento
    public function people()
    {
        //     $this->belongsTo(relação, chave estrangeira local, primary key da relação);
        return $this->belongsTo('App\Peoples', 'peopleid', 'id');
    }
}

Relationship N: M

An author can have several books and a book can have multiple authors, where this relationship generates a middle table.

class Authors extends Model
{    

    //Relacionamento.
    public function books()
    {
        //    $this->belongsToMany('relacao', 
                                   'nome da tabela pivot', 
                                   'key ref. authors em pivot',  
                                   'key ref. books em pivot')
        return $this->belongsToMany('App\Books',
                                    'booksauthors', 
                                    'authorid', 
                                    'bookid');
    }
}

class Books extends Model
{    

    //Relacionamento.
    public function authors()
    {
        //    $this->belongsToMany('relacao', 
                                   'nome da tabela pivot', 
                                   'key ref. books em pivot', 
                                   'key ref. author em pivot')
        return $this->belongsToMany('App\Authors', 
                                    'booksauthors', 
                                    'bookid', 
                                    'authorid');
    }
}

These are the more traditional relationships ( perhaps suffering some variation for some bank model ) and can help you tailor your old bank, but in addition to these three there are some other types described in the documentation that can be observed as well.

I've also written some answers that you can use as a basis:

References

05.03.2018 / 21:58