RastcodeS

Where we take good care of both your front and back-end needs

Great place to take your code to the next level

Checkout my work and get in touch



RastcodeS Logo
Go Back

Laravel Eloquent, Post List & Tinker - How I made this app Part VII


Eloquent Laravel PHP Tinker



Written by Rastko

date - 11/Sep/18 | views - 291

Laravel Eloquent, Post List & Tinker - How I made this app Part VII

     Getting a list of items from the database and returning it to the front end. Introduction to Tinker.

     The seventh part in the How I made this app series. If you haven't already, check out previous chapters:

 

     We've already created our first database table in the previous chapter. Now, we're going to start communicating with it.

     In Laravel, we have the option of using Eloquent, to read and write to databases and that's exactly what we're going to do. Eloquent ORM (Object Relational Mapping) is simple to use and we have an easy way of getting models, creating new models, updating and deleting them. The best part about Eloquent, we don't have to write all those long queries.

 

Tinker

 

     Before we start using Eloquent inside our PostsController, we're going to add a few posts with Tinker. Tinker is a smart tool that allows us to interact with our Laravel app, though a console line. We're going to use it to add a few posts.

     To start Tinker, in the terminal of your choice run:

php artisan tinker

 

     Unlike most commands, this one doesn't end. In fact this is just the beginning. You should see something like this:

Tinker Console

 

     Now, let's run our first Tinker command. If we want to know how many items we have in a table, we can run something like this:

App\Post::count()

 

     You see that we get 0. That's OK, since we haven't added any posts yet. Let's create a new Post:

$post = new App\Post();

 

     Nothing seems to have happened. It actually has, under the hood. Tinker has saved the $post variable to memory, and we can now start adding fields. Add the title:

$post->title = 'Welcome';

 

     And the content:

$post->content = 'Welcome to our brand new Blog....';

 

     Now we've set our fields. It's time to save our first post:

$post->save();

 

     If everything went well, you should see Tinker output true. Let's run that count function again and see how many posts we have now:

App\Post::count()

 

     We get 1. That's great and if you go to PHP MyAdmin, in our posts table, you'll find our new post. Let's add another one. Run these lines one at a time:

$post = new App\Post();
$post->title = 'Cool Tech Guide';
$post->content = 'Tech is everywhere. You don\'t say...';
$post->save();

 

     If you're following this guide word for word, make sure you escape your apostrophes. Running count now gives us 2. To exit Tinker run:

quit

 

     Tinker is pretty cool, right? It's great for programmers, but we need a way to show our posts to the visitors of our website. We'll do that next.


 

Post List

 

     As this small header says, we want to get a list of all of our posts and display it to the user. For that, we're going to do two things - develop our index function in PostsController and create a view to display the posts.

Controller

     Open the PostsController inside app - Http - Controllers directory. Remember that text we returned in the index function:

public function index()
{
   return "Hello Resource Controller";
}

 

     We'll get rid of that and create a proper function. Index is actually intended to work with lists.  Because we're dealing with posts, we need to tell our controller we want to use the Post model. Above the class declaration add:

//...
use App\Post;
class PostsController extends Controller
{
//...

 

     Now, we develop or index function:

public function index()
{
   // Get All Posts
   $posts = Post::all();

   // Return View with Posts
   return view('posts.index')->with('posts', $posts);
}

 

     Let's dissect this code. This is Eloquent. We simply declare a variable posts and assign it the result of the all function. This function returns all items from a table. You do need to tell Eloquent, which model you want to use. The return statement returns a view, like we did before. Now we're also using the with function. This function makes sure all of our posts go to the view. The first parameter is the variable you want to use in the view. If you try to access the posts now by searching for 'http://yourappname.me/posts' in your browser, you'll get an error. We haven't created the view yet. Let's do that.

 

View

     Go to views directory under resources and add a new folder - posts. Inside, create a new file - index.blade.php. Let's extend the default layout and add are meta information:

@extends('layouts.default')
{{-- All Posts --}}

{{-- Title --}}
@section('title', 'Posts')

{{-- Description --}} 
@section('description', 'These are all of our great Posts!')

@section('content')
   <br>
   <h2>Posts</h2>

   <p>These are all of our great Posts!</p>

@endsection

 

     We've extended the layout, added a title and a custom description. We've also implemented the content section. If you now search for 'http://yourappname.me/posts' in the browser, your new page will appear. No posts yet so let's do that now.  Under the paragraph add:

@forelse ($posts as $post)

   <div class="card">
      <div class="card-header">
         <a href="/posts/{{$post->id}}" class="lead">{{$post->title}}</a>
      </div>

      <div class="card-body"></div>
      <div class="card-footer"></div>
   </div>
   <br>
		 
@empty
   <p>No Posts found.</p>
@endforelse

 

     Ok, we've done a lot here, so let's explain. We are using a cool forelse loop, which comes with Blade. This allows toy to loop though an array, if there's anything inside it. If not, that's where empty comes to play. It's basically if-else and for each, all in one. The rest is pretty straightforward. We use some Bootstrap classes to create our post cards. Don't worry about the empty divs. We'll add more features later and that's when we'll use these. Inside the header we add a link, which displays the title of our post. It also leads to an individual post, though the id we pass. If you click on one of those links it's not going to show anything, since we haven't developed that yet. We'll do that in the next chapter. For now, you should have a list of posts, looking something like this:

Post List Example

 

GitHub

 

     To finish of, we do our Git routine.

     Add changes:

git add *

 

     Make a commit:

git commit -m "PostsController index function and posts index view"

 

     Push to GitHub:

git push

 

     You can find the code for this project at github.com/RastkoSavic

 

     This was all for this part of the How I made this app series. Next, we'll wrap up a few things, develop the show function and display individual posts. See you then!


View App/Backend
Login to comment

Comments


No Comments yet. Be the first!

Currently working on a Laravel project

Check it out
Java Break - Declare and Initialize Variables - Java Core Part - VI
Java Break - Declare and Initialize Variables - Java Core Part - VI

     Declaring and initializing variable in Java. How to do it properly and avoid mistakes.

     The sixth chapter...

Java


Recents


Popular