B
B
Burzui2021-01-04 13:04:00
Laravel
Burzui, 2021-01-04 13:04:00

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);
        });


Second migration:
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');
        });


Controller:
return organizations::with('address')->limit(10)->get() ;


Data acquisition model:
return $this->hasOne(addresses::class,'id','address')->select('address','id');


As a result, I get the answer:
[{"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}}]


And I get an object in the address, but I need the value to be just "address": "72871 Yundt Pine".

If in a request:
return $this->hasOne(addresses::class,'id','address')->select('address','id');

I remove id from select, then the value is returned NULL.

I can not understand how to do it correctly and optimally.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Talalaev, 2021-01-04
@neuotq

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');
   }

Next, you can add a calculated attribute, let's call it for example
public function getAddressString()
    {
       //optional - чтобы вернуть null если  address вернёт null и не упасть в ошибку
   return optional($this->address)->address;
    }

Well, further, in the code you can use $organization->address_string;which, if it can, will return the address string.
Read more here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question