brysonreece

brysonreece

Member Since 1 Year Ago

Norman, OK

Software Engineer at WellCaddie, LLC

Experience Points 8,460
Experience Level 2

1,540 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 74
Lessons
Completed
Best Reply Awards 1
Best Reply
Awards
  • start-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-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-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist 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.

12 Jul
1 year ago

brysonreece left a reply on Accessing Object Properties...

Okay, looking at the class definitions, it seems you can try

$location = app('geocoder')->geocode('London, UK')->get()->first(); 

dd($location->getCoordinates()->getLatitude());

brysonreece left a reply on Accessing Object Properties...

It looks like you can do

$geo = app('geocoder')->geocode('London, UK')->get(); 
dd($geo->coordinates->latitude);

brysonreece started a new conversation HasManyThrough Multiple Models?

Hey there!

I've come to know that with a table layout like

meters
   - id
a_sensors
   - id
   - meter_id
b_sensors
   - id
   - meter_id
c_sensors
  - id
   - meter_id

I can setup a hasMany Meter->Sensor relationship, as well as a belongsTo Sensor->Monitor. Using this, I know I can call $meter->a_sensors or $meter->b_sensors to get a relationship instance or collection of a specific sensors type.

My question is, I was wondering if it's possible to setup a relationship with this table format so that I may ALSO be able to call $meter->sensors to get a collection instance of all sensors, as well as $meter->sensors() to retrieve a query builder instance.

Bonus question:

If the above is possible, assume I also have the table

sensor_entries
  - id
  - sensor_id
  - sensor_type

Would it also be possible to call $meter->sensor_entries (as well as the () builder notation) and get the sensor entries associated with all meter sensors?

Thanks!

21 Jun
1 year ago

brysonreece left a reply on How To Stop Execution And Redirect?

I ended up solving this by creating a custom exception, throwing that exception in the checkCurrentPermissionsmethod, and then placing my Alert and Redirect in a

if ($exception instanceof MyCustomException) {
    // Alert
    // return Redirect
}

statement within my app/Exceptions/Handler.php render method.

brysonreece started a new conversation How To Stop Execution And Redirect?

I currently have a controller method that calls

Auth::user()->checkPermissions('admin', Auth::user()->currentTeam);

as a single statement.

The contents of this method are

// User Model
public function checkCurrentPermissions(string $role, Team $team) {
    // The $role string is the role value, we want the key
    $role = array_search($role, Team::ROLES);

    // If the User instance does not have permissions, redirect back
    if (!$this->hasTeamRolePermissions($role, $team) || !$this->currentTeam->is($team)) {
        return Redirect::back();
    }
}

My question is that since the Redirect response gets returned and nothing is done with it, code execution continues. Is there any way to stop execution and redirect back, regardless of where I am in the controller method?

Note, I could always do

// User Model
public function checkCurrentPermissions(string $role, Team $team) {
    // The $role string is the role value, we want the key
    $role = array_search($role, Team::ROLES);

    // If the User instance does not have permissions, redirect back
    if (!$this->hasTeamRolePermissions($role, $team) || !$this->currentTeam->is($team)) {
        return Redirect::back();
    }
    else {
        return null;
    }
}

and check if the result of the method call is null, however I'm trying to keep the

Auth::user()->checkPermissions('admin', Auth::user()->currentTeam);

to one line.

brysonreece left a reply on Ways To Determine Both Role And If CurrentTeam Is Model Team

It dies, but the middleware example I gave requires that I have a Team model associated with any other Model that may get encountered. This is less than ideal, and seems to be a very convoluted way of accomplishing the same task.

20 Jun
1 year ago

brysonreece left a reply on Ways To Determine Both Role And If CurrentTeam Is Model Team

@Cronix That's something I had considered, but it still doesn't solve the "middleware" issue. I could always remove the middleware and interact with the Model directly like you said, but it becomes tedious to have to include a wrapping if statement on every single Controller method.

brysonreece left a reply on Ways To Determine Both Role And If CurrentTeam Is Model Team

Note, I'm using Route Model Bindings to a pass an inferred model from the URI to the Controller.

It's worth noting to combat this, I've made sure every model that I utilize returns a "team" object and then have a middleware that attempts to parse the model from the URI and check permissions like so:

