Erti

Erti

Member Since 3 Years Ago

Experience Points
18,290
Total
Experience

1,710 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
180
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 4
18,290 XP
Jun
08
1 month ago
Activity icon

Replied to Weird Eloquent Bindings When Using AddSelect Method, Bindings Are Messing With Different Conditions.

Thank you very much @kalemdzievski This solved my issue although i still don't understand why this is happening as I have tried this some other times as well and was working fine! Anyway it looks like this solves it so Happy with it!

Activity icon

Started a new Conversation Weird Eloquent Bindings When Using AddSelect Method, Bindings Are Messing With Different Conditions.

  • Laravel Version: 7.13.0
  • PHP Version: 7.4.6
  • Database Driver & Version: Mysql & 8.0.20

Description:

There is a weird behaviour when i tried to use addSelect method on a database model. The bindings looks to have messed up and they are going on different parts of the query.

This is the eloquent query

$jobs = Postedjobs::with('skills')
        ->addSelect([
            'has_applied_for_job' => function ($query) {
                $query->selectRaw("job_proposals.proposal_id as has_applied_for_job")
                    ->from('job_proposals')
                    ->whereColumn('job_proposals.job_id', 'postedjobs.job_id')
                    ->where(function ($subquery) {
                        $subquery->where(function ($q) {
                            $q->where('proposable_type', User::class)->where('proposable_id', auth()->user()->id);
                        })
                        ->orWhere(function ($q) {
                            $myTeams = auth()->user()->ownTeams()->select('teams.team_id')->get();
                            $q->where('proposable_type', Team::class)->whereIn('proposable_id', $myTeams->toArray());
                        });
                    })
                    ->limit(1);
            }
        ])
        ->whereHas('user')
        ->whereDoesntHave('proposals', function ($query) {
            return $query->where('is_accepted', 'Y');
        })
        ->where('job_moderation', '2')
        ->where('available_until', '>', \Carbon\Carbon::now()->format('Y-m-d'))
        ->whereIn('job_status', ['1', '3'])
        ->where('job_action', '1')
        ->filter($request)
        ->get();

And this one is the raw query produced

SELECT
    `postedjobs`.*,
    (
    SELECT
        COUNT(*)
    FROM
        `job_proposals`
    WHERE
        `postedjobs`.`job_id` = `job_proposals`.`job_id`
) AS `proposals_count`,
(
    SELECT
        COUNT(*)
    FROM
        `user_chats`
    WHERE
        `postedjobs`.`job_id` = `user_chats`.`job_id`
) AS `messages_count`,
(
    SELECT
        job_proposals.proposal_id AS has_applied_for_job
    FROM
        `job_proposals`
    WHERE
        `job_proposals`.`job_id` = `postedjobs`.`job_id` AND(
            (
                `proposable_type` = 'App\User' AND `proposable_id` = 4
            ) OR(
                `proposable_type` = 'App\Models\Team\Team' AND `proposable_id` IN(1)
            )
        )
    LIMIT 1
) AS `has_applied_for_job`
FROM
    `postedjobs`
WHERE EXISTS
    (
    SELECT
        *
    FROM
        `users`
    WHERE
        `postedjobs`.`user_id` = `users`.`id`
) AND NOT EXISTS(
    SELECT
        *
    FROM
        `job_proposals`
    WHERE
        `postedjobs`.`job_id` = `job_proposals`.`job_id` AND `is_accepted` = 4
) AND `job_moderation` = 1 AND `available_until` > 'Y' AND `job_status` IN(2, '2020-06-07') AND `job_action` = 1

This is the query without the addSelect method

$jobs = Postedjobs::with('skills')
        ->whereHas('user')
        ->whereDoesntHave('proposals', function ($query) {
            return $query->where('is_accepted', 'Y');
        })
        ->where('job_moderation', '2')
        ->where('available_until', '>', \Carbon\Carbon::now()->format('Y-m-d'))
        ->whereIn('job_status', ['1', '3'])
        ->where('job_action', '1')
        ->filter($request)
        ->get();
SELECT
    `postedjobs`.*,
    (
    SELECT
        COUNT(*)
    FROM
        `job_proposals`
    WHERE
        `postedjobs`.`job_id` = `job_proposals`.`job_id`
) AS `proposals_count`,
(
    SELECT
        COUNT(*)
    FROM
        `user_chats`
    WHERE
        `postedjobs`.`job_id` = `user_chats`.`job_id`
) AS `messages_count`
FROM
    `postedjobs`
WHERE EXISTS
    (
    SELECT
        *
    FROM
        `users`
    WHERE
        `postedjobs`.`user_id` = `users`.`id`
) AND NOT EXISTS(
    SELECT
        *
    FROM
        `job_proposals`
    WHERE
        `postedjobs`.`job_id` = `job_proposals`.`job_id` AND `is_accepted` = 'Y'
) AND `job_moderation` = 2 AND `available_until` > '2020-06-07' AND `job_status` IN(1, 3) AND `job_action` = 1

As we can see above it looks like job_moderation should be 2 instead of 1 and available_until should be the date which in this case has jumped to job_status, is_accepted should be Y and job_status should be [1, 3]