Laravel Form methods VS traditional coding
Using built-in HTML helpers have many benefits:
Using
Form::open
you add CSRF protection input hidden (by default)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 codingIf 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 itAlso 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
Laravel 5.1 Date_Format Validation Allow Two Formats
Table Name as Parameter Using Pdo/MySQL Prepared Statement
Number_Format() Causes Error "A Non Well Formed Numeric Value Encountered"
Make Text Between Asterisks Bold
Regular Expression for Checking Website Url
How to Insert Multiple Rows in PHP Pdo MySQL
How to Add a View Helper Directory (Zend Framework)
Call Parent Constructor Before Child Constructor in PHP
How to Get the List of Available Locales in PHP
Comparing Floats - Same Number, But Does Not Equal
How to Disable PHP Magic Quotes at Runtime
Php: Most Frequent Value in Array
SQL Query with Binary Data (PHP and MySQL)
Get Order Increment Id in Magento
Extract Dom-Elements from String, in PHP
Swap Two Words in a String PHP
PHP Float Calculation 2 Decimal Point
PHP Laravel: No Connection Could Be Made Because the Target MAChine Actively Refused It