gpt4 book ai didi

php - ajax 成功但 laravel 未插入数据库

转载 作者:行者123 更新时间:2023-12-01 04:01:14 25 4
gpt4 key购买 nike

我的赞成/反对投票系统遇到了极大的困难。 ajax 执行 success 函数,但是 laravel 不会将任何内容插入数据库..我不知道我做错了什么。我的 AJAX:

$('[data-value]').on('click', function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
var $this = $(this);
var post_id = $this.data('id');
var votetype = $this.data('value');
$.ajax({
type:"POST",
url:'/post/' + post_id + '/vote/' + votetype,
success:vote(post_id,votetype)
});
// vote(post_id,votetype);
});

我的路线:

Route::post('/post/{id}/vote/{vote}', 'PostController@vote');

我的laravel投票功能:

  public function vote($id,$vote)
{
//TODO: refactor this..
$username = Session::get('username');
$id = Post::find($id);
$uservote = PostVotes::where('id',$id)->where('username',$username)->first();
if($uservote->count())
{
$votetype = $uservote->vote;
if($votetype === $vote)
{
$uservote->delete();
} else {
Vote::updateOrCreate(
['vote' => $votetype],
['vote' => $vote]
);
}
} else {
$uservote = new PostVotes;
$uservote->vote = $vote;
$uservote->username = $username;
$uservote->id = $id;
$uservote->save();
}
}

我不知道这是否有必要,但这是我的迁移

class CreatePostVotesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('post_votes', function (Blueprint $table) {
$table->integer('id');
$table->string('username',50);
$table->integer('vote');

});
}
}

class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id')->unique;
$table->string('title',80);
$table->string('username',50);
$table->integer('score');
$table->timestamp('created_at');
$table->string('image',512);
});
}
}

关系:(Post.php):

public function votes()
{
//Quick note: Id refers to the post id.
return $this->hasMany(PostVotes::class,'id');
}

PostVotes.php:

public function post()
{
return $this->belongsTo(Post::class,'id');
}

最佳答案

我确信问题之一就在这里:

$id = Post::find($id);
/*
* you are passing instance of the Post model or null into where('id', $id)
* predicate, instead of the id of the post
*/
$uservote = PostVotes::where('id',$id)->where('username',$username)->first();

您可以尝试更改此设置:

$post = Post::find($id);
$uservote = PostVotes::where([
'id' => $post->id,
'username' => $username,
])
->first();

因为您将模型 Post 的实例(或 null,如果 Post::find($id) 返回任何内容)传递到您的条件而不是帖子 id 属性。

添加:

在使用$uservote之前,您应该检查$uservote:

$uservote = PostVotes::where([
'id' => $post->id,
'username' => $username,
])
->first();

// $uservote is empty, you should create new instance of the PostVotes model
if( empty($uservote)){
}

您还可以传递$id,它是函数vote的参数,如果这个id是帖子的id。

$uservote = PostVotes::where([
'id' => $id, // <-- use $id, if $id is the post id
'username' => $username,
])
->first();

如果您想将帖子的 ID 存储到投票模型中,您可能应该更改此设置:

$uservote->id = $id;

关于这一点:

$uservote->id = $post->id;

接受我对您的代码的建议后,当 $post = Post::find($id);

顺便说一句,为了更好的可读性,如果您想要商店 ID,您应该将模型 Voteid 属性重命名为 post_id此属性中的帖子。通常,名称为id的字段用作具有自动增量属性的表主键。

另请查看 /storage/logs/laravel.log 文件,您可以在其中找到有关错误的更多信息。

添加#2

你应该得到这样的东西:

public function vote($id, $vote)
{
//TODO: refactor this..
$username = Session::get('username');
$post = Post::find($id);

if( empty($post) ){
abort(404, 'Cannot find post');
}

$uservote = PostVotes::where([
'id' => $post->id,
'username' => $username,
])
->first();

// replace $uservote->count() with !empty($userwote), because you cannot call count() nethod from null
if (!empty($uservote)) {
$votetype = $uservote->vote;
if ($votetype === $vote) {
$uservote->delete();
} else {
Vote::updateOrCreate(
['vote' => $votetype], ['vote' => $vote]
);
}
} else {
$uservote = new PostVotes();
$uservote->vote = $vote;
$uservote->username = $username;
$uservote->id = $post->id; // <!-- probably replace `id` field name on `post_id`
$uservote->save();
}
}

关于php - ajax 成功但 laravel 未插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43014094/

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