andkozinskiJEE
andkozinskiJEE

October ver.2.1.15 When building a query using LeftJoin the model id is replaced with the id with the appended table:

    public function scopeListFrontEnd($query, $options = []) {     
    extract(array_merge([
        'page' => 1,
        'perPage' => 3,
        'sort' => 'created_at',
        'typogloszenia' => null,
        'typnieruchomosci' => null,
        'typwlasciwosc' => null,
        'adres' => null,
        'powierzchnia' => null,
        'cena' => null
                    ], $options));

    if ($adres !== null) {
        $query->leftJoin('jakidom_ogloszenie_adres', 'jakidom_ogloszenie_.id', '=', 'jakidom_ogloszenie_adres.ogloszenie_id')
           ->orwhere(function ($q)use ($adres) {
                     //----------code
        }
    }
// without leftJoin

    if ($typogloszenia !== null) {
        $query->where('typogloszenia', $typogloszenia);
    }

The result is that the model id is replaced with the id of the address table. query without leftjoin returns valid the model id.

manual sql query: https://bobkarotkaoy.nazwa.pl/october/plugins/jakidom/ogloszenie/assets/Screenshot%20from%202021-10-16%2002-39-49.png

    0 => Jakidom\Ogloszenie\Models\Ogloszenie {#2739 ▼  <----Model "Ogloszenie"
    +table: "jakidom_ogloszenie_"
    +jsonable: array:3 [▶]

    +attributes: array:32 [▼
      "id" => 252                                       <----ID Models Address
      "typnieruchomosci_id" => 2
      "user_id" => 1
      "tytul" => "dom sdsdasdasdadasd"
      "promocja" => null
      "cena" => "897.00"
      "powierzchnia" => 12
      "typ_ogloszenia" => 0
      "opis" => "<p>sggfdgfg</p>"
      "typ_ogloszeniodawcy" => 0
      "typogloszenia" => "Sprzedam"
      "slug" => "dom-sdsdasdasdadasd"
      "imieNazwisko" => "asafdas"
      "email" => "test@test.com"
      "phone" => "343434"
      "odslon" => 4
      "odslonTel" => 0
      "pokoje" => 0
      "date_active" => null
      "created_at" => "2021-10-15 21:55:56"
      "updated_at" => "2021-10-15 23:38:42"
      "ogloszenie_id" => 259                                              <----  should be this ID              
      "address" => "Pilotów 35, 93-469 Łódź, Poland"
      "city" => "Łódź"
      "zip" => "93-469"
      "street" => "Pilotów"
      "number" => "35"
      "state_code" => "Łódzkie"
      "dzielnica" => "Polesie"
      "latitude" => 51.727559
      "longitude" => 19.3685861 0 => Jakidom\Ogloszenie\Models\Ogloszenie {#2739 ▼
          ]

the rest of the variables are correct

andkozinskiJEE
andkozinskiJEE

To solve the problem I changed the name in the table of the main key from id to id_address. But I think this is a bug and it shouldn't be like that.

daftspunk
daftspunk

When you perform a join, you risk overwriting the local attributes of the model. You can set the priority of the attributes by supplying a select statement.

$query->select(['table1.*','table2.*']);

In this case, the table2.id will appear as the id column since it is final layer in the select statement. Renaming the column also works.

I hope this helps!

andkozinskiJEE
andkozinskiJEE

Yes thank you for your reply it helped.

1-4 of 4