从Laravel 5

时间:2017-08-08 09:42:06

标签: php mysql laravel laravel-5 insert

Hello stackoverflow请帮助我理解laravel,我是laravel 5的新手。我真的是laravel的初学者。我正在学习Laravel 5.现在我正在创建一个注册页面,我需要将数据插入到我的数据库中。

这是我的问题: 请更正我的代码,因为我无法将数据插入数据库。 2.将我的数据插入两个表格。

现在在我的情况下,我有两个表:

enter image description here

现在,我正在创建一个包含以下字段的表单:

<form class="form-horizontal" action="{{ route('user_layouts.signup') }}" method="post">
  <div class="form-group">
    <label class="control-label col-sm-3" for="email">Email:</label>
    <div class="col-sm-6">
      <input type="email" class="form-control" id="email" name="email" placeholder="Email">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-3" for="pwd">Password:</label>
    <div class="col-sm-6"> 
      <input type="password" class="form-control" id="password" name="password" placeholder="Password">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-3" for="fname">First Name:</label>
    <div class="col-sm-6"> 
      <input type="text" class="form-control" id="fname" name="fname" placeholder="First Name">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-3" for="lname">Last Name:</label>
    <div class="col-sm-6"> 
      <input type="text" class="form-control" id="lname" name="lname" placeholder="Last Name">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-3" for="mname">Middle Name:</label>
    <div class="col-sm-6"> 
      <input type="text" class="form-control" id="mname" name="mname" placeholder="Middle Name">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-3" for="contactnum">Contact #:</label>
    <div class="col-sm-6"> 
      <input type="text" class="form-control" id="contactnum" name="contactnum" placeholder="Contact Number">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-3" for="gender">Gender:</label>
    <div class="col-sm-6"> 
        <label class="radio-inline"><input type="radio" name="gender">Male</label>
        <label class="radio-inline"><input type="radio" name="gender">Female</label>
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-3" for="bday">Birthday:</label>
    <div class="col-sm-8">
        <div class="row">
            <div class="col-xs-4"> 
            <select class="form-control" id="month" name="month">
                <option>Month</option>
                <option value="Jan">Jan</option>
                <option value="Feb">Feb</option>
                <option value="Mar">Mar</option>
                <option value="Apr">Apr</option>
                <option value="May">May</option>
                <option value="Jun">Jun</option>
                <option value="Jul">Jul</option>
                <option value="Aug">Aug</option>
                <option value="Sep">Sep</option>
                <option value="Oct">Oct</option>
                <option value="Nov">Nov</option>
                <option value="Dec">Dec</option>
            </select>
            </div>

            <div class="col-xs-4">                      
            <select class="form-control" id="day" name="day">
                <option value="day">Day</option>
                @for ($i = 0; $i <= 31; $i++)
                    <option value="{{ $i }}">{{ $i }}</option>
                @endfor
            </select>
            </div>

            <div class="col-xs-4">
            <select class="form-control" id="year" name="year">
                <option>Year</option>                                   
                <option value="1992">1992</option>
                <option value="1993">1993</option>
                <option value="1994">1994</option>
                <option value="1995">1995</option>                                      
            </select>
            </div>
        </div>                  
    </div>
  </div>
  <div class="form-group"> 
    <div class="col-sm-offset-3 col-sm-10">
      <button type="submit" class="btn btn-success">Submit</button>
    </div>
  </div>
  {{ csrf_field() }}
</form>

现在这是我的路线:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('shop_layouts.index');
});

Route::get('/signup', [
    'uses'  => 'UserController@getSignup',
    'as'    => 'user_layouts.signup'
]);

Route::post('/signup', [
    'uses'  => 'UserController@postSignup',
    'as'    => 'user_layouts.signup'
]);

