Próbuję pobrać listę gatunków, które mają przypisany do nich utwór, używam tam, gdzie wymowne kwerendy, ale czuć się trochę zagubiony.Eloquent Laravel - pobieranie wyników tylko wtedy, gdy związek istnieje
Mam następujący
//model
public function workingGenre() {
$this->whereHas('tracks', function($query) {
$query->where('');
});
return $this->tracks()->first();
}
//controller (where i am passing variables etc)
$genres = Genre::with('tracks')->get();
//my view
@foreach($genres as $genre)
{{print_r($genre->workingGenre())}}
@endforeach
Jestem świadomy, że moim Gdzie jest pusty, obecnie moja print_r zwraca następujący:
1
App\Track Object
(
[table:protected] => Track
[fillable:protected] => Array
(
[0] => id
[1] => GenreId
[2] => Title
[3] => CreatedById
[4] => SelectedCount
[5] => ProducerName
[6] => SourceId
[7] => Published
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 6
[GenreId] => 4
[Title] => Guns Up
[CreatedById] => 1
[SelectedCount] => 0
[ProducerName] =>
[SourceId] => 1
[Published] => 1
)
[original:protected] => Array
(
[id] => 6
[GenreId] => 4
[Title] => Guns Up
[CreatedById] => 1
[SelectedCount] => 0
[ProducerName] =>
[SourceId] => 1
[Published] => 1
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[wasRecentlyCreated] =>
)
**And so on**
Naciśnięcie we właściwym kierunku byłoby świetnie, ja Znalezienie tego całego elokwentnego materiału jest naprawdę użyteczne, ale trudno mi się nim pochwalić.
Dzięki
myślę sformułować moje pytanie źle, wygrałem” • mam pola zerowe, chciałbym tylko wyświetlać gatunki, które mają ślad związany z nimi, moje złe. – ExohJosh
Ok tabelą utworów, czy mają 'genre_id'? – Froxz
Tak, z relacją modelu skonfigurowaną tak public function genre() { return $ this-> belongsTo (\ App \ Gatunek :: klasa, "GenreId", "id"); } – ExohJosh