gpt4 book ai didi

php - Laravel 5.4 级联无法创建 REST API(也无法实现一对多关系)

转载 作者:行者123 更新时间:2023-11-30 22:04:26 25 4
gpt4 key购买 nike

检查我的代码,我正在使用 laravel 5.4 和 Mysql 使用一对多关系创建 REST API,帮我找出错误

我的帐户迁移代码表和模型(用户有很多帐户)

<?php
class CreateAccountsTable extends Migration
{

public function up()
{
Schema::create('accounts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->string('bank_name');
$table->string('ac_no');
$table->timestamps();


});

Schema::table('accounts', function($table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('accounts');

Schema::table('accounts', function($table) {
$table->dropForeign('accounts_user_id_foreign');
});
}

账户模型

class Accounts extends Model
{
protected $fillable =[
'bank_name','ac_no'
];
public function useraccounts()
{
return $this->belongsTo('App\User','user_id');
}
}

User Table Migrate code & model User have Many accounts

表格代码

class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email',191)->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}


public function down()
{
Schema::dropIfExists('users');
}
}

用户模型

class User extends Authenticatable
{
public function accounts()
{
return $this->hasMany('App\Accounts');
}
// this is a recommended way to declare event handlers
protected static function boot() {
parent::boot();

static::deleting(function($user) { // before delete() method call this
$user->accounts()->delete();
// do the rest of the cleanup...
});
}
}

我的 Controller

 // One to many relationship

public function one_to_many(Request $request)
{
$v = Validator::make($request->all(),[
'bank_name'=>'required',
'ac_no'=>'required',
'user_id'=>'required'
]);

$user_id = $request->input('user_id');
$bankname = $request->input('bank_name');
$ac_no = $request->input('ac_no');

if($v->fails())
{
$response = [

'msg' => 'Check your input Error Occur',
'status' => false,
'reason' => $v->errors()
];

return response()->json($response, 422);
}elseif(! User::find($user_id))
{
$response = [

'msg' => 'Give the correct Userid',
'status' => false,
'reason' => 'Cant find the user_id from DB'
];

return response()->json($response, 422);
}

$user = User::find($user_id)->id;

//$get = $user->posts();

$array = array('bank_name'=>$bankname,'ac_no'=>$ac_no);

$savepost = Accounts::create($array);


$savepost->useraccounts()->save($user);

return $savepost;
}

错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`laraapirest`.`accounts`, CONSTRAINT `accounts_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE) (SQL: insert into `accounts` (`bank_name`, `ac_no`, `updated_at`, `created_at`) values (adasdasd, 11111, 2017-02-13 20:07:34, 2017-02-13 20:07:34))



PDOException in Connection.php line 449:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`laraapirest`.`accounts`, CONSTRAINT `accounts_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE)

最佳答案

您没有在 Controller 的这一行中传递用户 ID:

$array = array('bank_name'=>$bankname,'ac_no'=>$ac_no);

$savepost = Accounts::create($array);

您需要传递它,因为 create() 将尝试写入数据库。

另一种选择是使用 new 运算符初始化 Account,并传递变量:

$array = array('bank_name'=>$bankname,'ac_no'=>$ac_no);

$savepost = new Accounts;
$savepost->bank_name = $bankname
....

$savepost->useraccounts()->save($user);

关于php - Laravel 5.4 级联无法创建 REST API(也无法实现一对多关系),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42212875/

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