gpt4 book ai didi

php - 如何使用 laravel 将表单中的数据发布到数据库中?

转载 作者:搜寻专家 更新时间:2023-10-30 23:30:18 25 4
gpt4 key购买 nike

我正在做一个大学项目,它是一个食物日志,用户可以在其中登录然后记录他们的食物。理想情况下,这将发布到数据库,然后用户将能够查看表格中显示的所有记录的食物。

我正在尝试将表单信息发布到数据库,但似乎没有用。我还希望将用户的 ID 记录在数据库中,该数据库是食物日志表(来自用户表)中的外键。

这是我的 PostsController.php

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Post;

class PostsController 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()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store()
{

Post::create([

'id'->request('id');
'user_id'->Auth::user()->id;
'name'->request('name');
'servings'->request('servings');
'servingsize'->request('servingsize');
'calories'->request('calories');
'fat'->request('fat');

]);

return redirect('/foodlog');

}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}

这是我的 add.blade.php

    @extends('layouts.app')

<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">

@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">



<form method="POST" action="/posts">

{{ csrf_field() }}

<h1>Add to Foodlog</h1>
<div class="form-group">

<label for="item">Item:</label>

<input type="text" id="name" name="name" class="form-control">

</div>



<div class="form-group">

<label for="noOfServings">Number of Servings:</label>

<input type="number" id="servings" name="servings" class="form-control">

</div>



<div class="form-group">

<label for="servingSize">Serving Size (g):</label>

<input type="number" id="servingsize" name="servingsize" class="form-control">

</div>



<div class="form-group">

<label for="calories">Calories (kcal):</label>

<input type="number" id="calories" name="calories" class="form-control">

</div>



<div class="form-group">

<label for="fat">Fat (g):</label>

<input type="number" id="fat" name="fat" class="form-control">

</div>


<button type="submit" class="btn btn-primary">Add</button>




</form>



</div>
</div>
</div>
@endsection

还有我的 web.php

<?php

Route::get('/', 'FoodlogController@index');

Route::get('/add', 'FoodlogController@add');

Route::get('/account', 'FoodlogController@account');


Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/foodlog', function() {


return view('/foodlog');

});


Route::get('/logout', function(){
Auth::logout();
return Redirect::to('welcome');
});


Route::post('/posts', 'PostsController@store');

我还包含了我创建的 foodlog 迁移文件的屏幕截图,以便您可以看到我表中的字段

如您所见,我的 Post 模型也是空的

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
//
}

最佳答案

您的store 函数应该如下所示:

public function store(Request $request)
{

Post::create([

'id' => $request->id, //<--- if this is autoincrement then just delete this line
'user_id' => Auth::user()->id,
'name' => $request->name,
'servings' => $request->servings,
'servingsize' => $request->servingsize,
'calories' => $request->calories,
'fat' => $request->fat
]);

return redirect('/foodlog');

}

对于表之间的关系,请遵循此 link

添加到 Post 模型可填写字段:

protected $fillable = [
//if id is not autoincrement then add 'id'
'user_id',
'name',
'servings',
'servingsize',
'calories',
'fat'
];

关于php - 如何使用 laravel 将表单中的数据发布到数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49978962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com