title: How do I post data from a form into a database with laravel?
body: <p>Im doing a university project which is a foodlog, where the user is able to log in and then log their food items.  Ideally this will post to the database, then the user will be able to view all their logged foods displayed in a table. </p>
  
  <p>at the minute im trying to post the form information to the database and it doesnt seem to work.  I also want the user's id to be logged in the database which is a foreign key in the food log table (from users table).  </p>
  
  <p>This is my PostsController.php</p>
  
  <pre><code>    &lt;?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'-&gt;request('id');
              'user_id'-&gt;Auth::user()-&gt;id;
              'name'-&gt;request('name');
              'servings'-&gt;request('servings');
              'servingsize'-&gt;request('servingsize');
              'calories'-&gt;request('calories');
              'fat'-&gt;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)
      {
          //
      }
  }
  </code></pre>
  
  <p>This is my add.blade.php</p>
  
  <pre><code>    @extends('layouts.app')
  
  &lt;!-- Styles --&gt;
      &lt;link href="{{ asset('css/app.css') }}" rel="stylesheet"&gt;
  
  @section('content')
  &lt;div class="container"&gt;
      &lt;div class="row justify-content-center"&gt;
          &lt;div class="col-md-8"&gt;
  
  
  
                      &lt;form method="POST" action="/posts"&gt;
  
          {{ csrf_field() }}
  
          &lt;h1&gt;Add to Foodlog&lt;/h1&gt;
          &lt;div class="form-group"&gt;
  
              &lt;label for="item"&gt;Item:&lt;/label&gt;
  
              &lt;input type="text" id="name" name="name" class="form-control"&gt;
  
          &lt;/div&gt;
  
  
  
          &lt;div class="form-group"&gt;
  
                      &lt;label for="noOfServings"&gt;Number of Servings:&lt;/label&gt;
  
                      &lt;input type="number" id="servings" name="servings" class="form-control"&gt;
  
          &lt;/div&gt;
  
  
  
          &lt;div class="form-group"&gt;
  
                      &lt;label for="servingSize"&gt;Serving Size (g):&lt;/label&gt;
  
                      &lt;input type="number" id="servingsize" name="servingsize" class="form-control"&gt;
  
          &lt;/div&gt;
  
  
  
          &lt;div class="form-group"&gt;
  
                      &lt;label for="calories"&gt;Calories (kcal):&lt;/label&gt;
  
                      &lt;input type="number" id="calories" name="calories" class="form-control"&gt;
  
          &lt;/div&gt;
  
  
  
          &lt;div class="form-group"&gt;
  
                      &lt;label for="fat"&gt;Fat (g):&lt;/label&gt;
  
                      &lt;input type="number" id="fat" name="fat" class="form-control"&gt;
  
          &lt;/div&gt;
  
  
          &lt;button type="submit" class="btn btn-primary"&gt;Add&lt;/button&gt;
  
  
  
  
      &lt;/form&gt;
  
  
  
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  @endsection
  </code></pre>
  
  <p>And my web.php</p>
  
  <pre><code>&lt;?php
  
  Route::get('/', 'FoodlogController@index');
  
  Route::get('/add', 'FoodlogController@add');
  
  Route::get('/account', 'FoodlogController@account');
  
  
  Auth::routes();
  
  Route::get('/home', 'HomeController@index')-&gt;name('home');
  
  Route::get('/foodlog', function() {
  
  
      return view('/foodlog');
  
  });
  
  
  Route::get('/logout', function(){
     Auth::logout();
     return Redirect::to('welcome');
  });
  
  
  Route::post('/posts', 'PostsController@store');
  </code></pre>
  
  <p>I have also included a screenshot of my create foodlog migrations file so that you can see the fields in my table</p>
  
  <p>Also my Post model is empty as you can see </p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Post extends Model
  {
      //
  }
  </code></pre>