Answer the question
In order to leave comments, you need to log in
How to get the value of a related table correctly in laravel?
Happy holidays everyone!
I'm asking for help in getting the value of a field in Laravel.
First migration:
Schema::create('addresses', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->text('address')->nullable(true);
});
Schema::create('organizations', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->text('name');
$table->integer('inn');
$table->integer('address');
});
Schema::table('organizations', function (Blueprint $table) {
$table->foreign('address')
->references('id')
->on('addresses')
->onDelete('cascade');
});
return organizations::with('address')->limit(10)->get() ;
return $this->hasOne(addresses::class,'id','address')->select('address','id');
[{"id":2,"created_at":"2021-01-02T10:43:44.000000Z","updated_at":"2021-01-02T10:43:44.000000Z","name":"Braun Ltd","inn":5<b>,"address":{"address":"8702 Hosea Freeway Suite 266","id":260}}</b>,{"id":3,"created_at":"2021-01-02T10:43:44.000000Z","updated_at":"2021-01-02T10:43:44.000000Z","name":"Gusikowski, Bartell and Dickens","inn":1,"address":{"address":"2685 Cronin Lakes Apt. 633","id":534}},{"id":4,"created_at":"2021-01-02T10:43:44.000000Z","updated_at":"2021-01-02T10:43:44.000000Z","name":"Crona Group","inn":2,"address":{"address":"124 Leannon Mall","id":820}},{"id":5,"created_at":"2021-01-02T10:43:44.000000Z","updated_at":"2021-01-02T10:43:44.000000Z","name":"Cormier-Durgan","inn":6,"address":{"address":"21222 Lueilwitz Light","id":202}},{"id":6,"created_at":"2021-01-02T10:43:44.000000Z","updated_at":"2021-01-02T10:43:44.000000Z","name":"Rodriguez-Shanahan","inn":0,"address":{"address":"867 Harris Extension Apt. 010","id":374}},{"id":7,"created_at":"2021-01-02T10:43:44.000000Z","updated_at":"2021-01-02T10:43:44.000000Z","name":"Shields Ltd","inn":0,"address":{"address":"78427 Myron Causeway","id":139}},{"id":8,"created_at":"2021-01-02T10:43:44.000000Z","updated_at":"2021-01-02T10:43:44.000000Z","name":"Torp Group","inn":5,"address":{"address":"8316 Frederique Roads","id":580}},{"id":9,"created_at":"2021-01-02T10:43:44.000000Z","updated_at":"2021-01-02T10:43:44.000000Z","name":"Morar, Block and Mante","inn":4,"address":{"address":"29573 Howell Club","id":444}},{"id":10,"created_at":"2021-01-02T10:43:44.000000Z","updated_at":"2021-01-02T10:43:44.000000Z","name":"Torp-Stamm","inn":0,"address":{"address":"869 Heathcote Village","id":424}},{"id":11,"created_at":"2021-01-02T10:43:44.000000Z","updated_at":"2021-01-02T10:43:44.000000Z","name":"Ankunding, Klein and Volkman","inn":2,"address":{"address":"72871 Yundt Pine","id":420}}]
return $this->hasOne(addresses::class,'id','address')->select('address','id');
Answer the question
In order to leave comments, you need to log in
Why is the class name in lower case? addresses::class, ?
Anyway.
Ok, you set a method that specifies a connection, called it address, the code, as I understand it, is this:
public function address()
{
/* select в целом хорошо, чтобы меньше данных брать и гонять туда сюда, но если нужно больше, то можно убрать. Без id не будет работать тк не сможешь установить связь */
return $this->hasOne(addresses::class,'id','address')->select('address','id');
}
public function getAddressString()
{
//optional - чтобы вернуть null если address вернёт null и не упасть в ошибку
return optional($this->address)->address;
}
$organization->address_string;
which, if it can, will return the address string. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question