Laravel Project Example
Hello Coders, today we develop the laravel crud application with laravel letest version larave 5.7.
Step 1 : First we install the laravel package using below command,
1 |
composer create-project --prefer-dist laravel/laravel CRUD |
On this command we get laravel letest version because we not define the version of the laravel on the installation command, if we added the version on the command, command look like this,
1 |
composer create-project --prefer-dist laravel/laravel blog 5.5 |
If you want to know about how you can setup a existing laravel project see this link Click Here
Step 2: On second step we configure the database file name .env file, which is stored in Laravel project root directory.
Sometimes this file named as .env.example, so not confused do one thing that make a copy of that file and save with .env.
.env
1 2 3 4 5 6 7 8 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database_name DB_USERNAME=host_username DB_PASSWORD=host_password |
Step 3: On this step we create a table on database.
we are going rto craete a crud proect for users, means Users data crud application. So very first run theis command on you terminal or gitbash( for windows ).
1 |
php artisan make:migration create_users_table --create=Users |
After run this command you will find a file on following path: database/migrations. And you have to put below file on the migration file for create a users table
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 |
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateUsersTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('Users', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->text('detail'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('Users'); } } |
After creting this file you the laravel migration command which is use for migrating the table on phpmyadmin database. Command is in below line,
1 |
php artisan migrate |
Step 4: Now we creating a route file for Route the URL or controller function.
So first we add resource root for Users crud project, open path: routes/web.php and add below route code.
routes/web.php
1 |
Route::resource('Users','UsersController'); |
Step 5: Now we Create Controller and Model
In this step first we create a controller named UsersController. For automatic creation we runt the below command,
1 |
php artisan make:controller UsersController --resource --model=Users |
On this command after resource part we create a model, this part “–resource –model=Users”
After bellow command we will find controller file in this path: app/Http/Controllers/UsersController.php.
Laravel have some methods for creating crud application, some are as follows on the below code,
So, copy the below code on UsersController.php file.
app/Http/Controllers/UsersController.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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
<?php namespace App\Http\Controllers; use App\Users; use Illuminate\Http\Request; class UsersController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $Users = Users::latest()->paginate(5); return view('Userss.index',compact('Userss')) ->with('i', (request()->input('page', 1) - 1) * 5); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('Users.create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $request->validate([ 'name' => 'required', 'detail' => 'required', ]); Users::create($request->all()); return redirect()->route('Userss.index') ->with('success','Users created successfully.'); } /** * Display the specified resource. * * @param \App\Users $Users * @return \Illuminate\Http\Response */ public function show(Users $Users) { return view('Userss.show',compact('Users')); } /** * Show the form for editing the specified resource. * * @param \App\Users $Users * @return \Illuminate\Http\Response */ public function edit(Users $Users) { return view('Users.edit',compact('Users')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Users $Users * @return \Illuminate\Http\Response */ public function update(Request $request, Users $Users) { $request->validate([ 'name' => 'required', 'detail' => 'required', ]); $Users->update($request->all()); return redirect()->route('Userss.index') ->with('success','Users updated successfully'); } /** * Remove the specified resource from storage. * * @param \App\Users $Users * @return \Illuminate\Http\Response */ public function destroy(Users $Users) { $Users->delete(); return redirect()->route('Userss.index') ->with('success','Users deleted successfully'); } } |
Ok, now open Users.php file and copy below code:
path: app/Users.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; //these are give the access to fields which fields are take data on database class Users extends Model { protected $fillable = [ 'name', 'detail' ]; } |
On next we create designing files, laravel use Blade template engine for creating design files like below which i defined.
Step 6: Creating design files using Blade template engine.(layout file)
In last step. In this step we have to create just blade files. So mainly we have to create layout file and then create new folder “users” then create blade files of crud app. So finally you have to create following bellow blade file:
Blade Files: layout.blade.php, index.blade.php, create.blade.php, edit.blade.php, show.blade.php
So now create these files and put below code.
path: resources/views/Users/layout.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<!DOCTYPE html> <html> <head> <title>Laravel 5.7 CRUD Project - codingtasks.net</title> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet"> </head> <body> <div class="container"> @yield('content') </div> </body> </html> |
resources/views/Userss/index.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 |
@extends('Users.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Laravel 5.7 CRUD Example - codingtasks.net</h2> </div> <div class="pull-right"> <a class="btn btn-success" href="{{ route('Users.create') }}"> Create New Users</a> </div> </div> </div> @if ($message = Session::get('success')) <div class="alert alert-success"> <p>{{ $message }}</p> </div> @endif <table class="table table-bordered"> <tr> <th>No</th> <th>Name</th> <th>Details</th> <th width="280px">Action</th> </tr> @foreach ($Userss as $Users) <tr> <td>{{ ++$i }}</td> <td>{{ $Users->name }}</td> <td>{{ $Users->detail }}</td> <td> <form action="{{ route('Userss.destroy',$Users->id) }}" method="POST"> <a class="btn btn-info" href="{{ route('Userss.show',$Users->id) }}">Show</a> <a class="btn btn-primary" href="{{ route('Userss.edit',$Users->id) }}">Edit</a> @csrf @method('DELETE') <button type="submit" class="btn btn-danger">Delete</button> </form> </td> </tr> @endforeach </table> {!! $Userss->links() !!} @endsection |
resources/views/Users/create.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 |
@extends('Users.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Add New Users</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('Users.index') }}"> Back</a> </div> </div> </div> @if ($errors->any()) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ route('Users.store') }}" method="POST"> @csrf <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> <input type="text" name="name" class="form-control" placeholder="Name"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Detail:</strong> <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail"></textarea> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 text-center"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> </form> @endsection |
resources/views/Users/edit.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 |
@extends('Users.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2>Edit Users</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('Users.index') }}"> Back</a> </div> </div> </div> @if ($errors->any()) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{ route('Users.update',$Users->id) }}" method="POST"> @csrf @method('PUT') <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> <input type="text" name="name" value="{{ $Users->name }}" class="form-control" placeholder="Name"> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Detail:</strong> <textarea class="form-control" style="height:150px" name="detail" placeholder="Detail">{{ $Users->detail }}</textarea> </div> </div> <div class="col-xs-12 col-sm-12 col-md-12 text-center"> <button type="submit" class="btn btn-primary">Submit</button> </div> </div> </form> @endsection |
resources/views/Users/show.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 |
@extends('Users.layout') @section('content') <div class="row"> <div class="col-lg-12 margin-tb"> <div class="pull-left"> <h2> Show Users</h2> </div> <div class="pull-right"> <a class="btn btn-primary" href="{{ route('Users.index') }}"> Back</a> </div> </div> </div> <div class="row"> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Name:</strong> {{ $Users->name }} </div> </div> <div class="col-xs-12 col-sm-12 col-md-12"> <div class="form-group"> <strong>Details:</strong> {{ $Users->detail }} </div> </div> </div> @endsection |
Now we complete the Crud project on laravel 5.7, open gitbash or terminal and run below command,
1 |
php artisan serve |
Open below URL on your browser:
http://localhost:8000/Users
[…] Laravel 5.7 CRUD application […]
[…] Laravel 5.7 CRUD application […]