Laravel dane powiązane

0

hej,

dlaczego nie mogę tak

$customers = Customer::BelongingToUser()->pluck('name', 'id');
$locations = $customers->location(); //Location::pluck('location_name', 'id');

dostaje
Method location does not exist.
w przypadku $customers->location property

oczywiście na Customer

    public function locations()
    {
        return $this->hasMany('App\Models\Location');
    }
0

działa natomiast coś takiego:

$c = Customer::find(1);
$l = $c->locations;
foreach ($l as $r)
    echo $r->location_name;
0

No bo location to nie to samo, co locations :-P

0

mój błąd tam u góry

$customers = Customer::BelongingToUser()->pluck('name', 'id');
$locations = $customers->locations; // Property [locations] does not exist on this collection instance. lub Method locations does not exist.
0

$customers to kolekcja, nie model.

0

Eloquent zwraca model lub kolekcję modeli.
Czy gdy uzyję "where", "pluck", etc... nie mogę tak dostawać się do pokrewnych modeli?
W takim razie jak postępować? Jak wyciągnąć lokalizacje w tym przypadku?

0
$customers = Customer::BelongingToUser()->with('location')->get(); // *

foreach ($customers as $customer) {
  dump($customer->location);
}

* - technicznie rzecz biorąc with('location') jest zbędne, lecz bez tego Twój kod będzie prezentował tzw. problem N+1.

0

w takiej formie dostaję
Call to undefined relationship [location] on model [App\Models\Customer].

a metodę mam

    public function locations()
    {
        return $this->hasMany('App\Models\Location');
    }
0

Jeez, no to with('locations') oraz $customer->locations...

0

chodzi tutaj raczej o with('location')

0

na przyszłość walnij sobie dd($customers) to byś zobaczył w czym tkwi problem

1 użytkowników online, w tym zalogowanych: 0, gości: 1