public function handle($request, Closure $next)
    {
        $route = $request->route();
        $model = $route->parameters[$route->parameterNames[0]];

        if (!(Auth::user()->isTeamAdmin($model->team)) && Auth::user()->currentTeam->is($model->team)) {
            Alert::message('Whoops!', 'You don\'t seem to have permission to do that.', 'error', ['toast' => true, 'position' => 'bottom', 'timer' => 3000, 'showConfirmButton' => false]);
            return Redirect::back();
        }

        return $next($request);
    }

However this convoluted workaround is hard to maintain and requires me to define a "team" attribute for every route model binding that may get ran through the middleware.

brysonreece started a new conversation Ways To Determine Both Role And If CurrentTeam Is Model Team

I was wondering, is there a simpler way to check if a user is performing an action on a model with correct permissions, while also checking they are performing actions on behalf of the current team?

For instance, I have a Team->Note relationship set up, so if I only want admins to edit Notes, my controller method looks like:

public function edit(Note $note) {
    if (Auth::user()->currentTeam->is($note->team) && Auth::user()->isTeamAdmin($note->team)) {
        // do work
    }
    else {
        // redirect back with error
    }
}

Now, at first glance it works and isn't bad, but that's a fairly long if statement to include in every admin method, especially if the number of Model subchildren grow (think if Note had a tree of children below it).

26 Apr
1 year ago

brysonreece left a reply on Retrieving Multiple Table Rows With Common Column

I was attempting to get data from all three table in separate rows.

I ended up solving this by adding a TYPES constant to Sensor.php like so:

public const TYPES = array(
    'A Sensor' => ASensor::class,
    'B Sensor' => BSensor::class,
    'C Sensor' => CSensor::class
);

and back in Container.php changed my sensors() method to:

public function sensors() {

    $sensors = collect([]);
    $sensor_types = array_values(Sensor::TYPES);

    for ($i = 0; $i < count($sensor_types); $i++) {
        $current_sensors = $sensor_types[$i]::where('monitor_id', $this->id)->get();
        $sensors = $sensors->merge($current_sensors);
    }

    return $sensors;
}

Might not be the most eloquent solution (no pun intended), but it works and easily allows me to expand my number of sensors later on without having to edit a number of functions.

brysonreece started a new conversation Multiple Table Rows With Common Column

Hi there, I'm currently trying to retrieve a collection of DB records that all have a common column, but seem to be running into a few issues with Eloquent. For example, my models are set up as follows:

ASensor.php

class ASensor extends Sensor { }

BSensor.php

class BSensor extends Sensor { }

CSensor.php

class CSensor extends Sensor { }

Sensor.php

abstract class Sensor extends Model {

    public function container() {
        return $this->belongsTo(Container::class);
    }
}

Container.php

class Container extends Model {

    public function a_sensors() {
        return $this->hasMany(ASensor::class);
    }
    
    public function b_sensors() {
        return $this->hasMany(BSensor::class);
    }
    
    public function c_sensors() {
        return $this->hasMany(CSensor::class);
    }
}

As you can see, each Sensor has a "container_id" column referencing the parent Container model. My problems arises when I try to implement an overall sensors method as follows:

Container.php

    ...
    
    public function sensors() {
        $sensors = DB::table('a_sensors')
            ->join('b_sensors', 'b_sensors.container_id', 'a_sensors.container_id')
            ->join('c_sensors', 'c_sensors.container_id', 'b_sensors.container_id')
            ->where('container_id', '=', $this->id)
            ->get();
            
        return $sensors;
    }

I (am attempting to and) would expect the above method would return a collection of ALL sensors that have the container_id equal to the current Container instance's ID. However, it only returns c_sensors that follow that pattern.

Any thoughts?

08 Mar
1 year ago

brysonreece started a new conversation Spark - Per Seat Target Types

Hey there, I'm currently working with a Laravel Spark instance that should allow users to customize subscriptions for different types of products.

To begin, I know I can call

Spark::chargeTeamsPerSeat('Projects', function ($user) {
    return $user->projects()->count();
});

Spark::teamPlan('Basic', 'team-basic')
    ->price(10)
    ->features([
        'First', 'Second', 'Third'
    ]);

in my app/Providers/SparkServiceProvider.php file.

As it stands, Spark seems to only allow (from what I can tell) mass assignment of one plan to each project instance.

A solution I'm looking for would allow a Team owner to customize plan types for each individual project; something similar to

