when($type,function ($query) use ($type){ $query->where('type',$type); })->where('uuid',$uuid)->when($link_id,function ($query) use ($link_id){ $query->where('link_id',$link_id); })->where('cate',$cate); } public function recruitment(){ return $this->belongsTo(DataRecruitment::class,'link_id'); } public function teachingknowledge(){ return $this->belongsTo(DataTeachingKnowledge::class,'link_id'); } public function bestknowledge(){ return $this->belongsTo(DataBestKnowledge::class,'link_id'); } public function joblist(){ return $this->belongsTo(DataUserApplyJobInfo::class,'link_id'); } public function shipyard(){ return $this->belongsTo(DataShipyard::class,'link_id'); } public function scopeHasShipyard(Query $query){ $query ->where('type',5) ->whereExists( DataShipyard::where('id',Db::raw("{$this->getTable()}.link_id"))->buildSql() ); } public function user(){ return $this->belongsTo(DataUser::class,'uuid'); } public static function isFav($uuid,$type,$id,$cate=1){ return (bool)self::where('uuid',$uuid) ->where('cate',$cate) ->where('type',$type) ->where('link_id',$id) ->value('id'); } public static function saveFav($uuid,$type,$id,$cate=1,$save=true){ if($save==false){ return self::where('uuid',$uuid) ->where('cate',$cate) ->where('type',$type) ->where('link_id',$id) ->delete(); } if(self::isFav($uuid,$type,$id,$cate)){ return true; } return self::create([ 'uuid'=>$uuid, 'type'=>$type, 'link_id'=>$id, 'cate'=>$cate, ]); } }