gpt4 book ai didi

javascript - Laravel 插入 2 个表

转载 作者:行者123 更新时间:2023-11-29 06:11:34 25 4
gpt4 key购买 nike

我有 2 个表 usertn_user,表 user 是一个包含登录信息的表,我从 https://laravel.com/ 开始通过教程制作的所以基本上它是自动创建的,而 tn_user 是我自己制作的表

USER TABLE image alt in case u can't see the atribut are id, name, email, password that the important things, email and password in this table is used to logging in

TN_USER TABLE image alt the atribut are cn_id, cv_name, cv_email, cn_phone, cv_position, cv_address, cv_country, cv_username, cv_password, cv_privileges, those are the important thing

根据下面的表格,我想将 usernamepassword 插入到表 user 中,其余插入到表 tn_user 我该怎么做?我对 laravel 很陌生,所以不是很明白,通常我使用 CI

image alt

UserController.php this is where the code i use to insert data i use json response to parse the data and not quite sure how to insert data into 2 tables little help here

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);
}

UserCompany.php is my model but since im new im not really understand how to use relationship yet

 <?php namespace Activity;

use Illuminate\Database\Eloquent\Model;

class UserCompany extends Model {
protected $table = 'tn_user';
public $timestamps = false;

protected $fillable = [

];

/*public function usercompany(){
return $this->belongsTo('Activity\user');
}*/
}

最佳答案

你应该知道在 UserCompany 类中,通过设置 fillable,这意味着你正在设置要更改的表列,在本例中为 tn_user 表。所以这意味着,通过设置

protected $fillable = [];

这意味着,当您使用像这样的命令时,您不会对任何表格列进行修改;

$user_details->cv_name = Input::get('cv_name');

好的,所以你应该知道的第一件事是,当创建两个表时,即 userstn_users 你应该有一个列,其中包含一个与这两个表相关的值表,我建议您使用 users 表中的用户 id:

我注意到您已将 cn_id 用作链接器,但最好每个表都有自己的递增 id 列,在这种情况下还有自己的 link_id 列

假设您要重新开始:

  1. 打开命令提示符终端并转到你的laravel项目文件夹目录并输入:-$ php artisan make:model User -m -$ php artisan make:model UserDetail -m

    这将做的是,创建 UserUserDetail,并添加 -m 意味着它为关联的模型创建迁移,即 create_users_tablecreate_user_details_table

  2. 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 它有点重要,你应该设置与用户帐户的链接,这样假设你删除了用户,他的凭据也被删除了,但你可以让它这样做,如果你想要。


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 并将 Controller 与它的资源。

在 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();
}
}

希望我已经回答了您的问题。以下是一些有用的学习链接。

关于javascript - Laravel 插入 2 个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38007306/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com