我是Laravel,AJAX等人的新手。我是一名从事Twitter类型项目的学生,这是我有史以来提出的第一个堆栈问题。我试图寻找答案,但是老师帮助我的代码与其他示例完全不同。我很确定我的问题出在我的“ Tweet控制器”内部的方法不同...非常感谢您的帮助!希望我提供了足够的信息,并希望这可以在将来对其他人有所帮助:)
这是我的错误:[1]:https://i.stack.imgur.com/zkxsd.png
POST http://localhost:8000/tweets/19/unlike 404 (Not Found)
Uncaught (in promise) Error: Request failed with status code 404
at createError (app.js:14253)
at settle (app.js:35706)
at XMLHttpRequest.handleLoad (app.js:14127)
这是我的点赞表/迁移
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateLikesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('likes', function (Blueprint $table) {
$table->increments('id');
$table->integer('tweet_id');
$table->integer('user_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('likes');
}
}
这是我的推特控制器
public function like($id){
$like = Like::create([
'user_id'=>auth()->id(),
'tweet_id'=> $id
]);
if($like){
return json_encode(array('status' => 'success'));
}
return json_encode(array('status' => 'failed'));
}
public function unlike($id){
$like = Like::delete([
'user_id'=>auth()->id(),
'tweet_id'=> $id
]);
if($like){
return json_encode(array('status' => 'success'));
}
}
这是我的网络路线
Route::post('tweets/{tweet}/like', 'TweetController@like');
Route::delete('tweets/{tweet}/like', 'TweetController@unlike');
这是我的模特
public function likes(){
return $this->hasMany(Like::class);
}
public function likedByCurrentUser(){
$userId=auth()->id();
//like boolean
$like = $this->likes->first(function($v) use ($userId){
//$v is a reference to the single like
return $v->user_id == $userId;
});
//if the user has liked a post
if($like){
return true;
}
return false;
}
这是我的Vue组件:
<script>
export default {
name: 'likeButton',
props: ['likeCount','hasLiked','tweetId','csrf'],
mounted(){
this.dataLikeCount = this.likeCount;
this.dataHasLiked = this.hasLiked;
},
data(){
return{
dataLikeCount:0,
dataHasLiked:false
}
},
methods:{
doLike(){
var type='like';
if(this.dataHasLiked){
type='unlike'
}
axios({
method:'POST',
url:'/tweets/' + this.tweetId + '/'+ type,
headers:{
'X-CSRFToken':this.csrf
},
json: true
}).then((response) => {
if(response.data.status == 'success'){
// response was successful (regardless of type)
return true
// if type is like
// add one to like count, set hasLiked to true
if(type == 'like'){
this.dataLikeCount++
}
// if type is unlike
// deduct one from like count, set hasLiked to false
if(type =='unlike'){
return false
this.dataLikeCount--
}
}
});
}
}
}
</script>
<template>
<div>
<button type="submit"
:class="{'btn btn-link':dataHasLiked}"
@click="doLike">
<i class="star"></i>
Like {{ dataLikeCount }}
</button>
</div>
</template>
答案 0 :(得分:1)
您没有unlike
的匹配路线。
更改
Route::delete('tweets/{tweet}/like', 'TweetController@unlike');
收件人
Route::delete('tweets/{tweet}/unlike', 'TweetController@unlike');
答案 1 :(得分:0)
正如@DigitalDrifter所说,您没有路线可以与众不同。所以需要改变它。
您的路线就像
Route::delete('tweets/{tweet}/unlike', 'TweetController@unlike');
因此必须使用ajax通过DELETE方法调用此路由。您使用post方法调用了ajax。它还会给出方法不允许的错误。
因此,更改您的网络路线,
来自
Route::delete('tweets/{tweet}/unlike', 'TweetController@unlike');
收件人
Route::post('tweets/{tweet}/unlike', 'TweetController@unlike');
并在控制器方法中更改返回响应,如下所示
推特控制器
public function like($id){
$like = Like::create([
'user_id'=>auth()->id(),
'tweet_id'=> $id
]);
if($like){
return response()->json(['status' => 'success']);
}
return response()->json(['status' => 'failed']);
}
public function unlike($id){
$like = Like::delete([
'user_id'=>auth()->id(),
'tweet_id'=> $id
]);
if($like){
return response()->json(['status' => 'success']);
}
}
希望现在有帮助!
答案 2 :(得分:0)
使用此
Route::delete('tweets/{tweet}/unlike', 'TweetController@unlike');