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

时间:2018-04-23 10:35:06

标签: php database laravel post

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

当我试图将表单信息发布到数据库时,它似乎无法正常工作。我还希望将用户的id记录在数据库中,该数据库是食物日志表中的外键(来自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)
    {
        //
    }
}

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

1 个答案:

答案 0 :(得分:1)

您的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'
];