我有2个表user
和tn_user
,表user
是一个包含要登录信息的表,我是通过https://laravel.com/的教程制作的,所以基本上它是自动的创建,而tn_user
是我自己制作的表格
USER TABLE 万一你无法看到atribut是
id
,name
,password
重要的事情TN_USER TABLE atribut是
password
,cn_id
,cv_name
,cv_email
,cn_phone
,cv_position
,cv_address
,cv_country
,cv_username
,cv_password
,这些是重要的事情
根据下面的表单,我想将cv_privileges
和username
插入表password
,其余部分插入表user
,我该怎么做?我对laravel很新,所以不太了解如何,通常我使用CI
UserController.php 这是我用来插入数据的代码 我使用json响应来解析数据,而不太确定如何将数据插入到2个表中,这里的帮助很少
tn_user
UserCompany.php 是我的模特,但是因为新手我真的不懂如何使用关系
public function createOrEdit(){
//get current user
$currentUserId = Auth::user()->id;
$isUpdate = false;
$id = Input::get('id');
$user = new UserCompany;
if($id != ""){
$user = UserCompany::where('cn_id', '=', $id)->firstOrFail();
$user->cv_updated_by = $currentUserId;
$user->cv_updated_at = Carbon::now();
$isUpdate = true;
}else{
$user->cv_created_by = $currentUserId;
$user->cv_created_at = Carbon::now();
}
$user->cv_name = Input::get('name');
$user->cv_position = Input::get('position');
$user->cv_email = Input::get('email');
$user->cn_phone = Input::get('phone');
$user->cv_address = Input::get('address');
$user->cv_username = Input::get('username');
$user->cv_password = Input::get('password');
$user->cv_country = Input::get('country');
if($isUpdate){
UserCompany::where('cn_id','=',$id)->update(['cv_updated_by' => $user->cv_updated_by,
'cv_updated_at' => $user->cv_updated_at,
'cv_name' => $user->cv_name,
'cv_position' => $user->cv_position,
'cv_email' => $user->cv_email,
'cn_phone' => $user->cn_phone,
'cv_country' => $user->cv_country,
'cv_username' => $user->cv_username,
'cv_password' => $user->cv_password,
'cv_address' => $user->cv_address]);
}else{
$user->save();
}
$returnedData = UserCompany::all();
$response = array(
'content' => $returnedData,
'status' => 'success',
);
return Response::json($response);
}
答案 0 :(得分:1)
您创建2个对象
$user = new User()
$user->username = INPUT::get('username');
$user->password = $password // Hashed
$user->save();
$user_detail = new UserCompany() // Your detail table modal.
$user_detail->cv_name = Input::get('cv_name');
//etc
$user_detail->save()
答案 1 :(得分:1)
您应该知道在UserCompany
类中,通过设置fillable
,这意味着您要设置要更改的表列,在本例中为tn_user
表。所以这意味着,通过设置
protected $fillable = [];
这意味着,当您使用像<; p>这样的命令时,您不会对表列进行修改
$user_details->cv_name = Input::get('cv_name');
好的,所以你应该知道的第一件事是,当创建两个表,即users
和tn_users
时,你应该有一个列带有一个与两个表相关的值,我建议你是来自id
表的用户users
:
我注意到你已经使用cn_id
作为链接器,但最好是每个表都有自己的递增id列,在这种情况下,它自己的link_id列
假设你重新开始:
打开命令提示符或终端,然后转到laravel项目文件夹目录并输入:-$ php artisan make:model User -m
并再次输入-$ php artisan make:model UserDetail -m
< / p>
这样做,创建User
和UserDetail
,添加-m表示为相关模型创建迁移create_users_table
和create_user_details_table
< / p>
从create_users_table
创建所需的表格列,如下所示:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table){
$table->increments('id');
$table->integer('auth');
$table->string('username')->unique();
$table->string('email');
$table->string('password');
$table->boolean('online');
$table->string('lang', 2);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::drop('users');
}
}
现在create_tn_users_table
有点重要,你应该设置哪些链接与用户帐户,以便假设您删除用户,他的凭据也被删除,但如果你愿意,你可以做其他的。< / p>
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTnUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tn_users', function (Blueprint $table) {
$table->increments('id');
$table->string('full_name');
$table->string('username')->unique();
$table->integer('link_user_id')
->references('id')->on('users'); // Relationship btn table tn_users and users
$table->string('phone');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tn_users');
}
}
现在转到命令提示符或终端,然后输入-$ php artisan migrate
以创建表格。
再次在命令提示符或终端中输入-$ php artisan make:controller UserController --resource
,并将控制器与其资源一起制作。
在UserController内的create()
函数中,将Request
作为参数添加。
提交您创建的表单后,将触及这些功能
namespace App\Http\Controllers;
use App\User;
use App\TnUser;
use ...
class UserController extends Controller{
public function create(Request $request){
$tn_user = new TnUser();
$user = new User();
$user->username = $request['username'];
$user->password = bcrypt($request['username']);
...
$user->save();
$tn_user->full_name = ucword(strtolower($request['full_name'));
$tn_user->link_user_id = $user->id; // uses the previously save id
$tn_user->phone = trim($request['phone']);
$th_user->save();
}
}
我希望我已经回答了你的问题。以下是一些有用的学习链接。