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

Php 如何使用laravel将表单中的数据发布到数据库中?,php,database,laravel,post,Php,Database,Laravel,Post,我正在做一个大学项目,这是一个foodlog,用户可以登录,然后记录他们的食物项目。理想情况下,这将发布到数据库,然后用户将能够查看表中显示的所有记录的食物 目前,我正试图将表单信息发布到数据库中,但它似乎不起作用。我还希望将用户id记录到数据库中,该数据库是food log表(来自users表)中的外键 这是我的PostsController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Reque

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

目前,我正试图将表单信息发布到数据库中,但它似乎不起作用。我还希望将用户id记录到数据库中,该数据库是food log表(来自users表)中的外键

这是我的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)
    {
        //
    }
}

您的
存储
功能应该如下所示:

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');

}

非常感谢!你知道为什么说userid需要是一个可填充的字段吗?我希望在用户登录时将其分配给用户?您应该删除
'id'=>$request->id
,然后它就可以工作了。因为它是自动递增的。之后,您可以在用户登录时访问该id。:)如果解决方案有帮助,请提交:)我已经更新了我的答案。如果有任何错误表明
某些表需要是可填充字段
,那么只需将该字段添加到model->filleble数组中即可
<?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');
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    //
}
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');

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