Cara Cara

untung99.homes: Basic Task List Laravel


Untung99 menawarkan beragam permainan yang menarik, termasuk slot online, poker, roulette, blackjack, dan taruhan olahraga langsung. Dengan koleksi permainan yang lengkap dan terus diperbarui, pemain memiliki banyak pilihan untuk menjaga kegembiraan mereka. Selain itu, Untung99 juga menyediakan bonus dan promosi menarik yang meningkatkan peluang kemenangan dan memberikan nilai tambah kepada pemain.

Berikut adalah artikel atau berita tentang Harian untung99.homes dengan judul untung99.homes: Basic Task List Laravel yang telah tayang di untung99.homes terimakasih telah menyimak. Bila ada masukan atau komplain mengenai artikel berikut silahkan hubungi email kami di koresponden@untung99.homes, Terimakasih.

Introduction

This quickstart guide provides a basic introduction to the Laravel framework and includes content on database migrations, the Eloquent ORM, routing, validation, views, and Blade templates. This is a great starting point if you are brand new to the Laravel framework or PHP frameworks in general. If you have already used Laravel or other PHP frameworks, you may wish to consult one of our more advanced quickstarts.

To sample a basic selection of Laravel features, we will build a simple task list we can use to track all of the tasks we want to accomplish (the typical “to-do list” example). The complete, finished source code for this project is available on GitHub.

Installation

Of course, first you will need a fresh installation of the Laravel framework. You may use the Homestead virtual machine or the local PHP environment of your choice to run the framework. Once your local environment is ready, you may install the Laravel framework using Composer:

composer create-project laravel/laravel quickstart --prefer-dist

You’re free to just read along for the remainder of this quickstart; however, if you would like to download the source code for this quickstart and run it on your local machine, you may clone its Git repository and install its dependencies:

git clone https://github.com/laravel/quickstart-basic quickstart

For more complete documentation on building a local Laravel development environment, check out the full Homestead and installation documentation.

Prepping The Database

Database Migrations

First, let’s use a migration to define a database table to hold all of our tasks. Laravel’s database migrations provide an easy way to define your database table structure and modifications using fluent, expressive PHP code. Instead of telling your team members to manually add columns to their local copy of the database, your teammates can simply run the migrations you push into source control.

So, let’s build a database table that will hold all of our tasks. The Artisan CLI can be used to generate a variety of classes and will save you a lot of typing as you build your Laravel projects. In this case, let’s use the make:migration command to generate a new database migration for our tasks table:

php artisan make:migration create_tasks_table --create=tasks

The migration will be placed in the database/migrations directory of your project. As you may have noticed, the make:migration command already added an auto-incrementing ID and timestamps to the migration file. Let’s edit this file and add an additional string column for the name of our tasks:

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

class CreateTasksTable extends Migration

