Laravel Crud Tutorial From Scratch – Laravel Insert Update Delete

Laravel Crud Tutorial From Scratch – Laravel Insert Update Delete

In this Post We Will Explain About is Laravel Crud Tutorial From Scratch – Laravel Insert Update Delete With Example and Demo.Welcome on infinityknow.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Laravel 5.4 CRUD (Create Read Update Delete) Example from Scratch Example

In this post we will show you Best way to implement Simple Laravel CRUD with Resource Controllers , hear for Laravel 5 – Create Simple CRUD(Create Read Update Delete) with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Laravel 5.4 Crud Example From Scratch

Welcome, infinityknow.com, In today’s infinityknow Tutorial, I have Learn the Simple simple code of Laravel Crud Example From Scratch as well as steps by step. It is simple Laravel 5 Comments for beginners.

Overview Laravel 5 Crud Example From Scratch

1 Laravel
2 Phase 1: Make a Laravel project
3 Phase 2: Edit .env simple file data for database configuration
4 Phase 3: Use migrations Available by laravel In to some Make users and password_resets table
5 Phase 4: Make a controller simple file data for our CRUD operations.
6 Phase 5: Make a model simple file data for CRUD operations.
7 Phase 6: Edit crud migration simple file data and Make the Simple required fields for the Simple database.
8 Phase 7: Make views for set up a form
9 Phase 8: Make a form in create.blade.php
10 Phase 9: Setup a route for the Simple request handling.
11 Phase 10: Edit SimpleCRUDCtrl File
12 Phase 11: Add CSRF token and set the Simple comments route of the Simple form.
13 Phase 12: Source Code the Simple DataAdd function and use Crud model In to some insert the Simple data
14 Phase 13: Source Code index() function in the Simple SimpleCRUDCtrl File.
15 Phase 14: Need In to some update index.blade.php
16 Phase 15: Add Edit and Delete Button in the Simple index.blade.php
17 Phase 16: Make an edit function In to some pass the Simple data In to some the Simple edit view.
18 Phase 17: Make an edit view.
19 Phase 18: Source Code update() in the Simple SimpleCRUDCtrl.
20 Phase 19: Make a delete form In to some delete the Simple data.
21 Phase 20: Source Code the Simple destroy() method in the Simple SimpleCRUDCtrl.
22 Output
22.1 Github: <a href="https://github.com/KrunalLathiya/Laravel54">Github Download</a>
22.2 Steps In to some use Github code

Laravel

For creating Laravel simple Project, We need In to some have below following simple some things Like as a.

1. Latest PHP >= 5.6.4
2. simple OpenSSL PHP Extension
3. PDO version PHP Extension
4. Mbstring of the Simple PHP Extension
5. Tokenizer PHP simple Extension
6. XML some PHP Extension
7. Setup Composer

Phase 1: Make a Laravel project

Type below following simple command in our CMD(terminal) Command Prompt In to some create a project.

composer create-project --prefer-dist laravel/laravel Laravel5.4

It shall take 2 minutes In to some install a brand new Laravel Framework

and then installing, After that, Now our project root Dirctory and type php artisan serve in CMD(terminal) Command Prompt, our project URL might look like It’s

http://localhost:8000

Now open that laravel project Dirctory in our favorite editor Like as a Notepad++.

Phase 2: Edit .env simple file data for database setting means configuration

In our project root Dirctory, there is a simple file data called .env, Extentions, which we need In to some edit In to some set up a database setting means configuration. We Are using MySQL database.

READ :  Laravel check User online offline

//setup .env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel5.4
DB_USERNAME=root
DB_PASSWORD=mysql

Phase 3: Use migrations Available by laravel In to some Make users and password_resets table

Now After that, Now CMD(terminal) Command Prompt, and run below following simple command

php artisan migrate

If We have Making the Simple database in simple MySQL and We have Available correct setting means credentials In to some the Simple .env simple file data, then We shall learn In to some there are created simple three tables generated in MySQL dynemically database.

Phase 4: Make a controller simple file data for our Laravel CRUD operations.

After That CMD(terminal) Command Prompt, type the Simple below following simple command in our root project.

php artisan create:controller SimpleCRUDCtrl --resource

Now After that, Now laravel5.4 Versions > app > Http > Controllers, We shall learn In to some SimpleCRUDCtrl simple file data in that Dirctory.

This simple file data has all the Simple boilerplate some needed for our Crud operations using Laravel.

