2017-08-27 04:44:53 +09:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2019-04-05 23:10:26 +09:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2017-08-27 04:44:53 +09:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2019-04-05 23:10:26 +09:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
2017-08-27 04:44:53 +09:00
|
|
|
|
|
|
|
class Ejaculation extends Model
|
|
|
|
{
|
|
|
|
//
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'user_id', 'ejaculated_date',
|
2018-01-05 00:26:48 +09:00
|
|
|
'note', 'geo_latitude', 'geo_longitude', 'link',
|
2017-08-27 04:44:53 +09:00
|
|
|
'is_private'
|
|
|
|
];
|
2017-11-05 01:26:52 +09:00
|
|
|
|
|
|
|
protected $dates = [
|
|
|
|
'ejaculated_date'
|
|
|
|
];
|
2018-01-07 22:19:33 +09:00
|
|
|
|
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\User');
|
|
|
|
}
|
2018-01-08 08:50:22 +09:00
|
|
|
|
|
|
|
public function tags()
|
|
|
|
{
|
|
|
|
return $this->belongsToMany('App\Tag')->withTimestamps();
|
|
|
|
}
|
2018-06-02 23:33:16 +09:00
|
|
|
|
|
|
|
public function textTags()
|
|
|
|
{
|
2019-01-15 00:05:01 +09:00
|
|
|
return implode(' ', $this->tags->map(function ($v) {
|
|
|
|
return $v->name;
|
|
|
|
})->all());
|
2018-06-02 23:33:16 +09:00
|
|
|
}
|
2019-04-05 23:10:26 +09:00
|
|
|
|
|
|
|
public function likes()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Like::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeWithLikes(Builder $query)
|
|
|
|
{
|
|
|
|
if (Auth::check()) {
|
|
|
|
// (ejaculation_id, user_id) でユニークなわけですが、サブクエリ発行させるのとLeft JoinしてNULLかどうかで結果を見るのどっちがいいんでしょうね
|
2019-04-07 23:27:24 +09:00
|
|
|
return $query
|
|
|
|
->with(['likes.user' => function ($query) {
|
|
|
|
$query->where('is_protected', false)
|
|
|
|
->orWhere('id', Auth::id());
|
|
|
|
}])
|
|
|
|
->withCount([
|
|
|
|
'likes',
|
|
|
|
'likes as is_liked' => function ($query) {
|
|
|
|
$query->where('user_id', Auth::id());
|
|
|
|
}
|
|
|
|
]);
|
2019-04-05 23:10:26 +09:00
|
|
|
} else {
|
2019-04-07 23:27:24 +09:00
|
|
|
return $query
|
|
|
|
->with(['likes.user' => function ($query) {
|
|
|
|
$query->where('is_protected', false);
|
|
|
|
}])
|
|
|
|
->withCount('likes')
|
2019-04-05 23:10:26 +09:00
|
|
|
->addSelect('0 as is_liked');
|
|
|
|
}
|
|
|
|
}
|
2017-08-27 04:44:53 +09:00
|
|
|
}
|