How to Retrieve Records in Laravel Model?

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-retrieve-records-in-laravel-model

In this post, I will share an example of how to retrieve records in the Laravel model. Af…


This content originally appeared on DEV Community and was authored by codeanddeploy

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/how-to-retrieve-records-in-laravel-model

In this post, I will share an example of how to retrieve records in the Laravel model. After saving records we need to retrieve the records and show them to the HTML table. Now let's do it.

I assumed that you have already installed Laravel 8 application.

Step 1: Create Model, Migration, and Controller

Let's create first our migration. See our previous post about creating a model with additional options.

Run the following command:

php artisan make:model Post --migration --controller

Once generate our Model, migration, and controller. Navigate your migration with this path: project_folder/database/migrations/{datetime}_create_posts_table.php

See below the sample code of our migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('description')->nullable();
            $table->text('body');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Then run migrate command:

php artisan migrate

Once done with the migration create your posts seeder. See this post on how to do it.

Step 2: Create Route

Now let's create our posts routes. Navigate project_folder/routes/web.php

Route::get('/posts', 'PostController@index')->name('post.index');

Step 3: Setup Controller

Next, we will set up our controller for retrieving data.

<?php

namespace App\Http\Controllers;

use App\Models\Post;
use Illuminate\Http\Request;

class PostController extends Controller
{
    public function index() 
    {   
        $posts = Post::all();

        return view('posts.index', compact('posts'));
    }
}

Step 4: Setup Views

Then we will add our views. Create posts folder inside project_folder/resources/views. Then add index.blade.php see below code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Posts</title>
</head>
<body>
    <table>
        <tr>
            <td>Id</td>
            <td>Title</td>
            <td>Description</td>
            <td>Body</td>
        </tr>
        @foreach($posts as $post)
            <tr>
                <td>{{ $post->id }}</td>
                <td>{{ $post->title }}</td>
                <td>{{ $post->description }}</td>
                <td>{{ $post->body }}</td>
            </tr>
        @endforeach
    </table>
</body>
</html>

Now we already retrieve our records from our Laravel model. See below results.

how-to-retrieve-records

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/how-to-retrieve-records-in-laravel-model if you want to download this code.

Happy coding :)


This content originally appeared on DEV Community and was authored by codeanddeploy


Print Share Comment Cite Upload Translate Updates
APA

codeanddeploy | Sciencx (2022-04-19T05:01:21+00:00) How to Retrieve Records in Laravel Model?. Retrieved from https://www.scien.cx/2022/04/19/how-to-retrieve-records-in-laravel-model/

MLA
" » How to Retrieve Records in Laravel Model?." codeanddeploy | Sciencx - Tuesday April 19, 2022, https://www.scien.cx/2022/04/19/how-to-retrieve-records-in-laravel-model/
HARVARD
codeanddeploy | Sciencx Tuesday April 19, 2022 » How to Retrieve Records in Laravel Model?., viewed ,<https://www.scien.cx/2022/04/19/how-to-retrieve-records-in-laravel-model/>
VANCOUVER
codeanddeploy | Sciencx - » How to Retrieve Records in Laravel Model?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/19/how-to-retrieve-records-in-laravel-model/
CHICAGO
" » How to Retrieve Records in Laravel Model?." codeanddeploy | Sciencx - Accessed . https://www.scien.cx/2022/04/19/how-to-retrieve-records-in-laravel-model/
IEEE
" » How to Retrieve Records in Laravel Model?." codeanddeploy | Sciencx [Online]. Available: https://www.scien.cx/2022/04/19/how-to-retrieve-records-in-laravel-model/. [Accessed: ]
rf:citation
» How to Retrieve Records in Laravel Model? | codeanddeploy | Sciencx | https://www.scien.cx/2022/04/19/how-to-retrieve-records-in-laravel-model/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.