Spark::chargeTeamsPerSeat('Projects', function ($user) {
    return $user->projects()->count();
});

Spark::teamPlan('Basic', 'team-project-basic')
    ->price(10)
    ->features([
        'First', 'Second', 'Third'
    ]);

Spark::teamPlan('Intermediate', 'team-project-intermediate')
    ->price(20)
    ->features([
        'First', 'Second', 'Third'
    ]);

Spark::teamPlan('Advanced', 'team-project-advanced')
    ->price(30)
    ->features([
        'First', 'Second', 'Third'
    ]);

allowing Teams to have a mix of "Basic", "Intermediate", and "Advanced" projects instances billed.

Any help would be greatly appreciated, thanks!

19 Feb
1 year ago

brysonreece started a new conversation Per Seat Target Types

Hey there, I'm currently working with a Laravel Spark instance that should allow users to customize subscriptions for different types of products.

To begin, I know I can call

Spark::chargePerSeat('Projects', function ($user) {
    return $user->projects()->count();
});

Spark::teamPlan('Basic', 'team-basic')
    ->price(10)
    ->features([
        'First', 'Second', 'Third'
    ]);

in my app/Providers/SparkServiceProvider.php file.

As it stands, Spark seems to only allow (from what I can tell) mass assignment of one plan to each project instance.

A solution I'm looking for would allow a Team owner to customize plan types for each individual project; something similar to

Spark::chargePerSeat('Projects', function ($user) {
    return $user->projects()->count();
});

Spark::teamPlan('Basic', 'team-project-basic')
    ->price(10)
    ->features([
        'First', 'Second', 'Third'
    ]);

Spark::teamPlan('Intermediate', 'team-project-intermediate')
    ->price(20)
    ->features([
        'First', 'Second', 'Third'
    ]);

Spark::teamPlan('Advanced', 'team-project-advanced')
    ->price(30)
    ->features([
        'First', 'Second', 'Third'
    ]);

allowing Teams to have a mix of "Basic", "Intermediate", and "Advanced" projects instances billed.

Any help would be greatly appreciated, thanks!

22 Dec
1 year ago

brysonreece left a reply on Eloquent HasMany Abstract Class Relationship

I solved this problem by emulating the Collection results often returned by an Eloquent relationship, as such:

public function getSensorsAttribute() {
    $types = [TemperatureSensor::class, LightSensor::class, AudioSensor::class];
    $sensors = collect();

    foreach($types as $type) {
        $sensors_of_type = $this->hasMany($type)->getResults();

        foreach($sensors_of_type as $sensor) {
            $sensors->push($type::find($sensor['id']));
        }
    }

    return $sensors;
}

brysonreece started a new conversation Eloquent HasMany Abstract Class Relationship

I'm trying to dynamically handle Eloquent child relationships for a Model I've created.

So far my approach has been the following:

public function sensors() {
    $types = [TemperatureSensor::class, LightSensor::class, AudioSensor::class];
    $sensors = collect();

    foreach(Sensor::TYPES as $type) {
        $sensors->concat($this->hasMany($type));
    }

    return $sensors;
}

However, I need the ability to return a relationship instance. All of the various Sensor types extend a generic Sensor class. Because of such, I've also tried

public function sensors() {
    return $this->hasMany(Sensor::class);
}

Any ideas?

brysonreece left a reply on Changing Default Tinker Namespace?

Running composer dumpauto fixed the issue.

brysonreece left a reply on Changing Default Tinker Namespace?

Having just tested it in-depth more, it seems that Tinker is picking up on the changed models except for User.php and Team.php (this is a Laravel Spark instance).

Any ideas?

brysonreece started a new conversation Changing Default Tinker Namespace?

I've recently changed the namespace of my Models to MyNamespace\Models.

Is there a way to change Tinker's default namespace to include these Models that way I don't have to import it every time?

06 Oct
1 year ago

brysonreece started a new conversation Relationships; Nested Parents?

Hi! I'm currently using a few Laravel models with hasMany and hasOne relationships. I was wondering if there was a way to retrieve "nested parents"?

For instance if I have School->Bus->Child model relationships, I know I can call Child->Bus->School to find out which School a specific Child belongs to. I am looking for a way to simply call Child->School as our relationships expand to more nested instances in the future.

e.g. State->County->City->School->Bus->Child being the hierarchy, but being able to call Child->City or Child->State and having it return the appropriate model.