英文:
How to Save data to the Database using Laravel 8?
问题
这是ItemManufactureController文件
class ItemManufactureController extends Controller
{
public function index(){
return view('item_manufacture');
}
// 将类别数据保存到数据库中
public function store(Request $request){
$newManufacture = new ItemManufacture;
$newManufacture->name = $request->input('txtManufactureName');
$newManufacture->status = $request->input('status', 'available');
dd($newManufacture);
$newManufacture->save();
return redirect('/item_manufacture');
}
}
这是item_manufacture.blade.php文件
{{--将此页面添加到布局中--}}
@extends('layout.layout_01')
{{--从布局中标识内容--}}
@section('content')
<div class="container">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<div class="card">
<h5 class="card-header">添加项目制造详情</h5>
<div class="card-body">
<div class="input-field p-3">
<label for="txtManufactureName">制造商名称:</label>
<div class="col-sm-8 p-2">
<input type="text" placeholder="项目名称" name="txtManufactureName" id="txtManufactureName">
</div>
</div>
<div class="input-field p-3">
<div class="col-sm-8 p-2">
</div>
</div>
<a href="/save_manufacture" class="btn btn-primary mb-2" id="btnAdd">添加</a>
</div>
</div>
</div>
<div class="col-md-4"></div>
</div>
</div>
@endsection
这是路由文件
// 保存制造商
Route::get('/save_manufacture', [ItemManufactureController::class, 'store'])->name('saveManufacture');
Route::get('/item_manufacture', function (){
return view('pages.item_manufacture');
});
这是模型文件
class ItemManufacture extends Model
{
use HasFactory;
// public $timestamps=false;
protected $connection = 'mysql';
protected $primaryKey = 'id';
protected $table = 'item_manufacture';
protected $fillable = [
'name',
'status'];
}
我使用的是Laravel 8框架,当我将数据添加到item_manufacture表单的输入字段中时,数据不会传递到数组中。如果我的代码中有任何错误,请进行更正。
如何使用Laravel框架保存数据并获取输入字段的值?
英文:
This is ItemManufactureController file
class ItemManufactureController extends Controller
{
public function index(){
return view('item_manufacture');
}
// Save category data into the database
public function store(Request $request){
$newManufacture = new ItemManufacture;
$newManufacture->name = $request->input('txtManufactureName');
$newManufacture->status = $request->input('status', 'available');
dd($newManufacture);
$newManufacture->save();
return redirect('/item_manufacture');
}
}
This is item_manufacture.blade.php file
{{--this page add to layout --}}
@extends('layout.layout_01')
{{--identity the content form the layout--}}
@section('content')
<div class="container">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<div class="card">
<h5 class="card-header">Add Item Manufacture Details</h5>
<div class="card-body">
<div class="input-field p-3">
<label for="txtManufactureName">Manufacture Name :</label>
<div class="col-sm-8 p-2">
<input type="text" placeholder="Item Name" name="txtManufactureName" id="txtManufactureName">
</div>
</div>
<div class="input-field p-3">
<div class="col-sm-8 p-2">
</div>
</div>
<a href="/save_manufacture" class="btn btn-primary mb-2" id="btnAdd">ADD</a>
</div>
</div>
</div>
<div class="col-md-4"></div>
</div>
</div>
@endsection
This is route file
//save manufacture
Route::get('/save_manufacture', [ItemManufactureController::class, 'store'])->name('saveManufacture');
Route::get('/item_manufacture', function (){
return view('pages.item_manufacture');
});
This is Model file
class ItemManufacture extends Model
{
use HasFactory;
// public $timestamps=false;
protected $connection = 'mysql';
protected $primaryKey = 'id';
protected $table = 'item_manufacture';
protected $fillable = [
'name',
'status'];
}
when add data into the form and click "ADD" button array comes null value
I used Laravel 8 framework, when I add data into the input field of the item_manufacture form, data will not pass the array. If there any error of my code, Please correct it.
How to save data and get values from the input fields using Laravel framework?
答案1
得分: 1
请将您的路由设置为post
,因为您要存储数据,并在链式调用的name()
方法中更改路由为saveManufacture.store
。
Route::post('/save_manufacture', [ItemManufactureController::class, 'store'])->name('saveManufacture.store');
在您的Blade文件中,将输入包装在form
标签中,并在action
中设置命名路由。
然后,将a
标签(锚标签)替换为input
类型的submit
,因为我们在表单标签中添加了action
。因此,您的Blade文件将如下所示:
{{--此页面添加到布局--}}
@extends('layout.layout_01')
{{--标识来自布局的内容--}}
@section('content')
<div class="container">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<div class="card">
<h5 class="card-header">Add Item Manufacture Details</h5>
<div class="card-body">
<form action="{{ route('saveManufacture.store') }}" method="post">
<div class="input-field p-3">
<label for="txtManufactureName">Manufacture Name :</label>
<div class="col-sm-8 p-2">
<input type="text" placeholder="Item Name" name="txtManufactureName" id="txtManufactureName">
</div>
</div>
<div class="input-field p-3">
<div class="col-sm-8 p-2">
</div>
</div>
<input type="submit" class="btn btn-primary mb-2" id="btnAdd" value="ADD">
</form>
</div>
</div>
</div>
<div class="col-md-4"></div>
</div>
</div>
@endsection
现在,您可以在store()
函数中获取请求参数,请尝试使用dd($request->post());
进行调试。
英文:
Please make your route as post
since you're storing the data and change your route in chaining name()
method as saveManufacture.store
Route::post('/save_manufacture', [ItemManufactureController::class, 'store'])->name('saveManufacture.store');
And in your blade file wrap your inputs inside form
tag and set named route in your action.
And then replace a
tag (anchor tag) with input
type submit since we have added action in our form tag. so your blade file will look this.
{{--this page add to layout --}}
@extends('layout.layout_01')
{{--identity the content form the layout--}}
@section('content')
<div class="container">
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-4">
<div class="card">
<h5 class="card-header">Add Item Manufacture Details</h5>
<div class="card-body">
<form action="{{ route('saveManufacture.store') }}" method="post">
<div class="input-field p-3">
<label for="txtManufactureName">Manufacture Name :</label>
<div class="col-sm-8 p-2">
<input type="text" placeholder="Item Name" name="txtManufactureName" id="txtManufactureName">
</div>
</div>
<div class="input-field p-3">
<div class="col-sm-8 p-2">
</div>
</div>
<input type="submit" class="btn btn-primary mb-2" id="btnAdd" value="ADD">
</form>
</div>
</div>
</div>
<div class="col-md-4"></div>
</div>
</div>
@endsection
Now you'll able to get the request param in your store()
function, please try to debug dd($request->post());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论