the purpose of this tutorial is to show you how you can connect to your database and display the data into a webpage, this is a very simple way of doing it, the steps are basic for people who are trying to learn laravel
Create a model to tie into your database, you can use thiscommand: (use capitalization)
if you need to, you can also create the file manually with the following code:
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Page extends Model { // }
next, you will need a controller, if you followed the previous tutorial, we have already created our pagesController.php, if you havent, you can create the controller by using this command:
after the controller has been created, we need to be able to connect to our database, for that, you need to set the mysql connection settings in the /.env file
1. open .env and set:
DB_DATABASE=[YOUR DATABASE NAME]
DB_USERNAME=[YOUR DATABASE USER NAME] (example: root)
DB_PASSWORD=[YOUR DATABASE PASSWORD]
after you have setup your database connection settings in the .env file, open phpmyadmin, create a new database called laravel5 and dump the following:
-- ////////////////// START DUMP /////////////////////// DROP TABLE IF EXISTS `contents`; CREATE TABLE `contents` ( `id` int(11) NOT NULL, `page_id` int(11) NOT NULL, `body` text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; INSERT INTO `contents` (`id`, `page_id`, `body`) VALUES (1, 1, 'This is the content for the about us page. This content comes from the database.'), (2, 2, 'This is the content for the contact us page. This content comes from the database.<br><form method="post" action="">Your Name<input type="text" name="name"><br>Your Email<input type="email" name="email"><br>Your Phone<input type="number" name="number"><br><input type="submit" value="submit"></form>'), (3, 3, 'we offer many services on this page'); DROP TABLE IF EXISTS `pages`; CREATE TABLE `pages` ( `id` int(11) NOT NULL, `page_title` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `page_url` varchar(100) COLLATE utf8_unicode_ci NOT NULL, `page_description` varchar(100) COLLATE utf8_unicode_ci NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; INSERT INTO `pages` (`id`, `page_title`, `page_url`, `page_description`) VALUES (1, 'About Us Page', 'about_us', 'This is the description for the about us page'), (2, 'Contact Us Page', 'contact_us', 'This the the description for the Contact us page'), (3, 'Services Page', 'services', 'this is the description for the services page'); ALTER TABLE `contents` ADD PRIMARY KEY (`id`); ALTER TABLE `pages` ADD PRIMARY KEY (`id`); ALTER TABLE `contents` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4; ALTER TABLE `pages` MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4; -- ////////////////// END DUMP ///////////////////////
Route::get('page', function(){NOTE: the reason why I usee App in App\Page:: is because if you open the model we created (App/Page.php) you will see a line of code that says: namespace App;
$page = App\Page::first();
echo $page->page_title;
});
restart your server through the command line: to stop the server, just hit control+c to stop and and to start it:
now check to see if it works in your browser by opening this page: http://localhost:8000/page
if you get an error: SQLSTATE[HY000] [1045] Access denied for user 'homestead'@'localhost' (using password: YES)
you will need to restart your server, so stop and then start again the server:
if you successfully opened the url,, you will see the first page title "About Us Page"
Now lets say for example, we wan to display the second page using the id, you can do this too. open web.php file and change the following line of code:
FROM: $page = App\Page::first();
TO: $page = App\Page::find(2);
http://localhost:8000/page
refresh your browser, now you will see the contact us page title: "Contact Us Page"
however, manually putting the id is not very dynamic, the best way is to pass the id from the url, to do that, change the route to the following:
Route::get('page/{id}', function($id){
$page = App\Page::find($id);
echo $page->page_title;
});
open your browser to: http://localhost:8000/page/1 - you will see the about us page, how about if you change the url to http://localhost:8000/page/2 - you will see the contact us page title
ok, so far, you have seen how you can get each page from the database, if you notice on the database table, each page contains a row called 'page_url' this is more practical way to get our pages from the database from the urls
open your browser to: http://localhost:8000/page/about_us
ERROR: you will get an error: Trying to get property of non-object
this is because we are still using the id in the route we created in the web.php file, so lets change the page route in web.php to the following:
Route::get('page/{page_url}', function($page_url){ $page = App\Page::where('page_url','=',$page_url)->first(); echo $page->page_title; });
refresh your browser to: http://localhost:8000/page/about_us
Cool! you should see the about us page title
now, lets say we wanted to show all of our pages in our database, since we only have three in our case, its no big deal.
open web.php and add the following route at the bottom of the file
Route::get('all', function(){ $pages = App\Page::all(); foreach($pages as $page){ echo $page->page_title . '<br>'; } });
open your browser http://localhost:8000/all
you should see:
About Us Page
Contact Us Page
Services Page
now, how can we get the content for each page from the content database table, for that, we need to use relations, relations allow us to bind two tables.
this is how we will do it, first we need to create a Content model, for the contents database table so send this command:
now that you have created the Content model, this will allow us to have access to the content table in our database,
open the web.php file and edit the 'all' route to the following:
Route::get('all', function(){ $contents = App\Content::all(); foreach($contents as $content){ $page = App\Page::where('id','=',$content->page_id)->first(); echo '<h2>'. $page->page_title.'</h2>'. $content->body .'<hr>'; }
});
now refresh your browser to: http://localhost:8000/all
you will see all thre pages with the page title and the page content
public function page(){app/Content.php should look like this:
return $this->belongsTo('App\Page');
} }
Open web.php
Remove this line:
$page = App\Page::where('id','=',$content->page_id)->first();
edit this line:
FROM: echo '<h2>'. $page->page_title.'</h2>'. $content->body .'<hr>';
TO: echo '<h2>'. $content->page->page_title.'</h2>'. $content->body.'<hr>';
now refresh your browser to: http://localhost:8000/all
you will see all three pages with the page title and the page content, nothing has changed