Php 从Laravel 5中的一个表格插入不同的表格
你好,请帮助我了解拉威尔,我是拉威尔5号的新手。我真的是拉威尔的初学者。我正在学习拉维5。现在我正在制作一个注册页面,在那里我需要将数据插入数据库 以下是我的问题: 1.请更正我的代码,因为我无法将数据插入数据库。 2.将我的数据插入到两个表中 在我的情况下,我有两张表: 现在,我正在创建一个包含以下字段的表单:Php 从Laravel 5中的一个表格插入不同的表格,php,mysql,laravel,laravel-5,insert,Php,Mysql,Laravel,Laravel 5,Insert,你好,请帮助我了解拉威尔,我是拉威尔5号的新手。我真的是拉威尔的初学者。我正在学习拉维5。现在我正在制作一个注册页面,在那里我需要将数据插入数据库 以下是我的问题: 1.请更正我的代码,因为我无法将数据插入数据库。 2.将我的数据插入到两个表中 在我的情况下,我有两张表: 现在,我正在创建一个包含以下字段的表单: <form class="form-horizontal" action="{{ route('user_layouts.signup') }}" method="post"&
<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>
电邮:
密码:
名字:
姓氏:
中名:
联系方式:
性别:
男性
女性
生日:
月
简
二月
破坏
四月
也许
六月
七月
八月
九月
十月
十一月
12月
白天
@对于($i=0;$i我认为您需要更新以下文件,如:
客户模型
<?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');
}
}
希望这对您有用!!!您有任何错误吗?检查日志。您有什么错误?我认为您需要使用“使用”来包括这两个模型例如:使用path/to/customer模型;使用path/to/user模型;@VivekShah正如我所说的。请检查我的代码。我完全是一个初学者。根据我的代码,我做得对吗?请帮助我。我的控制台也没有错误,因为我在控制器或任何显示的东西中没有错误处理程序error@Sunil谢谢你的回复,先生。请帮忙我检查我的代码,因为我是laravel的初学者。我不认为我用我的代码做的是正确的方式。谢谢你的回答,先生。仍然没有。为什么?请帮助我。我的迁移有问题吗?或者为什么不能保存?@JcJohn让我检查一下,你喜欢用哪种方法来存储数据雄辩或流畅nt?也许很有说服力。我们可以讨论一下吗?我真的不明白这个模型是如何工作的。请帮助我。在我的模型中,我没有一个能插入数据库的函数。我需要把我的迁移放进去吗?这样你可以检查一下吗?请帮助我,先生。thanks@JcJohn您的数据库结构正确吗?您在问题中显示了该结构
<?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');
}
}
<?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();
}