如何使用Laravel 8将数据保存到数据库?

huangapple go评论67阅读模式
英文:

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(&#39;item_manufacture&#39;);
}
// Save category data into the database
public function store(Request $request){
$newManufacture = new ItemManufacture;
$newManufacture-&gt;name = $request-&gt;input(&#39;txtManufactureName&#39;);
$newManufacture-&gt;status = $request-&gt;input(&#39;status&#39;, &#39;available&#39;);
dd($newManufacture);
$newManufacture-&gt;save();
return redirect(&#39;/item_manufacture&#39;);
}
}

This is item_manufacture.blade.php file

{{--this page add to layout   --}}
@extends(&#39;layout.layout_01&#39;)
{{--identity the content form the layout--}}
@section(&#39;content&#39;)
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-md-4&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;col-md-4&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h5 class=&quot;card-header&quot;&gt;Add Item Manufacture Details&lt;/h5&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;div class=&quot;input-field p-3&quot;&gt;
&lt;label for=&quot;txtManufactureName&quot;&gt;Manufacture Name :&lt;/label&gt;
&lt;div class=&quot;col-sm-8 p-2&quot;&gt;
&lt;input type=&quot;text&quot; placeholder=&quot;Item Name&quot; name=&quot;txtManufactureName&quot; id=&quot;txtManufactureName&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;input-field p-3&quot;&gt;
&lt;div class=&quot;col-sm-8 p-2&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;a href=&quot;/save_manufacture&quot; class=&quot;btn btn-primary mb-2&quot; id=&quot;btnAdd&quot;&gt;ADD&lt;/a&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-4&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
@endsection

This is route file


//save manufacture
Route::get(&#39;/save_manufacture&#39;, [ItemManufactureController::class, &#39;store&#39;])-&gt;name(&#39;saveManufacture&#39;);
Route::get(&#39;/item_manufacture&#39;, function (){
return view(&#39;pages.item_manufacture&#39;);
});

This is Model file

class ItemManufacture extends Model
{
use HasFactory;
//    public $timestamps=false;
protected $connection = &#39;mysql&#39;;
protected $primaryKey = &#39;id&#39;;
protected $table = &#39;item_manufacture&#39;;
protected $fillable = [
&#39;name&#39;,
&#39;status&#39;];
}

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(&#39;/save_manufacture&#39;, [ItemManufactureController::class, &#39;store&#39;])-&gt;name(&#39;saveManufacture.store&#39;);

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(&#39;layout.layout_01&#39;)
{{--identity the content form the layout--}}
@section(&#39;content&#39;)
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-md-4&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;col-md-4&quot;&gt;
&lt;div class=&quot;card&quot;&gt;
&lt;h5 class=&quot;card-header&quot;&gt;Add Item Manufacture Details&lt;/h5&gt;
&lt;div class=&quot;card-body&quot;&gt;
&lt;form action=&quot;{{ route(&#39;saveManufacture.store&#39;) }}&quot; method=&quot;post&quot;&gt;
&lt;div class=&quot;input-field p-3&quot;&gt;
&lt;label for=&quot;txtManufactureName&quot;&gt;Manufacture Name :&lt;/label&gt;
&lt;div class=&quot;col-sm-8 p-2&quot;&gt;
&lt;input type=&quot;text&quot; placeholder=&quot;Item Name&quot; name=&quot;txtManufactureName&quot; id=&quot;txtManufactureName&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;input-field p-3&quot;&gt;
&lt;div class=&quot;col-sm-8 p-2&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;input type=&quot;submit&quot; class=&quot;btn btn-primary mb-2&quot; id=&quot;btnAdd&quot; value=&quot;ADD&quot;&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-4&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
@endsection

Now you'll able to get the request param in your store() function, please try to debug dd($request-&gt;post());

huangapple
  • 本文由 发表于 2023年6月1日 18:36:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76381015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定