Doing.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Relations\BelongsToUserTrait;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Support\Facades\DB;
  6. class Doing extends Model
  7. {
  8. use BelongsToUserTrait;
  9. protected $table = 'doings';
  10. protected $fillable = ['user_id', 'action','source_type','source_id','subject','content','refer_id','refer_user_id','refer_content','created_at'];
  11. public $timestamps = false;
  12. public static function concerned(User $user)
  13. {
  14. $attentions = $user->attentions()->get();
  15. $tags = $questions = $users = [];
  16. foreach($attentions as $attention){
  17. if($attention->source_type == 'App\Models\Tag'){
  18. $tags[] = $attention->source_id;
  19. }elseif($attention->source_type == 'App\Models\User'){
  20. $users[] = $attention->source_id;
  21. }elseif($attention->source_type == 'App\Models\Question'){
  22. $questions[] = $attention->source_id;
  23. }
  24. }
  25. /*追加用户标签*/
  26. foreach( $user->tags()->get() as $tag ){
  27. $tags[] = $tag->id;
  28. }
  29. if($tags){
  30. $taggables = DB::table("taggables")->whereIn("tag_id",$tags)->get();
  31. foreach($taggables as $tagable){
  32. if($tagable->taggable_type == 'App\Models\Question'){
  33. $questions[] = $tagable->taggable_id;
  34. }
  35. }
  36. }
  37. return self::where(function($query) use($users){
  38. $query->whereIn("user_id",$users);
  39. })
  40. ->oRwhere(function($query) use($questions){
  41. $query->whereIn("source_id",$questions)->where("source_type","=","App\Models\Question");
  42. })
  43. ->where('doings.user_id','<>',$user->id)
  44. //->where('attentions.created_at','<','doings.created_at')
  45. ->select('doings.*')
  46. ->orderBy('doings.created_at','DESC');
  47. }
  48. public static function newest(){
  49. return self::where("source_type","=","App\Models\Question")->orderBy('created_at','desc');
  50. }
  51. }