Home » Php » php – How can I make prevent multiple additions server-side – Laravel?

php – How can I make prevent multiple additions server-side – Laravel?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I want only one entry in this table. Multiple records are created when the user clicks the button several times. I can do this client-side, no problem. But wouldn’t it be safer to do it server-side. How can I wait for your help.

When I do it with javascript, the user can right click and examine and remove the code. So I want to make it server-side and secure it.

Do you think it is enough for me to do this only on the client side?

My seos table below

id|site_name  |site_description          | site_keywords                |
1 |Fibonaccci |Site Description Here...  | keyword, keyword1, keyword2  |




@extends('layouts.admin-panel.master')

@section('title', 'Fibonacci - Seo')

@section('content')

    <div class="content">
        <div class="page-inner">
            <div class="page-header">
                <h4 class="page-title">Seo</h4>
                <ul class="breadcrumbs">
                    <li class="nav-home">
                        <a href="#">
                            <i class="flaticon-home"></i>
                        </a>
                    </li>
                    <li class="separator">
                        <i class="flaticon-right-arrow"></i>
                    </li>
                    <li class="nav-item">
                        <a href="#">Seo</a>
                    </li>
                </ul>
            </div>

            <div class="row">
                <div class="col-md-12">
                    <div class="card">
                        <div class="card-header">
                            <h4 class="card-title">Seo</h4>
                        </div>
                        <div class="card-body">

                                <form action="{{ route('seo.store') }}" method="POST" >
                                @csrf
                                    <div class="row">
                                        <div class="col-md-12">
                                            <div class="form-group p-0 margin-bottom-20 mt-0">
                                                <label for="name">Site Title (Characters left: 70)<span class="text-red">*</span></label>
                                                <input id="name" type="text" name="site_name" class="form-control"   placeholder="Title must be within 70 Characters" required>
                                            </div>
                                        </div>
                                        <div class="col-xl-6">
                                            <div class="form-group p-0 margin-bottom-16 mt-0">
                                                <label for="desc">Site Description (Characters left: 150)<span class="text-red">*</span></label>
                                                <textarea id="desc"  name="site_description" class="form-control" rows="5" placeholder="Description must be within 150 Characters" required></textarea>
                                            </div>
                                        </div>
                                        <div class="col-xl-6">
                                            <div class="form-group p-0 margin-bottom-16 mt-0">
                                                <label for="keyword">Site Keywords (Separate with commas)<span class="text-red">*</span></label>
                                                <textarea id="keyword"  name="site_keywords" class="form-control" rows="5" placeholder="keyword1, keyword2, keyword3" required></textarea>
                                            </div>
                                        </div>
                                        <div class="col-12">
                                            <button type="submit" class="btn btn-success">Create</button>
                                        </div>
                                    </div>
                                </form>

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

@endsection
How to&Answers:

You can use the ‘unique’ constraint in the form validation.

https://laravel.com/docs/6.x/validation#rule-unique

 $request->validate([
        'site_name'   =>  'required|max:70|unique:App\Site,site_name',
         ...
    ]);

Answer:

My solution here. I would love to find out if you have a better method.

SeoController.php

class SeoController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // Retrieving a model
        $seo = Seo::first();

        return view('admin-panel.seo.create', compact('seo'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        // Form validation
        $request->validate([
            'site_name'   =>  'required|max:70',
            'site_description'   =>  'required|max:150',
            'site_keywords'   =>  'required',
        ]);

        // Get All Request
        $input = $request->all();

        $count_rows = Seo::all()->count();

        if($count_rows == 0){
            // Record to database
            Seo::create($input);

            return redirect()->route('seo.create')
                ->with('success','Created  successfully.');
        }else {
            return redirect()->route('seo.create')
                ->with('success','Created  successfully.');
        }
    }

  }

My other solution. Is it possible to provide complete protection with this solution. For example, when the form tag is removed, the user is not able to submit it already. When it is not removed, it will always work. Say what.

$(function(){
        $('form').on('submit', function () {
            $(this).find(':submit').attr('disabled', 'true');
        })
    });