Phase 5: Make a model simple file data for CRUD operations.

In the Simple CMD(terminal) Command Prompt, type the Simple below following simple command in our root project.

php artisan create:model Crud -m

This command shall also create a migration simple file data for our CRUD operations.

Phase 6: Edit Laravel crud Operations migration simple file data and make the Simple required fields for the Simple database.

Migration database simple file data is path ti located in laravel5.4 Versions -> database -> migrations Dirctory.

increments('id');
            $table->string('subject');
            $table->string('comments');
            $table->timestamps();
        });
    }

    /**
     * Devloped By infinityknow.com Reverse the Simple migrations.
     *
     * Devloped By infinityknow.com @return void
     */
    public function down()
    {
        Schema::dropIfExists('cruds');
    }
}

Run the Simple below following simple command in our CMD(terminal) Command Prompt.

php artisan migrate

Phase 7: Make views for set up a HTML form

After That laravel5.4 Versions -> resources -> views . Locate into that Dirctory and then make a master view called master.blade.php. A create blade is templating some engine used by laravel.

<!--Devloped By infinityknow.com master.blade.php -->



    
        
        
        

        <title>Devloped By infinityknow.com CRUD Operations</title>

        <!--Devloped By infinityknow.com Fonts -->
        
    
    
        <br><br>
        @yield('content')
    

Now Make a new Dirctory inside views directory called crud. Go inside that Dirctory and Make below following simple files data

index.blade.php
create.blade.php
edit.blade.php

Phase 8: Make a form in create.blade.php

<!-- create.blade.php -->

@extends('master')
@section('content')
<div class="container">
  <form>
    <div class="form-group row">
      <label for="liveFormGroupData" class="col-md-2 col-form-label col-form-label-lg">Title</label>
      <div class="col-md-10">
        
      </div>
    </div>
    <div class="form-group row">
      <label for="smFormGroupInput" class="col-md-2 col-form-label col-form-label-md">Comments</label>
      <div class="col-md-10">
        <textarea name="comments" rows="8" cols="80"></textarea>
      </div>
    </div>
    <div class="form-group row">
      <div class="col-md-2"></div>
      
    </div>
  </form>
</div>
@endsection

Phase 9: Setup a route for the Simple some request handling.

After That the Simple routes -> web.php

welcome');
});
Route::resource('crud', 'SimpleCRUDCtrl');

Here we have added some resource route, and all the Simple functions reside in app -> Http -> controllers -> SimpleCRUDCtrl

Phase 10: Edit SimpleCRUDCtrl File