Schema::create('tasks', function (Blueprint $table)

$table->increments('id');

* Reverse the migrations.

To run our migration, we will use the migrate Artisan command. If you are using Homestead, you should run this command from within your virtual machine, since your host machine will not have direct access to the database:

This command will create all of our database tables. If you inspect the database tables using the database client of your choice, you should see a new tasks table which contains the columns defined in our migration. Next, we’re ready to define an Eloquent ORM model for our tasks!

Eloquent Models

Eloquent is Laravel’s default ORM (object-relational mapper). Eloquent makes it painless to retrieve and store data in your database using clearly defined “models”. Usually, each Eloquent model corresponds directly with a single database table.

So, let’s define a Task model that corresponds to our tasks database table we just created. Again, we can use an Artisan command to generate this model. In this case, we’ll use the make:model command:

The model will be placed in the app directory of your application. By default, the model class is empty. We do not have to explicitly tell the Eloquent model which table it corresponds to because it will assume the database table is the plural form of the model name. So, in this case, the Task model is assumed to correspond with the tasks database table. Here is what our empty model should look like:

use Illuminate\Database\Eloquent\Model;

We’ll learn more about how to use Eloquent models as we add routes to our application. Of course, feel free to consult the complete Eloquent documentation for more information.

Routing

Stubbing The Routes

Next, we’re ready to add a few routes to our application. Routes are used to point URLs to controllers or anonymous functions that should be executed when a user accesses a given page. By default, all Laravel routes are defined in the app/Http/routes.php file that is included in every new project.

For this application, we know we will need at least three routes: a route to display a list of all of our tasks, a route to add new tasks, and a route to delete existing tasks. So, let’s stub all of these routes in the app/Http/routes.php file:

use Illuminate\Http\Request;

Route::get('/', function ()

Route::post('/task', function (Request $request)

* Delete An Existing Task

Route::delete('/task/{id}', function ($id)

Displaying A View

Next, let’s fill out our / route. From this route, we want to render an HTML template that contains a form to add new tasks, as well as a list of all current tasks.

In Laravel, all HTML templates are stored in the resources/views directory, and we can use the view helper to return one of these templates from our route:

Route::get('/', function ()

Of course, we need to actually define this view, so let’s do that now!

Building Layouts & Views

This application only has a single view which contains a form for adding new tasks as well as a listing of all current tasks. To help you visualize the view, here is a screenshot of the finished application with basic Bootstrap CSS styling applied:

Defining The Layout

Almost all web applications share the same layout across pages. For example, this application has a top navigation bar that would be typically present on every page (if we had more than one). Laravel makes it easy to share these common features across every page using Blade layouts.

As we discussed earlier, all Laravel views are stored in resources/views. So, let’s define a new layout view in resources/views/layouts/app.blade.php. The .blade.php extension instructs the framework to use the Blade templating engine to render the view. Of course, you may use plain PHP templates with Laravel. However, Blade provides convenient short-cuts for writing cleaner, terse templates.

Our app.blade.php view should look like the following:

// resources/views/layouts/app.blade.php

<<>title>Laravel Quickstart - Basic<<>/title>

<<>nav class="navbar navbar-default">

Note the @yield('content') portion of the layout. This is a special Blade directive that specifies where all child pages that extend the layout can inject their own content. Next, let’s define the child view that will use this layout and provide its primary content.

Defining The Child View

Great, our application layout is finished. Next, we need to define a view that contains a form to create a new task as well as a table that lists all existing tasks. Let’s define this view in resources/views/tasks.blade.php.

We’ll skip over some of the Bootstrap CSS boilerplate and only focus on the things that matter. Remember, you can download the full source for this application on GitHub:

// resources/views/tasks.blade.php

-- Bootstrap Boilerplate... -->

-- Display Validation Errors -->

@include('common.errors')

<<>form action="/task" method="POST" class="form-horizontal">

<<>label for="task" class="col-sm-3 control-label">Task<<>/label>

<<>input type="text" name="name" id="task-name" class="form-control">

<<>div class="col-sm-offset-3 col-sm-6">

<<>button type="submit" class="btn btn-default">

<<>i class="fa fa-plus"><<>/i> Add Task

A Few Notes Of Explanation

Before moving on, let’s talk about this template a bit. First, the @extends directive informs Blade that we are using the layout we defined at resources/views/layouts/app.blade.php. All of the content between @section('content') and @endsection will be injected into the location of the @yield('content') directive within the app.blade.php layout.

Now we have defined a basic layout and view for our application. Remember, we are returning this view from our / route like so:

Route::get('/', function ()

Next, we’re ready to add code to our POST /task route to handle the incoming form input and add a new task to the database.

Note: The @include('common.errors') directive will load the template located at resources/views/common/errors.blade.php. We haven’t defined this template, but we will soon!

Adding Tasks

Validation

Now that we have a form in our view, we need to add code to our POST /task route to validate the incoming form input and create a new task. First, let’s validate the input.

For this form, we will make the name field required and state that it must contain less than 255 characters. If the validation fails, we will redirect the user back to the / URL, as well as flash the old input and errors into the session:

Route::post('/task', function (Request $request)

$validator = Validator::make($request->all(), [

'name' => 'required