Building Your First Application with Laravel

Dikshant Rajput
4 min readApr 24, 2021

--

In my previous post, I shared about the folder structure of Laravel. If you haven’t read it, I will suggest you to first read that and then carry on with this one.

Link of my previous blog post: https://dikshantraj2001.medium.com/getting-started-with-laravel-5c9191cee5d0

We will be making a simple To Do App. I know that sounds little simple at first but believe me it’s like the hello world program of our web world and by making it you could gain a lot of confidence. Just give it a shot.

NOTE: All the code is available on github here :

https://github.com/dikshantrajput/todo-laravel

It would be good if you just not only stare at this blog post but also code along with me. Are you ready for making your first application? If Yes!! Hit that clap button and Let’s Begin…..

First of all setup a new project along with database. You can name it as you wish . For simplicity I will name it to-do-app.

If you haven’t read my first blog post I would recommend you to first read that. Link is given above.

composer create-project laravel/laravel to-do-app

Open a terminal in your preferred directory and run above command.

After the project is successfully installed, setup your .env file and initialise the DB_DATABASE variable with the name of db that you want to keep.

If you don’t want to take more overhead just fork my repository and follow these steps:

Run composer install and then composer updateRun npm install && npm run devRun php artisan key:generateRun php artisan migrate --seed

P.S. You will get bootstrap and font awesome installed with the code .If you have any issue just shoot a comment. I will solve your queries

As I told in my first blog post, Laravel is a framework build with php following MVC pattern. So, the model that we will be creating will be named as ToDo and remember table is named after models in migrations. (e.g if we named our model ToDo then we have to name our table to_dos *plurar*).

So let’s first make migration for our db. Migration is like a schema for our to_dos table .

Run php artisan make:migration create_to_dos_table

In migration file, write this in up method

Schema::create('to_dos', function (Blueprint $table) {    $table->id();    $table->string('created_by');    $table->string('todo_text');    $table->dateTime('end_at');    $table->timestamps();});

After this make ToDo model where our db logic will be placed

php artisan make:model ToDo

In ToDo model add this code:

public $fillable = [    'created_by',    'todo_text',    'end_at',];public function getCreatedDateAttribute(){   return Carbon::parse($this->created_at)->diffForHumans();}public function getEndingDateAttribute(){    return Carbon::parse(now())->diffForHumans($this->end_at);}

In fillable array write the fields you want to create data with help of model.

Bellow two functions are just used for view purpose to make created_at and end_at value more human readable.

Then go to routes.php and define a route for todos. We will be making it a resourcefull controller . If you want to know more please read in documentation once and if you still need any help shoot that in the comment.

Place this code in your web.php file

Route::resource('todos','ToDoController');

Then create a controller file where all our actual logic will be placed .

php artisan make:controller ToDoController --resource

Place this code in it

<?phpnamespace App\Http\Controllers;use App\Http\Requests\ToDoRequest;use App\Models\ToDo;use Illuminate\Contracts\View\View;use Illuminate\Http\Request;class ToDoController extends Controller{public function index(): View{$todos = ToDo::orderBy('updated_at','desc')->paginate(20);return view('index',compact('todos'));}public function create(): View{return view('create');}
public function store(ToDoRequest $request){$data = $request->validated();if(ToDo::create($data)){return redirect()->route('todos.index')->with(['success-message'=>'ToDo created successfully']);}else{return redirect()->route('todos.create')->with(['error-message'=>'Error creating a todo']);}}public function edit(ToDo $todo): View{return view('edit',compact('todo'));}
public function update(ToDoRequest $request, ToDo $todo){$data = $request->validated();if($todo->update(['end_at'=>$data['end_at'],'todo_text'=>$data['todo_text']])){return redirect()->route('todos.index')->with(['success-message'=>'ToDo updated successfully']);}else{return redirect()->route('todos.create')->with(['error-message'=>'Error updating todo']);}}public function destroy(ToDo $todo){if($todo->delete()){return redirect()->route('todos.index')->with(['success-message'=>'ToDo deleted successfully']);}else{return redirect()->route('todos.create')->with(['error-message'=>'Error deleting todo']);}}}

Only the view part is left now. I know this is a long blog but hold on with me for a bunch of lines of code more.

Go to resources directory and in views directory create all the views file related to create todo, edit and some basic file like a layout file for basic html structure. You will find all these codes in my github repo.

Now run

php artisan serve

Open localhost:8000 in your browser and you will see something like this

Route /todos

If you want to add a todo click on Add a ToDo button and you will see something like this

Route /todos/create

After creating a todo you will be redirected to todos homepage.

Then you can edit it or delete it.

So, this was it for today’s blog. I hope you would like it. If you find any issue just shoot out it in comments and if you like it hit that clap button.

--

--

Dikshant Rajput
Dikshant Rajput

No responses yet