如何检查用户是否喜欢 属性
How do I check if a user has like a property
我正在制作一个属性广告网站。在应用中用户可以点赞或'star'一个属性。这在带星标的 table 中创建了一条记录 property_id 和 user_id.
我想检查用户是否喜欢当前的 属性,这样他们就不会再喜欢了。
目前就在 API 结束。
这是我的控制器
这将创建 liked/starred 记录。
public function store(Property $property, User $user, Request $request)
{
$starred = StarredProperty::create(['property_id' => $property->id, 'user_id' => $user->id]);
return $starred;
}
这是加星标的 属性 模特
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class StarredProperty extends Model
{
use SoftDeletes;
protected $fillable = ['property_id', 'user_id'];
}
这是与所有加星标属性相关的用户模型。
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Property;
use App\StarredProperty;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
protected $fillable = [
'first_name', 'last_name', 'username', 'email', 'phone_number', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function properties()
{
return $this->hasMany(Property::class);
}
public function starredProperties()
{
return $this->hasMany(StarredProperty::class);
}
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
}
您只需为此在控制器中添加一个检查即可。您可以将代码更改为如下内容:
public function store(Property $property, User $user, Request $request)
{
if (! $user->starredProperties()->where('property_id', $property->id)->exists()) {
$starred = StarredProperty::create(['property_id' => $property->id, 'user_id' => $user->id]);
return $starred;
}
// Return whatever else you want here if they have already starred the property
}
我正在制作一个属性广告网站。在应用中用户可以点赞或'star'一个属性。这在带星标的 table 中创建了一条记录 property_id 和 user_id.
我想检查用户是否喜欢当前的 属性,这样他们就不会再喜欢了。
目前就在 API 结束。
这是我的控制器
这将创建 liked/starred 记录。
public function store(Property $property, User $user, Request $request)
{
$starred = StarredProperty::create(['property_id' => $property->id, 'user_id' => $user->id]);
return $starred;
}
这是加星标的 属性 模特
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class StarredProperty extends Model
{
use SoftDeletes;
protected $fillable = ['property_id', 'user_id'];
}
这是与所有加星标属性相关的用户模型。
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Property;
use App\StarredProperty;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
protected $fillable = [
'first_name', 'last_name', 'username', 'email', 'phone_number', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function properties()
{
return $this->hasMany(Property::class);
}
public function starredProperties()
{
return $this->hasMany(StarredProperty::class);
}
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
}
您只需为此在控制器中添加一个检查即可。您可以将代码更改为如下内容:
public function store(Property $property, User $user, Request $request)
{
if (! $user->starredProperties()->where('property_id', $property->id)->exists()) {
$starred = StarredProperty::create(['property_id' => $property->id, 'user_id' => $user->id]);
return $starred;
}
// Return whatever else you want here if they have already starred the property
}