mankowitz

mankowitz

Member Since 2 Years Ago

Experience Points
1,780
Total
Experience

3,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
5
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 1
1,780 XP
Mar
20
1 week ago
Activity icon

Started a new Conversation HasManyThrough For Many To Many Relationship

I have several tables that are linked through many-to-many relationships. How can I use eloquent to query them? In my scenario, user has a many-to-many relationship with agencies, and sites can have one agency.

tables

  1. users: id
  2. agencies: id
  3. agency_user: user_id, agency_id
  4. sites: agency_id

Models are named traditionally: User, Agency, Site

In SQL, I'd do this SELECT * FROM users u JOIN agency_user au ON (au.user_id = u.id) JOIN agencies a ON (au.agency_id = a.id) JOIN sites s ON (s.agency_id = a.id)

I tried using

class User ....
    {

    public function agencySites()
    {
        return $this->hasManyThrough("App\Site", "App\Agency");
    }

with no luck. Any ideas?

Feb
27
1 month ago
Activity icon

Replied to Limit Returned Fields From `Auth::user`

@tisuchi - with your query, I get the user, but not the related role information

=> App\User {#4104
     name: "Scott Mankowitz",
     id: 2,
     role_id: 1,
   }

@sinnbeck - This works! Thanks!

=> App\User {#4170
     name: "Scott Mankowitz",
     id: 2,
     role_id: 1,
     role: TCG\Voyager\Models\Role {#3947
       name: "admin",
       id: 1,
     },
   }
Activity icon

Replied to Threads For Queries?

This is a complicated question, and there are probably many answers. First of all, you should make sure that your database is properly indexed. A good index can speed your query tenfold or more. Second, you may be able to optimize your data access with some eager loading. For example, if all of your tables are related to that user ID, you may be able to condense them into one query using with. Finally, if the database access is still too slow, consider loading a skeleton of the page and then using AJAX to load the rest.

Feb
26
1 month ago
Activity icon

Started a new Conversation Limit Returned Fields From `Auth::user`

I want to send the name and role of the current user to Vue. I can't figure out how to send what I want.

Auth::user()->load('role') gives this:

=> App\User {#3974
     id: 2,
     role_id: 1,
     name: "xxxx xxxxx",
     email: "[email protected]",
     office_phone: "(414) 858-818",
     avatar: "users/default.png",
     settings: null,
     created_at: "2017-11-21 17:10:28",
     updated_at: "2020-01-15 06:32:14",
     notification_email: null,
     notification_frequency: "i",
     notification_sites: 0,
     deleted_at: null,
     last_login: "2020-01-15 06:32:14",
     token_2fa: "29580",
     token_2fa_expiry: "2030-01-01 00:00:00",
     supervisor_id: null,
     last_login_ip: "127.0.0.1",
     role: TCG\Voyager\Models\Role {#3977
       id: 1,
       name: "admin",
     },
   }

But if I try Auth::user()->load('role:id,name')->only(['name','id','role_id','role.name'])

I get:

=> [
     "name" => "Scott Mankowitz",
     "id" => 2,
     "role_id" => 1,
     "role.name" => null,
   ]

If I try User::with('role:name,id')->find(Auth::user()->id)->select(['name','id','role_id'])->get() then I get a list of every user.

Dec
29
3 months ago
Activity icon

Started a new Conversation HasManyThrough To Self

I have two models: Patient and SiteCensus. A Patient belongs to only one SiteCensus, but a SiteCensus can have many patients. I am looking for a way to eager load all the patients that share a SiteCensus. Below is the relevant models:

class Patient extends Model
{
    public function siteCensus()
    {
        return $this->belongsTo("App\SiteCensus", "sites_census_id");
    }

}

and

class SiteCensus extends Model
{
    
    public function patients() 
    {
        return $this->hasMany("App\Patient", "sites_census_id", "id");
    }

}

So, some of this works well. If I want to find the other patients associated with a SiteCensus, I can do this:

$p = Patient::find(35);
$p->siteCensus->patients;

But, what I really want to do is eager loading and show all the patients that share this patient's SiteCensus. Spolier alert: the following does not work.

class Patient extends Model
{
    public function previousVisits()
    {
        return $this->hasManyThrough("App\Patient", "App\SiteCensus")->where('id', '<>', $this->id);
    }
}

It gives this error:

Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sites_census.patient_id' in 'field list' (SQL: select `patients`.*, `sites_census`.`patient_id` 
as `laravel_through_key` from `patients` inner join `sites_census` on `sites_census`.`id` = `patients`.`site_census_id` where `sites_census`.`patient_id` = 44 and `id` <> 44 and `patients`.`deleted_at` is null)'

What is the eloquent way to do this?

(in SQL, I'd do this: SELECT * FROM patients p1 WHERE p1.sites_census_id = (SELECT sites_census_id FROM patients WHERE id = ?) AND p1.id <> ?

Nov
16
4 months ago
Activity icon

Started a new Conversation Subquery Includes Deleted Rows

OK, I know this is a bit complicated, but here goes.

A site has many patients, machines and meetings. Given a particular machine, I'd like to see the patients that are associated with it. When I give the following query, it includes patients who are soft-deleted. How can I enforce that only meetings with non-deleted patients are shown?

$site=Site::with('meetings', "meetings.patient", "meetings")->whereHas('machines', function($q) use($machine){ $q->where("machine_name", $machine); })->has("patients")->get()

yields

=> Illuminate\Database\Eloquent\Collection {#4113
     all: [
       App\Site {#4037
         id: 1,
         name: "Nursing Home 1 (basic)",
         meetings: Illuminate\Database\Eloquent\Collection {#4111
           all: [
             App\ZoomMeeting {#4084
               id: 1,
               patient_id: 29,
               site_id: 1,
               patient: App\Patient {#4094
                 id: 29,
                 first_name: "Ryan",
                 last_name: "Hill",
                 site_id: 43,
                 deleted_at: null,
               },
             },
             App\ZoomMeeting {#4104
               id: 2,
               provider_id: 2,
               patient_id: 1,
               site_id: 1,
               patient: null,
             },
           ],
         },
       },
     ],
   }

Note that in the second ZoomMeeting (#4104) patient is null.

Here are the relevant portions of my Models

class MachineName extends Model
{
    public function site()
    {
        return $this->belongsTo('App\Site');
    }
}
class Site extends Model
{
    use SoftDeletes;

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

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

    public function meetings()
    {
        return $this->hasMany('App\ZoomMeeting');
    }
class ZoomMeeting extends Model
{
    use SoftDeletes;
    
    public function patient()
    {
        return $this->belongsTo('App\Patient');
    }
    
    public function site()
    {
        return $this->belongsTo('App\Site');
    }

}