ctyler

Member Since 3 Years Ago

Experience Points
20,780
Total
Experience

4,220 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
191
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.

  • Community Pillar

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

Level 5
20,780 XP
Apr
09
4 days ago
Activity icon

Replied to Connecting One Laravel App To Another Over API In Scheduled Task

Awesome response as always rodrigo.pedra.

Shared storage would be much easier. I will need to do some testing on both but I am leaning towards keeping them completely separate but I want to make sure, my discomfort is not from ignorance - it probably is.

Thank you rodrigo.pedra.

Activity icon

Started a new Conversation Connecting One Laravel App To Another Over API In Scheduled Task

Hello, I am working on a project that entails creating an intranet website application that is not accessible from internet. I am also creating an external application that will be used for mass communication for employees. On the intranet side there is an LDAP application that people use for creating new users in Active Directory. When new users are created on the intranet, I want these to also be pushed out to the notification application. I can do that easily enough using Laravels API functionality. However, I would like the ability for a scheduled task to run that could check all the user IDs on the intranet side with all of the users of the communication side and remove users on the communication side. I could connect to the communication app database directly from the intranet application over the internet - but I am not comfortable with that.

So my question is how can I connect to the communication app over the Laravel API without a user actually logging in and doing it securely like you would need to do in a scheduled task.

Feb
26
1 month ago
Activity icon

Replied to Trying To Get Logic Out Of Blade Nested Relationship

Wow. This is unbelievable. There is no more important resource you can give someone than their time. I am very appreciative.

I am definitely checking out those resources. I will try each one and test it. A lot of these people have been doing what they are doing for many years so I can see the trainings being quite large overtime. I appreciate the multiple solutions!

Activity icon

Replied to Trying To Get Logic Out Of Blade Nested Relationship

Hello rodrigo.pedra, That is so close. I know I maybe trying to do to much in the nested relation. I wanted to attach a screen shot of the report that is working in the Blade file but I dont seem to have that option. I think you have a good grasp of it. So i need to loop over all of the Requirements.

For each requirement I loop through all of the users in the division. For each user grab the latest training using trainings.date_taken field – only the latest one because that is the only one that matters as far as requirement compliance. It determines compliance by comparing trainings.date_taken to the requirements.recur_months taking the requirements.due_cycle (due on anniversary or calendar year or one time) into consideration.

If a user is in compliance for that particular requirement they will be skiped and should not even appear on the report. If they are not in compliance show the last training and the trainings.date_taken.

Your query

$division = Division::query()
            ->with([
                'requirements',
                'users.trainings' => function ($relation) use ($id) {
                    $relation->whereHas('requirements', function ($query) use ($id) {
                        $query->whereKey($id);
                    });
                },
            ])
            ->find($id);

I was wondering if you can just get the latest training from the whereHas.

Here is the loop

//Iterate over all the requirements for the division
        foreach($division->requirements as $requirement) {
            echo '-' . $requirement->name . '<br>';
            //Loop over all of the users for the divsion
            foreach ($division->users as $user) {
                if ($user->trainings->count() > 0) {
                    //Is there Any way to just return the latest training?
                    //Do the comparison with the $requirement.recur_months and $requirement.due_cycle
                    //If in compliance skip
                    continue;
                    //
                } else {
                    //else list the user and the latest training for requirement
                    echo '--' . $user->firstname . ' ' . $user->lastname . '<br>';
                }
            }

A few questions concerning your query. Where is the $relation variable coming from. Does Laravel make that available automatically? Also, I would love to become more proficient with eloquent and relationships. The online documentation is very good but I would not be able to derive the query you came up with from it. Do you have any recommendations?

Feb
25
1 month ago
Activity icon

Started a new Conversation Trying To Get Logic Out Of Blade Nested Relationship

I have an app that tracks training requirement compliance by Division.

I have Divisions that have a many to many relationship with requirements.

Division Model

public function requirements()
    {
        return $this->belongsToMany('App\Requirement')
            ->withTimestamps();
    }

I have users that have a many to many relationship with divisions.

User Model

public function divisions()
    {
        return $this->belongsToMany('App\Division', 'division_user')->withTimestamps();
    }

I have a one to many relationship with users and trainings.

User Model

public function trainings() 
    {
        return $this->hasMany('App\Training');
    }

I have a many to many relationship with trainings to requirements. Training Model

public function requirements()
    {
        return $this->belongsToMany('App\Requirement', 'training_meets_requirement')
            ->withTimestamps();
    }

I have a report that is currently in a blade file that works well but I want to move it into separate class.

This is a report that by the division I need to

  1. get a list of requirements for that division
  2. Loop through the requirements
  3. Under each requirements list all the users that belong to that division
  4. Under each user get the latest training for that user for that requirement and then check if it is in or out of compliance.

The logic in the blade template works 100%:


@foreach($requirements as $requirement)
        <table class="table table-bordered table-sm">
            <caption style="caption-side: top">#{{ $requirement->id }} {{ $requirement->name }}: Due
                every {{ $requirement->recur_months }} months on {{$requirement->due_cycle}}</caption>
            <thead>
            <tr>
                <th scope="col">#</th>
                <th scope="col">First</th>
                <th scope="col">Last</th>
                <th scope="col">Last Training</th>
                <th scope="col">Date Taken</th>
                <th scope="col"></th>
            </tr>
            </thead>
            <tbody>
            @foreach($users as $user)
                @php
                    $training = $user->trainings()->whereHas('requirements', function(\Illuminate\Database\Eloquent\Builder $query) use ($requirement) {
                        $query->where('requirements.id', $requirement->id);
                    })->latest('date_taken')->first();
                @endphp
                {{--            Check if any training exists--}}
                @if(!$training)
                    <tr class="table-danger">
                        <td>{{ $user->id }}</td>
                        <td>{{ $user->firstname }}</td>
                        <td>{{ $user->lastname }}</td>
                        <td>No training on record</td>
                        <td></td>
                        <td></td>
                    </tr>
                    @continue
                @else
                    {{--            Check if requirement is a one time training and make sure there is a training--}}
                    @if($requirement->due_cycle == 'one time'))
                        @continue
                    @endif
                    {{--                Get the due date for a training from the requirement and the training--}}
                    @php
                        $dueDate = \App\lib\GetDueDate::perform($requirement, $training);

                        $today = \Carbon\Carbon::now();
                        $dueDateMinus30Days = $dueDate->copy()->subDay(30);
                    @endphp
                    @if($dueDateMinus30Days->lt($today))
                        <tr class="{{ $today->between($dueDateMinus30Days, $dueDate) != true ? 'table-danger' : 'table-warning' }}">
                            <td>{{ $user->id }}</td>
                            <td>{{ $user->firstname }}</td>
                            <td>{{ $user->lastname }}</td>
                            <td>{{ $training->name }} </td>
                            <td>{{ $training->date_taken->format('m-d-Y') }}</td>
                            <td>@if($today->between($dueDateMinus30Days, $dueDate)) Expires within 30 days @endif</td>
                        </tr>
                    @endif
                @endif
            @endforeach
            </tbody>
        </table>
    @endforeach

