Laravel Form Methods VS Traditional Coding

Laravel Form methods VS traditional coding

Using built-in HTML helpers have many benefits:

  1. Using Form::open you add CSRF protection input hidden (by default)

  2. Using form elements (inputs/textarea etc.) and withInput method for Redirection allows you to easily fill in the form with the same data with almost no coding

    If you use Redirect::route('form'->withInput(); and have input
    text {{Form::text('username')}} it will automatically set input's value the old data - you don't need to code it yourself checking it

  3. Also if you want to match fields with labels its much easier:

    {{ Form::label('username', 'Enter username') }}
    {{ Form::text('username') }}

    it will generate the following code:

    <label for="username">Enter username</label>
    <input name="username" type="text" id="username">

    so as you see id will be created automatically

Probably there are some more. However the main disadvantage is that you need to learn and it's not portable in case you want to move your site to other Framework but each solution has pros and cons.

What's the best approach for data validation between forms and models on Laravel 5?

In the end, the best way I found to solve this matter was stitching together two Laravel extensions: Ardent, an Eloquent extension that includes validation, and a fork of Laravalid, that extends the Form with jQuery basic validation: that fork includes Ardent integration.

Laravel 7 conditional form method POST | PUT

You need to condition the @method('PUT') as well like below:

<form action="{{ isset($category) ? route('categories.update',$category->id) : route('categories.store') }}" method="POST">
@csrf
@if(isset($category))
@method('PUT')
@endif
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" class="form-control" name="name" value="{{ isset($category) ? $category->name : '' }}">
</div>

<div class="form-group">
<button class="btn btn-success">
{{ isset($category) ? 'Update category' : 'Add category' }}
</button>
</div>
</form>

Laravel form insert in one go

About the button

The reason why your button is part of your submission is that you put the attribute name.

See here for reference.

You can test with this:

<?php var_dump($_POST); ?>
<form action="index.php" method="post" id="form1">
<button type="submit" name="test" value="Submit1">Submit A</button>
<button type="submit" value="Submit2">Submit B</button>
</form>

About creating entries in the db

What you are trying to do in your code is called Massive Assignment and is really dangerous and insecure.

So as suggested you should use your model using the Product::create().

In addition to that in the Model Product you have to insert the property $fillable.

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model {
// this is just an example, modify this part in accord to your needs
protected $fillable = [`name`, `url_key`, `sku`, `quantity`, `price`, `specialprice`, `description`, `preorder`, `page_title`, `header_data`, `custom_json_data`, `category_id`];

// other code here
}


Related Topics



Leave a reply



Submit