这是我的控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserController extends Controller
{
    public function getSignup() {
        return view('user_layouts.signup');
    }

    public function postSignup(Request $request) {
        $this->validate($request, [
            'email' => 'email|required|unique:tbl_users',
            'password' => 'required|min:24',
            'fname' => 'required',
            'lname' => 'required',
            'mname' => 'required',
            'contactnum' => 'required',
            'gender' => 'required',
            'month' => 'required',
            'day' => 'required',
            'year' => 'required'
        ]);

        $user = new User([
            'email' => $request->input('email'),
            'password' => bcrypt($request->input('password')),
        ]);
        $user->save();

        $customer = new Customer([   
            'fname' => $request->input('fname'),
            'lname' => $request->input('lname'),
            'mname' => $request->input('mname'),
            'contactnum' => $request->input('contactnum'),
            'gender' => $request->input('gender'),
            'lname' => $request->input('lname'),
        ]);
        $customer->save();
    }

}

最后我的两个模特:

客户模式

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    protected $fillable = [
        'fname', 'lname','mname','contactnum','gender'
    ];

    public function user() {
        return $this->belongsTo(User::class);
    }
}

用户模型:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'email', 'password'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function customer() {
        return $this->hasOne(Customer::class);  
    }
}

这是我的迁移: 客户:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class TblCustomers extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tbl_customers', function (Blueprint $table){
            $table->increments('customer_id');
            $table->integer('user_id')->unsigned();
            $table->string('fname');
            $table->string('lname');
            $table->string('mname');
            $table->string('contact_no');
            $table->string('gender');
            $table->date('birthdate');
            $table->timestamps();                        
        });

        Schema::table('tbl_customers', function (Blueprint $table){
            $table->foreign('user_id')->references('user_id')->on('tbl_users')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('tbl_customers');
    }
}

用户:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tbl_users', function (Blueprint $table){
            $table->increments('user_id');
            $table->string('email')->unique();
            $table->string('password');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('tbl_users');
    }
}

1 个答案:

答案 0 :(得分:3)

我认为您需要更新以下文件:

客户模式

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{

  /**
   * The database table used by the model.
   *
   * @var string
   */
  protected $table = 'tbl_Customers';

  /**
  * The database primary key value.
  *
  * @var string
  */
  protected $guarded = ['customer_id', '_token'];

  /**
   * Attributes that should be mass-assignable.
   *
   * @var array
   */
  protected $fillable = [
      'fname', 'lname','mname','contact_no','gender','user_id','birthdate'
  ];

  /**
   * The roles that belong to the User.
   */
  public function tbl_users()
  {
    return $this->belongsTo('App\User', 'user_id');
  }
}

用户模型

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{


    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'tbl_users';

    /**
    * The database primary key value.
    *
    * @var string
    */
    protected $guarded = ['user_id', '_token'];

    /**
     * Attributes that should be mass-assignable.
     *
     * @var array
     */
    protected $fillable = ['email','password'];


    /**
    * The roles that belong to the Customer.
    */
    public function tbl_Customers()
    {
        return $this->hasMany('App\Customer');
    }
}

<强>控制器

use App/Customer;
use App/User;



public function postSignup(Request $request) {
  $this->validate($request, [
      'email' => 'email|required|unique:tbl_users',
      'password' => 'required|min:24',
      'fname' => 'required',
      'lname' => 'required',
      'mname' => 'required',
      'contactnum' => 'required',
      'gender' => 'required',
      'month' => 'required',
      'day' => 'required',
      'year' => 'required'
  ]);

  $dob1 = $request->month.'-'.$request->day.'-'.$request->year;

  $date1 = strtotime($dob1);

  $date2 = date('m-d-Y',$date1);

  $user = new User();
  $user->email = $request->email;
  $user->password = bcrypt($request->password);
  $user->save();


  $customer = new Customer();
  $customer->user_id = $user->user_id;
  $customer->fname = $request->fname;
  $customer->lname = $request->lname;
  $customer->mname = $request->mname;
  $customer->contact_no = $request->contactnum;
  $customer->gender = $request->gender;
  $customer->birthdate = $date2;
  $customer->save();
}

希望这对你有用!!!