First CRUD with laravel 5.5 from Scratch

First CRUD with laravel 5.5 from Scratch

laravel, laravel 5.5, laravel crud, PHP Framework, laravel beginner tutorial, MVC CRUD

Controllers: to handle user requests and retrieve data, by leveraging Models
Models: to interact with your database and retrieve your objects’ information
Views: to render the pages OR template

Additionally, laravel use routes to map URLs to designated controller actions and page action travelling.

In this blog we covered the first sample and simple CRUD which will help to beginner to learn with laravel 5.5.

Prerequisite

Composer: Laravel utilizes Composer to manage it’s dependencies. So, before setting-up Laravel, you have to check for Composer installed on your machine OR not.

– PHP: PHP >= 7.0.0
– PHP Extension: OpenSSL, PDO, Mbstring, Tokenizer and XML

Read more here about laravel 5.5 installation documentation.

Once above all things get ready we are ready to go for create laravel application:

‘composer create-project laravel/laravel –prefer-dist laracrud’

Here, ‘laracrud’ will be our application folder name but you can change whatever you want.

Next, configure the database in the `.env` file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=crud55
DB_USERNAME=root
DB_PASSWORD=root

Next, create schema for our database table which we will user for this example:

‘php artisan make:migration create_tickets_table’

Above command will generate the migration file under laracrud > database > migrations folder. And made column level change look like:

increments(‘id’);
$table->integer(‘user_id’);
$table->string(‘title’)->nullable();
$table->string(‘description’)->nullable();
$table->timestamps();
});
}

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

Once, above migration file saved, run below command to execute migration and it’ll create tickets table into your database:

php artisan migrate

After this you can setup laravel authentication which is provided by default:

php artisan make:auth

Now application is ready to execute. Remember that till now we just set-up the laravel and database with authentication only. Run below command to run laravel application:

php artisan serve

Now, we can create model, view and controller for our tickets CRUD:

Controller: php artisan make:controller TicketController
Model: php artisan make:model Ticket

Once above model and controller created, we will go to create view for it. First create one folder call ‘tickets’ under ‘laracrud > resources > views’ and create files ‘index.blade.php’, ‘create.blade.php’, ‘edit.blade.php’ into new created folder. File code will look like:

Now, we have related url with it’s controller via registering route into ‘laracrud > routes > web.php’ router file like:

Route::get(‘/tickets’, ‘TicketController@index’); //To listing of records
Route::get(‘/create/ticket’,’TicketController@create’); //To open create form
Route::post(‘/create/ticket’,’TicketController@store’); //To store create form submitted data
Route::get(‘/edit/ticket/{id}’,’TicketController@edit’); //To open edit form
Route::post(‘/edit/ticket/{id}’,’TicketController@update’); //To update edit form submitted data
Route::delete(‘/delete/ticket/{id}’,’TicketController@destroy’); //To delete form submitted record data

Now add all method into your controller:

user()->id)->get();

return view(‘user.index’,compact(‘tickets’));
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view(‘user.create’);
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$ticket = new Ticket();
$data = $this->validate($request, [
‘description’=>’required’,
‘title’=> ‘required’
]);

$ticket->saveTicket($data);
return redirect(‘/tickets)->with(‘success’, ‘Record has been created.’);
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$ticket = Ticket::where(‘user_id’, auth()->user()->id)
->where(‘id’, $id)
->first();

return view(‘user.edit’, compact(‘ticket’, ‘id’));
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$ticket = new Ticket();
$data = $this->validate($request, [
‘description’=>’required’,
‘title’=> ‘required’
]);
$data[‘id’] = $id;
$ticket->updateTicket($data);

return redirect(‘/tickets)->with(‘success’, ‘Record has been updated.’);
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$ticket = Ticket::find($id);
$ticket->delete();

return redirect(‘/tickets)->with(‘success’, ‘Record has been deleted.’);
}
}

And update model like:

protected $fillable = [‘user_id’, ‘title’, ‘description’];

Your all blade files look like:

index.blade.php

@extends(‘layouts.app’)

@section(‘content’)

@foreach($tickets as $ticket)

@endforeach

ID Title Description Action
{{$ticket->id}} {{$ticket->title}} {{$ticket->description}} id)}}” class=”btn btn-primary”>Edit id)}}” method=”post”>
{{csrf_field()}}

Delete

@endsection

create.blade.php

@extends(‘layouts.app’)

@section(‘content’)

@if ($errors->any())

    @foreach ($errors->all() as $error)

  • {{ $error }}
  • @endforeach

@endif

Ticket Title:

Ticket Description:

Create

@endsection

edit.blade.php

@extends(‘layouts.app’)

@section(‘content’)

@if ($errors->any())

    @foreach ($errors->all() as $error)

  • {{ $error }}
  • @endforeach

@endif

{{csrf_field()}}

Ticket Title:
title}} />

Ticket Description:
{{$ticket->description}}

Update

@endsection

That’s it! Now you can open of index of ticket crud something like: http://example.com/tickets

Hope this tutorial will help you understand ‘How to create basic CRUD in laravel 5.5’. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.