我的数据没有保存到数据库,我没有收到错误

时间:2018-03-23 08:12:29

标签: laravel model-view-controller eloquent laravel-eloquent

我有一个问题,这个代码只是执行而vardump显示var i放在文本字段中但是没有数据显示在数据库中,也没有出现错误

视图:

<!DOCTYPE html>
<html>
    <head>
        <link href="https://fonts.googleapis.com/css?family=Lato:100" rel="stylesheet" type="text/css">
       <body>
           <div>
               <form method="post" action="/post">
                   {{ csrf_field() }}
                   <input type="text" name="email">
                   <input type="text" name="password">
                   <input type="submit" name="submit">
               </form>
           </div>
       </body>        
    </head>
</html>

控制器:

  <?php
    namespace App\Http\Controllers;
    use Illuminate\Http\Request;
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    use App\werknemer_model;

    class werknemer extends Controller
    {
        /*Show the form for creating a new resource.*/
        public function create(Request $request)
        {
            $email = $request->input('email');
            var_dump($email);
            $werknemer = new werknemer();
            $werknemer->store($request);
            return view('login_portal');
        }
    }

模型:

<?php

    namespace App;
    use Illuminate\Database\Eloquent\Model;
    use App\controllers\werknemer
    class werknemer_model extends Model
    {
        public function store (Request $request){
            $email = $request->input('email');
            $password = $request->input('password');

            $werknemer = new werknemer;
            $werknemer->id = 1;
            $werknemer->bedrijf_id = 1;
            $werknemer->email = $email;
            $werknemer->naam = $password;
            $werknemer->save();
        try {
            DB::connection()->getPdo();
        } catch (\Exception $e) {
        die("Could not connect to the database.  Please check your configuration.");
        }
    }
}

路线:

    Route::get('/', function () {
        return view('Login_portal');
    });
    Route::post('/post','werknemer@create');

问题:我的数据没有保存到数据库,我没有收到错误

1 个答案:

答案 0 :(得分:1)

这是常见问题,容易忘记。您忘记在模型中设置变量(对于质量分配)。检查要将哪些数据放入数据库并在模型中设置它。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'another_data', 'another_variable'];
}

据我所知,你的模型结构不符合Laravel惯例。阅读并节省将来的时间:https://laravel.com/docs/5.6/eloquent#mass-assignment