【问题标题】:How to convert query builder to eloquent如何将查询生成器转换为 eloquent
【发布时间】:2020-09-11 20:30:10
【问题描述】:

我一直在试图弄清楚如何实现匹配系统,但遇到了困难。 我已经设法在控制器中构建了一个查询,该查询的功能与我想要的完全相同,但我想将其转换为 Eloquent 模型,因为图像已损坏并且还可以访问我的模型中的某些功能。

这是我希望转换的控制器中的查询生成器(如果可能的话)- 我正在检查用户是否都“喜欢”彼此(类似于 Tinder):


class MatchedEmployersController extends Controller
{
    public function index()
    {
        $matches = DB::table('applicant_likes')
        ->join('employers', 'employers.id', '=', 'applicant_likes.liked_employer_id')
        ->whereExists(function ($query) {
            $query->select(DB::raw(1))
                ->from('employer_likes')
                ->whereRaw('employer_likes.employer_id = applicant_likes.liked_employer_id');
        })
        ->get();

        return view('applicant.employers.matched', compact('matches'));
    }
}

这是申请者模型,下面我将逻辑提取为可用的特征

App\Models\Applicant

class Applicant extends Authenticatable
{
    use Notifiable, LikeableEmployer, MatchableEmployer;

    //

    public function getAvatarAttribute($value)
    {
        return asset($value ?: '/images/default-avatar.jpeg');
    }
}

App\Trais\LikeableEmployer

trait LikeableEmployer
{
    public function likeEmployer(Employer $employer)
    {
        return $this->likedEmployers()->save($employer);
    }

    public function unlikeEmployer(Employer $employer)
    {
        return $this->likedEmployers()->detach($employer);
    }

    public function toggleLikeEmployer(Employer $employer)
    {
        if ($this->likingEmployer($employer)) {
            return $this->unlikeEmployer($employer);
        }
        return $this->likeEmployer($employer);
    }

    public function likingEmployer(Employer $employer)
    {
        return $this->likedEmployers()->where('liked_employer_id', $employer->id)->exists();
    }

    public function likedEmployers()
    {
        return $this->belongsToMany(Employer::class, 'applicant_likes', 'applicant_id', 'liked_employer_id');
    }
}


最后,这里应该放置匹配的逻辑


namespace App\Traits;

use App\Traits\LikeableApplicant;
use App\Traits\LikeableEmployer;

trait MatchableEmployer
{
    use LikeableApplicant, LikeableEmployer;

    public function matchedEmployers()
    {
        //
    }
}

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    您需要创建一个用于存储匹配项的表。我们来看下面的例子。

    relationshipstable: id | from | to,如果我们有一对,那就是一对。示例:

    id | from | to
    1  | 1    | 2
    2  | 2    | 1
    

    现在创建Relationship 模型

    class Relationship extends Model
    {
        public static function getMatch($user_id)
        {
            return self::leftJoin('relationship reverse', 'relationship.to', '=', 'reverse.from')->where('relationship.from', 'reverse.to')->where('relationship.from', $user_id)->get();
        }
    }
    

    现在您只需拨打User::getMatch('any_user_id');

    【讨论】:

    • 我没有提到我已经有两个具有两个不同用户表的身份验证保护器,并且还使用了所有设置正确的数据透视表,但我会更新我的问题,谢谢您的宝贵时间!
    【解决方案2】:

    首先,为您在此查询中使用的每个表创建模型,然后添加以下关系。

    在ApplicantLike模型中

    public function employer(){
         return $this->belongsTo('App\Employer','liked_employer_id','id');
      }
    

    在雇主模式中

     public function likes(){
         return $this->hasMany('App\EmployerLike','employer_id','id');
      }
    

    在您的 MatchedEmployersController 中确定

    public function index()
    {
        $matches = ApplicantLike::with('employer','employer.likes')
         ->has('employer')
         ->has('employer.likes')
         ->get();
    
         // dd($matches); // try with this first
        return view('applicant.employers.matched', compact('matches'));
    }
    

    试试上面的代码,我将你给定的代码转换成 ORM,但我认为你为你需要的逻辑实现了错误的逻辑。如果有任何问题,请回复我,我会帮助您。

    【讨论】:

    • 感谢您的建议,我想我必须更新我的问题,因为我使用两个保护身份验证并且还使用数据透视表。
    猜你喜欢
    • 2021-10-14
    • 2020-07-09
    • 2015-07-21
    • 2020-10-13
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    相关资源
    最近更新 更多