As you can see by the @php statements it needs to be in an outside file. I will also need to reuse this for mailed reports.

Here is the code that I cannot seem to get correct

        $q = Division::with('requirements')
            ->with('users.trainings')
            ->whereHas('requirements', function ($q)  use($id) {
                $q->where('requirements.id', '=', $id);
            })
            ->orderBy('name', 'desc');
        $q->where('divisions.id', $id);

        $division = $q->first();

The first thing is this is not right

 ->whereHas('requirements', function ($q)  use($id) {
                $q->where('requirements.id', '=', $id);
            })

The ID is for the division. I need to get the trainings that have the same requirements as the division that is used in the outer with statement (Division::with('requirements'))

I hope this makes sense.

Feb
24
1 month ago
Activity icon

Started a new Conversation How To Land On A Named Anchor From A Redirect.

So I have a an course application that include attendance and grade taking. I have the attendance set up as a radio checkbox for absent or present and it is set to submit on change.

When it submit it updates the users attendance but when the page reloads, it reloads to the top of the page. I have tried adding the users ID as the on page anchor (id="1234" and name="1234") end append that to the returned url via #1234.

This is super frustrating for me but for my users it is unbearable. I plan to move this to ALAX however the anchor would be the best short term solution. But more important than that. Why would that not work?

Has anyone else experienced this?

Feb
23
1 month ago
Activity icon

Replied to Unable To Use WhereNotIN With Where And/ OrWhere

OMG. @michaloravec thank you so much I appreciate your time. I have such a hard time with precedence and grouping. Your the best!

Activity icon

Started a new Conversation Unable To Use WhereNotIN With Where And/ OrWhere

Hello Everyone, I have a class that has enrollment. I have a field that is populated with ajax. It is one field that uses a LIKE condition on three columns or my users table. It work perfectly when I use

$data = DB::table('users')
                ->select('users.id', 'users.firstname', 'users.lastname', 'users.email')
                ->where('firstname', 'LIKE', "%{$query}%")
                ->orwhere('lastname', 'LIKE', "%{$query}%")
                ->orwhere('email', 'LIKE', "%{$query}%")
                ->orderBy('lastname')
                ->take(25)
                ->get();

the problem is, I do not want users already enrolled to show up as options. So if I do something like this

            $data = DB::table('users')
                ->select('users.id', 'users.firstname', 'users.lastname', 'users.email')
                ->whereNotIn('users.id', $usersEnrolled)
                ->where('firstname', 'LIKE', "%{$query}%")
                ->orwhere('lastname', 'LIKE', "%{$query}%")
                ->orwhere('email', 'LIKE', "%{$query}%")
                ->orderBy('lastname')
                ->take(25)
                ->get();

I am getting enrolled users from

$usersEnrolled = $enrollment->users()->pluck('id');

Anyone have any idea how I can get this to work?

Feb
14
1 month ago
Activity icon

