Amaqhinga obuhlobo bobudlelwano beLaravel

Enye yamandla kaLaravel yi-syntax yayo enhle. Uma unamamodeli amaningi nobudlelwano eLaravel (ngenxa yamatafula amaningi asendaweni egciniwe), ukunqamula kuvame ukuphela ngekhodi engeyinhle kangako. Ngokuhamba kwesikhathi, izandiso ezintathu ezincane zikhombisile ukuthi zingifanele, engingathanda ukuzethula kafushane ngezansi. Sisebenzisa nje ifa elithile, izindlela zomlingo namaqoqo wangokwezifiso.


Okokuqala sakha imodeli entsha ngaphansi kohlelo lokusebenza \ ConvenienceModel.php:

b45e29b9125c2ab565b5526442437686

Ngemuva kwalokho sivumela wonke amamodeli ukuthi athole ifa kusuka ku- ConvenienceModel yethu entsha:

b45e29b9125c2ab565b5526442437686

Hlanganisa imisebenzi yakho nobudlelwano

Uma ufuna ukuthola amamodeli axhunyiwe, usebenzisa i-syntax ejwayelekile ukuwabiza:

b45e29b9125c2ab565b5526442437686

Uma manje ungeza i-logic yebhizinisi lakho, uthanda ukusebenzisa i-case (engezansi) yekesi lekesi:

b45e29b9125c2ab565b5526442437686

Sifuna ukulinganisa i-syntax yakho kokubili okwehlukile. Ukuze senze lokhu, sifaka indlela yomlingo __call ku- ConvenienceModel:

b45e29b9125c2ab565b5526442437686

Le kholi kungenzeka futhi kusibonelo esingenhla:

b45e29b9125c2ab565b5526442437686

Bheka izinto ezingenalutho

Uma ufuna ukukhipha igama lezwe kukheli lomuntu, uvame ukubona ikhodi elandelayo:

b45e29b9125c2ab565b5526442437686

Ukuvimbela lokhu, sifaka umtapo wezincwadi osebenza ngentambo bese sibuyisa into ekhethekile yesigaba esingenalutho uma imodeli ingatholakali:

b45e29b9125c2ab565b5526442437686

Lokhu futhi kwenza ucingo luphumelele (kulesi sibonelo, uma kutholakala okuthile, igama lezwe liyakhishwa, kuzo zonke ezinye izintambo zingenalutho).

b45e29b9125c2ab565b5526442437686

Uma futhi sifuna ukumboza icala lokuthi umsebenzisi one-ID 42 kungenzeka angabikho, sizokwakha nomsebenzi omncane womsizi walokhu:

b45e29b9125c2ab565b5526442437686

Le kholi elandelayo iletha intambo engenalutho noma ngabe umsebenzisi akekho nhlobo:

b45e29b9125c2ab565b5526442437686

Ukudabula kalula ubudlelwano be-n: m

Ekugcineni, sifuna ukwenza lula ikhodi elandelayo:

b45e29b9125c2ab565b5526442437686

Ngenxa yalokhu sisebenzisa amathuba amaqoqo wangokwezifiso . Siqala ngokwandisa imodeli elula:

b45e29b9125c2ab565b5526442437686

Ngemuva kwalokho sakha uhlelo lokusebenza lwefayela \ Helpers \ ConvenienceCollection.php , oluqinisekisa ukuthi ukubiza imisebenzi engaziwa kumaqoqo kwenza umsebenzi wazo zonke izinto eziseqoqweni:

b45e29b9125c2ab565b5526442437686

Ngakho-ke singasebenzisa ikhodi elandelayo ukukhipha wonke amagama awo wonke amazwe kuwo wonke amakheli abo bonke abazali bomuntu:

b45e29b9125c2ab565b5526442437686

Kepha izingcingo ezinjengalezi ezilandelayo zingenzeka:

b45e29b9125c2ab565b5526442437686

Hlunga ngamakholomu amaningi

Ucingo olulandelayo luhlungwa kuphela ngendawo , noma ngabe lokho bekungeyona into obuyifuna:

b45e29b9125c2ab565b5526442437686

Sandisa i-ConvenienceCollection yethu ukunika amandla ukuhlunga ngamakholomu amaningi:

b45e29b9125c2ab565b5526442437686

Ngalokho singakwazi ukuhlunga iqoqo ngokwamakholomu amaningi nge:

b45e29b9125c2ab565b5526442437686

Ukuhlunga okujwayelekile

Sandisa i-ConvenienceCollection ngomunye umsebenzi osizayo:

b45e29b9125c2ab565b5526442437686

Manje usunganikeza imodeli ngayinye umsebenzi wokuhlunga ojwayelekile ngamunye:

b45e29b9125c2ab565b5526442437686

Ngakho-ke singahlela iqoqo nge:

b45e29b9125c2ab565b5526442437686

Amagama ento

Imodeli ngayinye isebenzisa indlela ye-GetLabel (i-GetName iyahluka ngokuya ngemodeli):

b45e29b9125c2ab565b5526442437686

Ngalokhu ungabonisa ngokushesha futhi kalula igama lento. Ngemuva kwalokho ungasebenzisa indlela yomsizi we-sortByLabel ku-ConvenienceCollection:

b45e29b9125c2ab565b5526442437686

Imiphumela engenalutho

Uma isinyathelo esiphakathi singenalutho, imisebenzi yeLaravel first () and last () imvamisa ibuyisa uziro. Ngakho-ke sethula i-GetFirst () ne-GetLast () ukuze ikholi elandelayo iphumelele njalo:

b45e29b9125c2ab565b5526442437686

Ngalokhu sibuyisa i-__empty_helper __x eyibonayo:

b45e29b9125c2ab565b5526442437686

Emuva