Laravel 6 CRUD step by step (PART 2)

This will continuation to cover CRUD and API basics on Laravel .

The steps here will;
  1. Continue from previous Laravel project
  2. Create the interface to view the listing of articles along with the Author's name.
  3. Allow users to view article details of the Author.
  4. Route to access via web browser and display all articles
Its assumed that an article will have one author(person).

=1. Continue from previous Laravel project=

This tutorial is based on the following development environment.

PHP v7.3 (cli)
Centos v7.4.1708 (Core)
Kernel v3.10.0-693.17.1.el7.x86_64
Apache httpd v2.4.6(CentOS)

Database tested for both of these

MySQL MariaDB v5.5.56
Or
Postgresql 10

You need to complete the coding in Part 1 of this tutorial before proceeding.

=2. view the listing of articles along with the Author's name=

==Add Articles relationship with Author===

Firstly, We need to update the database and tightly associate authors to articles. Which means that if a particular author is removed from the database, all of the articles for that author should be deleted too.
Edit the file in database/migrations/xxxx_create_articles_table.php and update the foreign key line below;

Schema::create('articles', function (Blueprint $table) {
    $table->id();
    $table->string('title');
    $table->string('content');
    $table->integer('person_id')->unsigned();
    $table->timestamps();
    $table->foreign('person_id')->references('id')->on('people')->onDelete('cascade');
});

In the class Article we need to specify that it will have one author and the last updated date will also be available. Edit app/Article.php

protected $fillable = [
    'title', 'content', 'person_id', 'updated_at',
];
public function author()
{
    return $this->belongsTo('App\Person', 'person_id', 'id');
}

===Edit Controller display the view===

Laravel comes with several standard functions, one of it is the index( ) where default pages should be loaded. In here, we will have the following actions;

  • Load all the articles from database by editing the ArticleController.php file
  • Send the values to a view in the file article/index.blade.php where its contents will be displayed
Edit the file app/Http/Controllers/ArticleController.php with the following;

public function index()
{
    //
    $articles = Article::all();
    return view('article/index',compact('articles'));
}

Laravel by default use blade to process views, and the files are use the file naming format with extension .blade.php. Start by creating a new folder article where all related views will be placed. Then create the file resources/views/article/index.blade.php and add the following;

<body>
    <div class="articles center sans-serif">
        <h1>Articles</h1>

        @foreach($articles as $article)
        <div >
            <hr/>
            <header >{{ $article->title }}</header>
            <div >
                <p ><span >Author:</span> {{ $article->author->first_name }}</p>
            </div>
        </div>
        @endforeach
    </div>
</body>


===Create route===

Laravel comes with default API generating tools where we specify in the file routes/web.php all the url routings and to which controller it should access. Here is how a user will enter the URL localhost/articles and have the application trigger the function index in the file ArticleController.php.

$ vi routes/web.php

Route::get('articles', 'ArticleController@index');

==Test on web browser==

$ php artisan serve

Open URL http://localhost/articles/

=3. View article details=

==Edit Controller display the article details view===

Laravel standard function to display content details is the show( ) function. Edit the file ArticleController.php and replace the existing function show(Article $article) with the following

public function show($id)
{
//
$article = Article::find($id);
return view('article.show',compact('article'));
}

Edit the resources/views/article/index.blade.php with following contents;
<body>
    <div class="articles center sans-serif">
        <h1>Articles</h1>

        @foreach($articles as $article)
        <div >
            <hr/>
            <header ><a href="{{route('articles.show',['id'=>$article->id])}}">
            {{ $article->title }}</a></header>
            <div >
                <p ><span >Author:</span> {{ $article->author->first_name }}</p>
            </div>
        </div>
        @endforeach
    </div>
</body>

Create a new file resources/views/article/show.blade.php and add these contents.

<body>
<a href="/articles">BACK</a>
    <div class="articles center sans-serif">
        <h1>Article</h1>
            <header >{{ $article->title }}</header>
            <div >
                <p>{{ $article->content }}</p>
                <p ><span >Author:</span> {{ $article->author->first_name }}</p>
                <p>Last updated: {{ $article->updated_at }}</p>             
            </div>
    </div>
</body>

===Create route===

Edit existing web.php and add following line.

Route::get('articles/{id}', 'ArticleController@show')->name('articles.show');

==Test on web browser==

$ php artisan serve

Open URL http://localhost/articles/

No comments:

Blog Archive