Awarded Best Reply on Returning Model And Relationship With Multiple Optional Conditions

Ok. I was able to figure this one out. This is how I got it to work in case someone is looking.

$query = Order::query();
        $query->when($transid, function ($q) use($transid) {
            return $q->where('transaction_id', '=', $transid);
        });
        $query->when($amount, function ($q) use($amount) {
            return $q->where('amount', 'LIKE', '%' . $amount . '%');
        });
        $query->when($firstname, function ($q) use($firstname) {
            return $q->whereHas('user', function ($query) use($firstname) {
                $query->where('firstname', 'LIKE', '%' . $firstname . '%');
            });
        });
        $query->when($lastname, function ($q) use($lastname) {
            return $q->whereHas('user', function ($query) use($lastname) {
                $query->where('firstname', 'LIKE', '%' . $lastname . '%');
            });
        });
        $orders = $query->sortable(['created_at' => 'desc'])
            ->paginate(25);
Feb
13
2 months ago
Activity icon

Replied to Returning Model And Relationship With Multiple Optional Conditions

Ok. I was able to figure this one out. This is how I got it to work in case someone is looking.

$query = Order::query();
        $query->when($transid, function ($q) use($transid) {
            return $q->where('transaction_id', '=', $transid);
        });
        $query->when($amount, function ($q) use($amount) {
            return $q->where('amount', 'LIKE', '%' . $amount . '%');
        });
        $query->when($firstname, function ($q) use($firstname) {
            return $q->whereHas('user', function ($query) use($firstname) {
                $query->where('firstname', 'LIKE', '%' . $firstname . '%');
            });
        });
        $query->when($lastname, function ($q) use($lastname) {
            return $q->whereHas('user', function ($query) use($lastname) {
                $query->where('firstname', 'LIKE', '%' . $lastname . '%');
            });
        });
        $orders = $query->sortable(['created_at' => 'desc'])
            ->paginate(25);
Feb
11
2 months ago
Activity icon

Replied to Returning Model And Relationship With Multiple Optional Conditions

The Order model has:

public function user()
    {
        return $this->belongsTo(User::class);
    }

User Model has:

public function orders()
    {
        return $this->hasMany(Order::class);
    }

Does that answer your question.

Activity icon

Started a new Conversation Returning Model And Relationship With Multiple Optional Conditions

So I have an order table that has a belongs to relationship with users. So I want to be able to search results on the users table columns as well as the orders table columns. I think I am almost there but this query is not working.

        $orders = Order::has('user')
            ->when($transid, function($query, $transid) {
                return $query->where('transaction_id', 'LIKE', '%' . $transid . '%');
            })
            ->when($firstname, function($query, $firstname) {
                return $query->where('firstname', 'LIKE', '%' . $firstname . '%');
            })
            ->sortable()
            ->paginate(25)

Firstname is in the users table

This works fine:

        $orders = Order::has('user')
            ->when($transid, function($query, $transid) {
                return $query->where('transaction_id', 'LIKE', '%' . $transid . '%');
            })
            ->sortable()
            ->paginate(25)

I tried $orders = Order::with('user')

No joy.

Anybody have any idea how to accomplish?

Jan
15
2 months ago
Activity icon

Started a new Conversation Add Session Message When Redirecting From Auth Middleware

Good morning. I am working on an application that requires a user to be logged in to complete certain items. Is there a way to add a session message from a controller action that is protected by the auth middleware?

For instance. I do not want users to add items to their cart unless they are logged in so the [email protected] is protected by the auth middleware. When the user redirects to the login page I would like to display a message that says "You must log in to add items to your cart."

Dec
07
4 months ago
Activity icon

Replied to How To Record Product Orders

So lets say I do want stats. My question is the best practice on recording " record in the orders tables the characteristics you want to stat (color options, ...)." How would you do that. Json column in the order table. A separate table with order items.

Activity icon

Replied to How To Record Product Orders

Hi sr57, My question is how you do that in the orders table. Would you use just a single order table, or orders and orders_items tables, then copy the product details to the order_items table?

Activity icon

Started a new Conversation How To Record Product Orders

Hello all, I have a generalized question on an eCommerce site and the recording of orders/invoices.

So if you have a list of products on your site that has a name, price, etc. When someone purchases you would create an order. As part of that order you would list the product with the name, price etc. The issue that I am curious about is how the order/invoice is recorded. So you really cant list the name/price directly from the database. Wouldn't you have to copy this data to another table? Otherwise if you simply connect to the ID of the product from the order you will get the current price in the database. What if the name and/or price changes after the order is placed?

Do you have an orders and order_item tables that you copy to? Do you use an order table with a json field to record item names, prices etc. I am curious how you handle this issue and what you would consider best practice.

This effects a lot of my projects. I am currently working on an education site that generates certificates. The certificate has a course name/credit hours etc. The certificate should list these items from when the course was taken. You probably do not want it updated with the current name/price just in case it changes.

Any help, advice, and/ or suggestions would be very much appreciated.