为 Laravel 5.4 中的类似系统建立关系
Setting up relationships for like system in Laravel 5.4
我正在做一个简单的项目来练习 Laravel,但我卡住了。我不明白为什么每次我点击 like
按钮时,都会在我的 table 中创建一个新行...所以我有 "multiple rows (1)" 用于任何点击我喜欢的按钮使.
多行 (1)
我怀疑这与我的模型之间的关系有关,但我看不出我做错了什么。
我的User.php型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
class User extends \Eloquent implements Authenticatable
{
use AuthenticableTrait;
public function likes()
{
return $this->hasMany('App\Like');
}
}
我的Memo.php模特
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Memo extends Model
{
public function likes()
{
return $this->hasMany('App\Like');
}
}
我的Like.php模特
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Like extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
public function memo()
{
return $this->belongsTo('App\Memo');
}
}
我的 MemoController 处理我的 AJAX 请求的方法:
public function likeMemo(Request $request)
{
$memo_ID = $request['id'];
$memo = Memo::findOrFail($memo_ID)->first();
// Is already liked...?
$like = Auth::user()->likes()->where('memo_id', $memo_ID)->first();
if($like->is_like) {
$like->is_like = false;
$like->update();
return response()->json(['status' => $like], 200);
}
// Insert data into DB.
else {
$like = new Like();
$like->user_id = Auth::user()->id;
$like->memo_id = $memo_ID;
$like->is_like = true;
$like->save();
return response()->json(['status' => 'inserted like']);
}
}
该代码应该能够在特定的备忘录中切换点赞,例如 Instagram,因为我的目标是制定将这些备忘录添加到特定 收藏夹 类别中的逻辑。
我的猜测是,如果数据不受欢迎,您应该更新数据,只有在找不到记录时才创建一个新数据。
public function likeMemo(Request $request)
{
$memo_ID = $request['id'];
$memo = Memo::findOrFail($memo_ID)->first();
// Is already liked...?
$like = Auth::user()->likes()->where('memo_id', $memo_ID)->first();
// Insert data into DB.
if(!$like) {
$like = new Like();
$like->user_id = Auth::user()->id;
$like->memo_id = $memo_ID;
$like->is_like = true;
$like->save();
return response()->json(['status' => 'inserted like']);
} else {
if($like->is_like) {
$like->is_like = false;
$like->update();
return response()->json(['status' => $like], 200);
} else {
$like->is_like = true;
$like->update();
return response()->json(['status' => $like], 200);
}
}
}
我正在做一个简单的项目来练习 Laravel,但我卡住了。我不明白为什么每次我点击 like
按钮时,都会在我的 table 中创建一个新行...所以我有 "multiple rows (1)" 用于任何点击我喜欢的按钮使.
多行 (1)
我怀疑这与我的模型之间的关系有关,但我看不出我做错了什么。
我的User.php型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
class User extends \Eloquent implements Authenticatable
{
use AuthenticableTrait;
public function likes()
{
return $this->hasMany('App\Like');
}
}
我的Memo.php模特
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Memo extends Model
{
public function likes()
{
return $this->hasMany('App\Like');
}
}
我的Like.php模特
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Like extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
public function memo()
{
return $this->belongsTo('App\Memo');
}
}
我的 MemoController 处理我的 AJAX 请求的方法:
public function likeMemo(Request $request)
{
$memo_ID = $request['id'];
$memo = Memo::findOrFail($memo_ID)->first();
// Is already liked...?
$like = Auth::user()->likes()->where('memo_id', $memo_ID)->first();
if($like->is_like) {
$like->is_like = false;
$like->update();
return response()->json(['status' => $like], 200);
}
// Insert data into DB.
else {
$like = new Like();
$like->user_id = Auth::user()->id;
$like->memo_id = $memo_ID;
$like->is_like = true;
$like->save();
return response()->json(['status' => 'inserted like']);
}
}
该代码应该能够在特定的备忘录中切换点赞,例如 Instagram,因为我的目标是制定将这些备忘录添加到特定 收藏夹 类别中的逻辑。
我的猜测是,如果数据不受欢迎,您应该更新数据,只有在找不到记录时才创建一个新数据。
public function likeMemo(Request $request)
{
$memo_ID = $request['id'];
$memo = Memo::findOrFail($memo_ID)->first();
// Is already liked...?
$like = Auth::user()->likes()->where('memo_id', $memo_ID)->first();
// Insert data into DB.
if(!$like) {
$like = new Like();
$like->user_id = Auth::user()->id;
$like->memo_id = $memo_ID;
$like->is_like = true;
$like->save();
return response()->json(['status' => 'inserted like']);
} else {
if($like->is_like) {
$like->is_like = false;
$like->update();
return response()->json(['status' => $like], 200);
} else {
$like->is_like = true;
$like->update();
return response()->json(['status' => $like], 200);
}
}
}