//Devloped By infinityknow.com SimpleCRUDCtrl.php

  /**
     *Devloped By infinityknow.com Show the Simple form for creating a new resource.
     *
     *Devloped By infinityknow.com @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('crud.create');
    }

Here we have In to some return the Simple view when the Simple request hits the Simple route; it shall be redirected In to some It’s controller’s make method. Our view should be accessible via below following simple URL the Simple form.

http://localhost:8000/crud/create

We are now able In to some learn In to some the Simple form with the Simple two fields.

Phase 11: Add CSRF some token and set the Simple comments route of the Simple form.

<!-- create.blade.php -->

@extends('master')
@section('content')
<div class="container">
  <form method="comments" action="{{url('crud')}}">
    <div class="form-group row">
      {{csrf_field()}}
      <label for="liveFormGroupData" class="col-md-2 col-form-label col-form-label-lg">Title</label>
      <div class="col-md-10">
        
      </div>
    </div>
    <div class="form-group row">
      <label for="smFormGroupInput" class="col-md-2 col-form-label col-form-label-md">Comments</label>
      <div class="col-md-10">
        <textarea name="comments" rows="8" cols="80"></textarea>
      </div>
    </div>
    <div class="form-group row">
      <div class="col-md-2"></div>
      
    </div>
  </form>
</div>
@endsection

We have put the Simple {{csrf_field()}} in the Simple form so that malicious attack could not harm our web application.

READ :  Angular 6 Tutorial with Top 10 Examples

Phase 12: Source Code the Simple DataAdd function and use Crud model In to some insert the Simple data

 $request->get('subject'),
          'comments' => $request->get('comments')
        ]);

        $liveCrud->save();
        return redirect('/crud');
    }

    /**
     *Devloped By infinityknow.com Display the Simple specified resource.
     *
     *Devloped By infinityknow.com @param  int  $id
     *Devloped By infinityknow.com @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //Devloped By infinityknow.com
    }

    /**
     *Devloped By infinityknow.com Show the Simple form for editing the Simple specified resource.
     *
     *Devloped By infinityknow.com @param  int  $id
     *Devloped By infinityknow.com @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //Devloped By infinityknow.com
    }

    /**
     *Devloped By infinityknow.com Update the Simple specified resource in storage.
     *
     *Devloped By infinityknow.com @param  \Illuminate\Http\Request  $request
     *Devloped By infinityknow.com @param  int  $id
     *Devloped By infinityknow.com @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //Devloped By infinityknow.com
    }

    /**
     *Devloped By infinityknow.comDevloped By infinityknow.com Remove the Simple specified resource from storage.
     *
     *Devloped By infinityknow.com @param  int  $id
     *Devloped By infinityknow.com @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //Devloped By infinityknow.com
    }
}

Here we need In to some create a protected field called $fillable in the Simple Crud model. Otherwise, it shall throw a mass assignment exception.

//Devloped By infinityknow.com Crud.php
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Crud extends Model
{
    protected $fillable = ['subject','comments'];
}

Now, when We fill the Simple subject and comments field in our form and submit the Simple form, the Simple new entry shall be added In to some the Simple database. We could confirm it by below following simple steps.

Phase 13: Source Code index() function in the Simple SimpleCRUDCtrl File.

//Devloped By infinityknow.com CrudController.php

toArray();
        
        return view('crud.index', compact('cruds'));
    }

    /**
     *Devloped By infinityknow.com Show the Simple form for creating a new resource.
     *
     *Devloped By infinityknow.com @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('crud.create');
    }

    /**
     *Devloped By infinityknow.com Store a newly created resource in storage.
     *
     *Devloped By infinityknow.com @param  \Illuminate\Http\Request  $request
     *Devloped By infinityknow.com @return \Illuminate\Http\Response
     */
    public function DataAdd(Request $request)
    {
        $liveCrud = new Crud([
          'subject' => $request->get('subject'),
          'comments' => $request->get('comments')
        ]);

        $liveCrud->save();
        return redirect('/crud');
    }

    /**
     *Devloped By infinityknow.com Display the Simple specified resource.
     *
     *Devloped By infinityknow.com @param  int  $id
     *Devloped By infinityknow.com @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //Devloped By infinityknow.com
    }

    /**
     * Show the Simple form for editing the Simple specified resource.
     *
     *Devloped By infinityknow.com @param  int  $id
     *Devloped By infinityknow.com @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //Devloped By infinityknow.com
    }

    /**
     *Devloped By infinityknow.com Update the Simple specified resource in storage.
     *
     *Devloped By infinityknow.com @param  \Illuminate\Http\Request  $request
     *Devloped By infinityknow.com @param  int  $id
     *Devloped By infinityknow.com @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //Devloped By infinityknow.com
    }

    /**
     *Devloped By infinityknow.com Remove the Simple specified resource from storage.
     *
     *Devloped By infinityknow.com @param  int  $id
     *Devloped By infinityknow.com @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //Devloped By infinityknow.com
    }
}

Phase 14: Need In to some update index.blade.php

<!--Devloped By infinityknow.com index.blade.php -->
@extends('master')
@section('content')
  <div class="container">
    <table class="table table-striped">
    <thead>
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Comments</th>
      </tr>
    </thead>
    <tbody>
      @foreach($cruds as $data_val)
      <tr>
        <td>{{$comments['id']}}</td>
        <td>{{$comments['subject']}}</td>
        <td>{{$comments['comments']}}</td>
      </tr>
      @endforeach
    </tbody>
  </table>
  </div>
@endsection

Next, shoot the Simple below following simple URL.

READ :  Vue js array Updating - VueJS update object properties

http://localhost:8000/crud

You shall learn In to some a table which has the Simple id, subject and comments column with their respective data.

Phase 15: Add Edit and Delete Button in the Simple index.blade.php

<!--Devloped By infinityknow.com index.blade.php -->

@extends('master')
@section('content')
  <div class="container">
    <table class="table table-striped">
    <thead>
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Comments</th>
        <th colspan="2">Action</th>
      </tr>
    </thead>
    <tbody>
      @foreach($cruds as $data_val)
      <tr>
        <td>{{$comments['id']}}</td>
        <td>{{$comments['subject']}}</td>
        <td>{{$comments['comments']}}</td>
        <td><a href="{{action('[email protected]', $data_val['id'])}}" class="btn btn-warning">Edit</a></td>
        <td><a href="{{action('[email protected]', $data_val['id'])}}" class="btn btn-danger">Delete</a></td>
      </tr>
      @endforeach
    </tbody>
  </table>
  </div>
@endsection

Phase 16: Make an edit function In to some pass the Simple data In to some the Simple edit view.

<?php

//Devloped By infinityknow.com SimpleCRUDCtrl.php  
/**
     *Devloped By infinityknow.com Show the Simple form for editing the Simple specified resource.
     *
     *Devloped By infinityknow.com @param  int  $id
     *Devloped By infinityknow.com @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $liveCrud = Crud::find($id);
        
        return view('crud.edit', compact('crud','id'));

    }

Phase 17: Make an edit view.

<!-- edit.blade.php -->

@extends('master')
@section('content')
<div class="container">
  <form method="comments" action="{{action('[email protected]', $id)}}">
    <div class="form-group row">
      {{csrf_field()}}
       
      <label for="liveFormGroupData" class="col-md-2 col-form-label col-form-label-lg">Title</label>
      <div class="col-md-10">
        subject}}">
      </div>
    </div>
    <div class="form-group row">
      <label for="smFormGroupInput" class="col-md-2 col-form-label col-form-label-md">Comments</label>
      <div class="col-md-10">
        <textarea name="comments" rows="8" cols="80">{{$crud->comments}}</textarea>
      </div>
    </div>
    <div class="form-group row">
      <div class="col-md-2"></div>
      <button type="submit" class="btn btn-success">Update</button>
    </div>
  </form>
</div>
@endsection

Here we have used one more hidden field called _method, which shall be a PATCH request In to some the Simple server so that we could update the Simple data very quickly.

Phase 18: Source Code update() in the Simple SimpleCRUDCtrl.

subject = $request->get('subject');
        $liveCrud->comments = $request->get('comments');
        $liveCrud->save();
        return redirect('/crud');
    }

and then filling the Simple update form, we could learn In to some the Simple index page that our data is updated. Therefore now only Delete functionality is remaining.

Phase 19: Make a delete form In to some delete the Simple data.

<!--infinityknow.com index.blade.php -->

@extends('master')
@section('content')
  <div class="container">
    <table class="table table-striped">
    <thead>
      <tr>
        <th>ID</th>
        <th>Title</th>
        <th>Comments</th>
        <th colspan="2">Action</th>
      </tr>
    </thead>
    <tbody>
      @foreach($cruds as $data_val)
      <tr>
        <td>{{$comments['id']}}</td>
        <td>{{$comments['subject']}}</td>
        <td>{{$comments['comments']}}</td>
        <td><a href="{{action('[email protected]', $data_val['id'])}}" class="btn btn-warning">Edit</a></td>
        <td>
          <form action="{{action('[email protected]', $data_val['id'])}}" method="comments">
            {{csrf_field()}}
            
            <button class="btn btn-danger" type="submit">Delete</button>
          </form>
        </td>
      </tr>
      @endforeach
    </tbody>
  </table>
  </div>
@endsection

Phase 20: Source Code the Simple destroy() method in the Simple SimpleCRUDCtrl.

delete();

      return redirect('/crud');
    }

Here in delete functionality, we have not put the Simple any confirm dialog box, but it is very okay for the Simple demo. We just want We In to some data take It’s live demo and learn In to some how the Simple laravel’ flow step by step is working.

Output

There is some routes list in resource controller. Type in the Simple CMD(terminal) Command Prompt: php artisan route:list

If We are curious how some resource controller works data behind the Simple scenes,and then It’s is very our answer.

Github:

Github code

Steps In to some use Github code

  • Clone the Simple repo in our local.
  • After That root of the Simple project and run command “composer update“
  • Edit .env simple file data and use our MySQL database credentials.
  • Run the Simple command “php artisan migrate“
  • Now, we need In to some bootstrap Laravel server so run “php artisan serve“
  • If now After that, Now It’s URL: http://localhost:8000/crud/create

Example

I hope you have Got What is how to use insert update delete in laravel 5.4 And how it works.I would Like to have FeadBack From My Blog(infinityknow.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(infinityknow.com) Are Most Always Welcome.

https://pakainfo.com/

Add a Comment

Your email address will not be published. Required fields are marked *