Laravel 5 : Learn Easy: Computer Programming For Everybody by Sanjib Sinha
Author:Sanjib Sinha [Sinha, Sanjib]
Language: eng
Format: azw3
Published: 2016-03-27T04:00:00+00:00
$this>songs = $songs;
}
//code ended
And now we need not inject Model Song object to our index() method any more. So instead of writing that:
public function index(Song $songs)
{
$songs = $songs->get();
return view('songs.songs', ['songs' => $songs]);
}
//code ended
We can write this:
//code starting
public function index()
{
$songs = $this->songs->get();
return view('songs.songs', ['songs' =>
$songs]);
}
//code ended
And our show() methods also changes as we can safely write something like this:
//code starting
public function show($songs)
{
$songs = $this>songs;
return view('songs.showsongs', ['songs' =>
$songs]);
}
//code eneded
Finally I would like to show one more thing before concluding this part of Model dependencies injection. There are few more useful methods to fetch either 'songs' table in array or in a simple JSON format. It is very nice and handy. So let us have a look at it.
For a quick demo, we have a route like this:
get('songsall', 'SongsController@test');
And in the 'SongsController' test() method we have something like this:
//code starting
public function test()
{
$songs = $this->songs->get()>toArray();
return $songs;
}
//code ended
We have already injected Model Song in our construct so we could grab all the values in our $songs object and return them. Same is true for a JSON format. We could have changed that method to this:
//code starting
public function test()
{
$songs = $this->songs->get()>toJson();
return $songs;
}
//code ended
And the output of JSON format is as expected:
//output starting
[{"id":"1","singer_id":null,"user_id":null,"title":"Baby do you wanna
dance","slug":"baby-do-you-wanna-dance","lyrics":null,"created_at":"2015-07-17
00:49:58","updated_at":"2015-07-17 01:03:44"},
{"id":"2","singer_id":null,"user_id":null,"title":"Rasputin","slug":"rasputin","ly
rics":null,"created_at":"2015-07-17 01:11:32","updated_at":"2015-07-17
01:11:32"},{"id":"3","singer_id":null,"user_id":null,"title":"I have a
dream","slug":"i-have-a-dream","lyrics":null,"created_at":"2015-07-17
01:12:10","updated_at":"2015-07-17 01:12:10"},
{"id":"4","singer_id":null,"user_id":null,"title":"Money, Money,
Money","slug":"money-money-money","lyrics":null,"created_at":"2015-07-17
01:12:53","updated_at":"2015-07-17 01:12:53"}]
//output ended
Next we will very quickly have a look at few methods. Say if we write in our test() method:
public function test()
{
$songs = Song::findOrNew(2);
return $songs;
}
We get back the details of the song with ID 2. If we write something like this inside our test() method:
$songs = Song::where('id', 2)->first();
return $songs;
We get the same effect.
Suppose you have created a Singer Model where in your table 'singers' you have a record of 'age' of the singers, then You can have a not found exception with this codes:
$singers = Singers::where('age', '>', 75) ->firstOrFail();
return $singers;
There are many like through your User Model you can mathematically calculate in your 'users' table like this:
$count = User::where('active', 1)->count();
$max = User::where('active', 1)->max('age');
I would suggest you to go through the source codes and spend as much time as possible to get more acquainted with the new features of Laravel Eloquent operations.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Personalized inhaled bacteriophage therapy for treatment of multidrug-resistant Pseudomonas aeruginosa in cystic fibrosis by unknow(182499)
CONSORT 2025 statement: updated guideline for reporting randomized trials by unknow(90868)
Critical evaluation of the ProfiLER-02 study design and outcomes by Vivek Subbiah & Razelle Kurzrock(90450)
Cardiac gene therapy makes a comeback by Oliver J. Müller & Susanne Hille & Anca Kliesow Remes(90219)
Whisky: Malt Whiskies of Scotland (Collins Little Books) by dominic roskrow(74460)
Unveiling the design rules for tunable emission in graphene quantum dots: A high-throughput TDDFT and machine learning perspective by Şener Özönder & Mustafa Coşkun Özdemir & Caner Ünlü(50911)
A yeast-based oral therapeutic delivers immune checkpoint inhibitors to reduce intestinal tumor burden by unknow(40283)
Covalent hitchhikers guide proteins to the nucleus by Alexander F. Russell & Madeline F. Currie & Champak Chatterjee(40223)
Meet the Authors: Christopher R. Mansfield and Emily R. Derbyshire by Christopher R. Mansfield & Emily R. Derbyshire(40109)
Alkaline-earth metals promote propane dehydrogenation with carbon dioxide through geometric effects: Altering the reaction pathway by unknow(32751)
Induced iron vacancies boosting FeOOH loaded on sustainable Fenton-like collagen fiber membrane for efficient removal of emerging contaminants by unknow(32534)
Efficient electric-field-assisted photochemical conversion of methane to n-propanol exclusively over penetrated TiO2Ti hollow fibers by Guanghui Feng(32468)
Bi2SiO5 nanosheets as piezo-photocatalyst for efficient degradation of 2,4-Dichlorophenol by Hangyu Shi & Yifu Li & Lishan Zhang & Guoguan Liu & Qian Zhang & Xuan Ru & Shan Zhong(32405)
A novel NDIPTA organic heterojunction photocatalyst with built-in electric field for efficient hydrogen production by Jiahui Yang & Baojun Ma & Yongfa Zhu(32378)
Enhanced conversion of methane to liquid-phase oxygenates via hollow ferrite nanotube@horseradish peroxidase based photoenzymatic catalysis by Jun Duan & Shiying Fan & Xinyong Li & Shaomin Liu(32346)
Ordered macroporous superstructure of defective carbon adorned with tiny cobalt sulfide for selective electrocatalytic hydrogenation of cinnamaldehyde by Xiao-Shi Yuan & Sheng-Hua Zhou & San-Mei Wang & Wenbo Wei & Xiaofang Li & Xin-Tao Wu & Qi-Long Zhu(32268)
What's Done in Darkness by Kayla Perrin(27162)
Topological analysis of non-conjugated ethylene oxide cored dendrimers decorated with tetraphenylethylene: Insights from degree-based descriptors using the polynomial approach by A Theertha Nair & D Antony Xavier & Annmaria Baby & S Akhila(26551)
Investigation of mechanical and self-healing properties of hydroxyl-terminated polybutadiene functionalized with 2-ureido-4-pyrimidinone by Mohsen Kazazi & Mehran Hayaty & Ali Mousaviazar(26481)