I'm making a property advertisement website. In the app users can like or 'star' a property. This created a record in the starred table with the property_id, and the user_id.
I want to check if the user has like the current property so they can't like it again.
This is all on the API end at the moment.
Here is my controller
This creates the liked/starred record.
public function store(Property $property, User $user, Request $request)
{
$starred = StarredProperty::create(['property_id' => $property->id, 'user_id' => $user->id]);
return $starred;
}
This is the starred property model
<?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'];
}
This is the user model with a relationship for all starred properties.
<?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 [];
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire