Laravel and PostgreSQL Web App PART 5

Have a Database Problem? Speak with an Expert for Free
Get Started >>

Introduction

This is the “Laravel and PostgreSQL web app Part 5”, a lesson in the multiple-series tutorial. In Part 4, you discovered how to make a dashboard for your web app and then showed some records. You also created a modal form to add records to your database in PostgreSQL. Today, in Part 5, you’ll learn how to perform the CREATE operation in your Laravel PostgreSQL web app.

If you know the steps for executing the operation CREATE for the web app that you just developed, skip to The Code.

Prerequisites

Complete Parts 1 through 4 of the multiple-series tutorial, “Laravel PostgreSQL web app.” Here are the links:

Laravel and PostgreSQL Web App Part 1

Laravel and PostgreSQL Web App Part 2

Laravel and PostgreSQL Web App Part 3

Laravel and PostgreSQL Web App Part 3

How to CREATE record in PostgreSQL using Laravel

In the previous lesson, Part 4, we went over the steps to validate the app with the function store() located within the file ProjectController.php. Alter the file to give your Laravel PostgreSQL web app the required functionality so that it can store or save the modal form details.

  • Start by performing an application model importation. Supplement the code like this:
1
use AppProject;
  • Use this command to construct a new variable $project:
1
2
3
4
5
6
7
8
9
// create project
$project = new Project;
$project->name = $request->input('project_name');
$project->description = $request->input('description');
$project->user_id = auth()->user()->id;

$project->save();

return redirect('/home')->with('success', 'Project Added');

Here’s the explanation for the code above:

  • You created the $project and allocated your Project to that variable.

  • The $request object contains the fields you need. The description and project_name is what you furnished from the input().

  • You used code for the authentication function auth()->user() to get the current user’s ID.

  • Next, you called the save() function.

  • You performed a home page redirect and alerted the user that the project was added successfully with the Project Added notification message.

  • Now, you can verify that the modifications were made by logging into your app. Add a project. When you do, you should see something resembling this:

and let’s verify that via pgAdmin.

How to READ record in PostgreSQL using Laravel

In lesson 4, you created a new record for your Laravel PostgreSQL web app. Let’s go a step further and do some record fetching. You’ll want to display the records on the home page of your web app.

  • Use this code to change the function index() located in your file ProjectController.php :
1
2
 $projects = Project::where('user_id', auth()->user()->id)->get();
        return view('home')->with('projects', $projects);

Here’s an explanation about the code shown above:

  • The Project::where('user_id', auth()->user()->id)->get(); query returns data which the variable $projects will hold.

  • The current user’s projects are returned.

  • By way of the variable $projects, the PostgreSQL database values were sent to your home page.

  • You can now update your home page so that it displays the records.

  • Open the file home.blade.php , and input this code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<tbody>
@foreach($projects as $project)
    <tr>
    <th scope="row">{{$project->id}} </th>
    <td>{{$project->name}}  </td>
    <td>{{$project->description}} </td>
    <td> <button data-id="" type="button" class="delete_programs btn btn-danger" name="button"><span class="fa fa-trash"></span>Delete</button>

    <td> <button type="button"
    data-toggle="modal"
    data-target="#editModal" class="btn btn-primary" name="button" ><span class="fa fa-edit"  ></span>Edit</button>
    </tr>
@endforeach

</tbody>

Here’s an explanation of the above code:

  • The $project data object is looped to obtain the necessary details.

  • Your Laravel PostgreSQL web app is ready to be tested. When you do, it should resemble this type of output:

It’s a success! You retrieved a record in your Laravel and PostgreSQL web app.

Conclusion

You finished Part 5 of the Laravel PostgreSQL web app multiple-series tutorial. Excellent job! Today, you learned how to display records that pertain to the current user’s specific ID. You assigned your project to the variable $project. Shortly afterward, it was necessary to make some modifications to the index() file found inside your web app’s _ProjectController.php_file. This resulted in adding functionality for saving the records and displaying them. Refer to these steps as needed and then proceed to Part 6 when you’re ready.

The Code

Here’s the entire code for the Laravel PostgreSQL web app, Part 5.

The home.blade.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header bg-info text-white">Dashboard</div>

                <div class="card-body">
                  <!-- This is for input validation purposes -->
                   @include('inc.messages')
                   <button class="btn btn-primary btn-lg"
                   data-toggle="modal"
                   data-target="#addModal"
                   type="button" name="button">Add Projects</button>
                   <hr>
                   <h2> My Available Projects </h2>


                   <table class="table table-striped table-dark" id="table-programs">

                    <thead>
                        <tr>
                        <th scope="col">#</th>
                       <th scope="col">Project Name</th>
                        <th scope="col">Description</th>
                        <th scope="col">Action</th>
                        </tr>
                    </thead>

                    <tbody>
                    @foreach($projects as $proyekto)
                        <tr>
                        <th scope="row">{{$proyekto->id}} </th>
                        <td>{{$proyekto->name}}  </td>
                        <td>{{$proyekto->description}} </td>
                        <td> <button data-id="" type="button" class="delete_programs btn btn-danger" name="button"><span class="fa fa-trash"></span>Delete</button>

                        <td> <button type="button"
                        data-toggle="modal"
                        data-target="#editModal" class="btn btn-primary" name="button" ><span class="fa fa-edit"  ></span>Edit</button>
                        </tr>
                    @endforeach

                    </tbody>
                    </table>

                </div>
            </div>
        </div>
    </div>
</div>


<div class="modal fade bg-primary text-dark" tabindex="-1" role="dialog" id="addModal">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Add Project</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body">
        <form class="" action="{{route('projects.store')}}" method="post">
            {{csrf_field()}}
            <div class="form-group">
                <label for="">Project Name</label>
                <input type="text" class="form-control" name="project_name">
            </div>
            <div class="form-group">
                <label for="">Program Description</label>
                <textarea type="text" class="form-control" name="description"></textarea>

            </div>
            <input type="submit" name="submit" value="Submit" class="btn btn-success">
        </form>
      </div>

    </div>
  </div>
</div>
@endsection

The ProjectsController.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Project;
class ProjectsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }


    public function index()
    {
        $project = Project::where('user_id', auth()->user()->id)->get();
        return view('home')->with('projects', $project);
    }

    public function store(Request $request){
        // validation
        $this->validate($request,[
            'project_name' => 'required',
            'description'  => 'required'
        ]);


        // create project
        $project = new Project;
        $project->name = $request->input('project_name');
        $project->description = $request->input('description');
        $project->user_id = auth()->user()->id;

        $project->save();

        return redirect('/home')->with('success', 'Project Added');
    }
}

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.