danhorton

danhorton

Member Since 2 Years Ago

Experience Points 4,300
Experience Level 1

700 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 32
Lessons
Completed
Best Reply Awards 1
Best Reply
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

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

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

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

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    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 Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

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

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

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

15 Dec
6 months ago

danhorton left a reply on Cashier And Cancel Subscription As Admin

$user = $id; $user->subscription('Testing')->cancel();

Youre calling subscription() on the ID integer value passed in the route. You would need $user to be user::find($id) or whatever.

danhorton left a reply on Str_limit Truncates At The Wrong Point

Would you really need the html if you're using str_limit presumably to show a snippet of the article? You could do {!! str_limit(strip_tags($news->content), 550) !!} but this removes all the html.

14 Dec
6 months ago

danhorton left a reply on My Paginator Don't Conserve The Url

If you mean you need to persist the query string parameters, you need to replace the paginator links() method with this

{{ $users->appends(request()->query())->links() }}

danhorton left a reply on Many Users Belong To One Team Which Belongs To One Project - Relationship Problems

@REALRANDYALLEN - Hi Randy

This does work so thank you, is there a better way of doing this by team be a singular object holding many users rather than a collection of teams->users being returned? The team is supposed to just be the "roster" of people associated with a project so there would never be an instance of there being more than 1 team for a project.

danhorton left a reply on Many Users Belong To One Team Which Belongs To One Project - Relationship Problems

I really don't get it, I have changed the relationships and it is still returning the same.

Bid.php

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

Team.php

    public function user()
    {
        return $this->belongsToMany('App\User', 'teams', 'id', 'user_id');
    }

User.php

    public function team()
    {
        return $this->belongsToMany('App\Team');
    }

dd($bid->team) returns this

Collection {#678 ▼
  #items: array:2 [▼
    0 => Team {#675 ▶}
    1 => Team {#676 ▶}
  ]
}

And trying to access users in the way I thought returns

@foreach($bid->team->user as $team)
Property [user] does not exist on this collection instance. 

danhorton left a reply on Many Users Belong To One Team Which Belongs To One Project - Relationship Problems

Hi,

The teams table:

id|bid_id|user_id|role_id|created_at|updated_at

2|1|1|1|2018-11-30 16:56:31.310|2018-11-30 16:56:31.310

4|1|3|1|2018-11-30 16:59:21.447|2018-11-30 16:59:21.447

Users

id|first_name|last_name
1|Dan|Dan
3|Assdsdd|fdfdf

danhorton started a new conversation Many Users Belong To One Team Which Belongs To One Project - Relationship Problems

Hi,

in the site I am building, there is a project which is the main component of the application. A project has one team, which in turn can have many users associated with it.

I have clearly forgotten how to set up relationships as I am having trouble with this.

A project has one team (although if i do it this way, laravel does a select top 1 query from the teams table, which holds multiple user records inside)

    public function team()
    {
        return $this->hasOne('App\Team');
    }

Then, a team has many users

    public function user()
    {
        return $this->hasMany('App\User', 'id', 'user_id');
    }

If I {{dd($bid->team)}} on a project with a team and users associated, it returns this:

Collection {#678 ▼
  #items: array:2 [▼
    0 => Team {#675 ▶}
    1 => Team {#676 ▶}
  ]
}

Which is 1 team object for every row in the teams database.

To access the users, I have to do $bid->team[0]->user->name etc rather than $bid->team->user->name.

What am I doing wrong here?

04 Dec
6 months ago

danhorton left a reply on Search Query Not Working With Min And Max Values

@MELODIA - Add as the first line in your PropertyFilters class

class PropertyFilters extends Filters
{
    
    protected $filters = ['city_suburb', 'type', 'min_price', 'max_price', 'bathrooms', 'bedrooms'];

public function city_suburb($city){
    if(!$city) {
       return $this->builder;
    } 
    return $this->builder->where('city', $city)->orWhere('suburb', $city);
}

public function type($type){
    if(!$type) {
       return $this->builder;
    } 
    return $this->builder->where('type', $type);
}
etc..

danhorton left a reply on Search Query Not Working With Min And Max Values

I'm pretty sure you need to do something like this

Controller

public function search_page(PropertyFilters $filters)
{
   $properties =  $this->getProperties($filters)

   return view('pages.search', compact('properties'));
 
} 
...
    protected function getProperties(BidFilter $filters)
    {
        $bids = Property::filter($filters);
        
        return $bids->paginate(9);
    }  

PropertyFilters.php


class PropertyFilters extends Filters
{
    
    protected $filters = ['city_suburb', 'type', 'min_price', 'max_price', 'bathrooms', 'bedrooms'];

.... 

danhorton started a new conversation Creating Collection Wiith Relationships (nesting) From 2 Query Builder Queries

I'm trying to create a collection from 2 query builder results. These execute correctly and return as expected, i'm now just trying to transform them into how I need.

        $bid = collect(DB::connection('sqlsrv')->table('Bids')->select('*')->where('crm_ref', '=', $sn)->get());
        $crm = collect(DB::connection('sqlsrv_crm')->table('OpportunityBase')->select('*')->where('new_SalesNumber', '=', $sn)->get());
dd($result = $bid->merge($crm));

This returns a collection with 2 items:

Collection {#496 ▼
  #items: array:2 [▼
    0 => {#452 ▶}
    1 => {#498 ▶}
  ]
}

How I want is as below (i.e. with the $crm nested into $bid->crm as eloquent does):

Collection {#496 ▼
  #items: array:1 [▼
    0 => {#452 ▶}
                  1 => {#498 ▶}
  ]
}

I also tried $bid->push($crm) however this just creates a flat collection.

danhorton left a reply on Filtering Model B/C From Model A Controller

@VAJID - Hi Vajid, thanks for your suggestion. Unfortunately this does not work as the databases are on different server instances (SQL server).

03 Dec
6 months ago

danhorton left a reply on Can Not Access/figure Out Custom Model Attributes

I ended up solving this by using this in the view

$bid->crm->teammember->where('Record2RoleIdName', 'Bid Manager')->pluck('Name')->implode(', ')}}

CRM Model

    public function teammember()
    {
        return $this->hasMany('App\TeamMember', 'Record1Id', 'OpportunityId');
    }

TeamMember Model

    public function crm()
    {
        return $this->belongsTo('App\CRM', 'Record1Id', 'OpportunityId');
    }

danhorton left a reply on Filtering Model B/C From Model A Controller

@VAJID - I have this defined already. Laravel does not seem to be able to differentiate between connections when using where/whereHas therefore I am trying to come up with a custom solution.

danhorton left a reply on Error Creating Elasticsearch Mapping With Elasticquent Lib

The route for select_vote/votes/_mapping needs a delete method

Route::delete('select_vote/votes/_mapping', '[email protected]');

danhorton started a new conversation Eager Loading Filtering Related Model On Different Database Instance

i'm trying to implement filtering as defined here.

I have two databases and with a different model on each. I am able to query and eager load these fine without any filtering. However when filtering the 2nd model (CRM), the relationship seems to break and only the CRM record(s) are loaded. This is also complicated by the fact that the 2 models are on different database servers/instances so i'm not able to use where/whereHas etc.

Here is an example

BidController.php


    public function index(BidFilter $filters)
    {
            $bids = $this->getBids($filters);
            return view('public.bids.index', compact('bids'));
    }    

protected function getBids(BidFilter $filters)
    {
        $bids = Bid::with('crm')->filter($filters);
        
        return $bids->paginate(10);
    }

BidFilter.php

    protected function sn($sn)
    {
        return $this->builder = DB::connection('sqlsrv_crm')->table('OpportunityBase')->select('*')->where('new_SalesNumber', '=', $sn);
    }

Like I said, the normal bid->crm relationship eager load works fine. Only when trying to filter CRM via the SN method in BidFilter.php, the relationship seems to disconnect between bid<->CRM and no Bid results are returned, only CRM.

danhorton left a reply on Seasons, Classes, And Students - Trouble Setting This Up

You would just assign the student to the class and not worry about the season. The season relationship would be through the Class class. HasManyThrough could be used to say find all students in Fall 19, which would be found from their relationship to class. The relationship to student would be defined on the Class class. Having a season class seems weird personally to me though. Would it not make more sense to have separate Class classes, with the dates they are running? Start/end etc.

02 Dec
6 months ago

danhorton left a reply on Error Creating Elasticsearch Mapping With Elasticquent Lib

405 is method not allowed, do you have a delete method defined in your routes file for select_vote/votes/_mapping?

30 Nov
6 months ago

danhorton left a reply on Filtering Model B/C From Model A Controller

Hi, thanks for your reply. This looks like it should work, however I forgot to mention that Model A and Model B sit on different database connections. This does not seem to pull through when using this

[SQL Server]Invalid object name 'Opportunity'. (SQL: select * from [bids] where exists (select * from [Opportunity] where [bids].[crm_ref] = [Opportunity].[sNumber] and [sNumber] = SN5184620))

Opportunity is the database that is coded in the CRM model and Bids is the connection in Model A.

danhorton started a new conversation Filtering Model B/C From Model A Controller

Hi,

i'm trying to implement filtering as defined [here] (https://github.com/JeffreyWay/council/tree/master/app/Filters).

This works (to filter model A from controller A), however controller A/model A has a relation to model B, which is what I want to filter, as well as a 3rd relationship to model C from model B.

Trying to mess around, I tried something like the below, which clearly does not work, however will hopefully illustrate what i'm trying to do. This filter gets applied to model A

    protected function sn($sn)
    {
        $s= Bid::where('crm', function ($query) {
            $query->where('sNumber', '=', 'SN5184620');
        })->get();

        return $s;
    }

Bid is model A/controller A, CRM is model B which is the one I want to filter.

I thought about having numerous different functions in the model to filter, however I don't know if this was the best solution and I thought it was better to get it all out into another class.

20 Nov
6 months ago

danhorton left a reply on Can Not Access/figure Out Custom Model Attributes

Thanks for the replies.

I have sort of managed to get this to work via a method which seems totally backwards and dirty. The only problem with it is that this creates 1 query per field retrieved. I eventualy need to pull more fields using the role so I figure this will end up creating tons of extra queries.

            $bids = Bid::where('mandatory', 1)->with('crm')->paginate(10);
            foreach($bids as $b)
            {  
                $team[$b->crm_ref]['Bid_Manager'] = TeamMember::role('Bid Manager', $b->crm->OpportunityId);
            }
            
            return view('public.bids.mandatory.index', compact('bids', 'team'));

I can then access this via $team[$bid->crm_ref]['Bid_Manager'].

I am using the following scope in the TeamMember model:

    public function scopeRole($query, $type, $opportunity)
    {
        return $query->where('Record2RoleIdName', $type)->where('Record1Id', $opportunity)->get();
    }

is there a better way of doing this?

danhorton left a reply on Can Not Access/figure Out Custom Model Attributes

@REALRANDYALLEN - Hi, if i put this on the TeamMember model (where i guess it should go, as that is where the Record2RoleId column is sourced from, it retrieves everything from the TeamMember model with no relation to the CRM or Bid Model.

(SQL: select * from [Connection] where [Record2RoleId] = SM and [record2objecttypecode] = 8 and [record1objecttypecode] = 3 and [connection].[statuscode] = 1)

This is the teamMember model

    public function crm()
    {
        return $this->belongsTo('App\CRM', 'Record1Id', 'OpportunityId');
    }

    public function scopeRole($query, $type)
    {
        return $query->where('Record2RoleId', $type);
    }

CRM Model

    public function bid()
    {
        return $this->belongsTo('App\Bid');
    }

    public function teammember()
    {
        return $this->hasMany('App\TeamMember', 'Record1Id', 'OpportunityId');
    }

Bid model

    public function crm()
    {
        return $this->hasOne('App\CRM', 'New_SalesNumber', 'crm_ref')->with('teammember');
    }

    public function teammember()
    {
        return $this->hasManyThrough('App\TeamMember', 'App\CRM', 'new_salesnumber', 'Record1Id', 'crm_ref', 'opportunityid');
    }
19 Nov
6 months ago

danhorton left a reply on Can Not Access/figure Out Custom Model Attributes

Hi, thanks for your detailed replies.

There is no direct relationship from Bid<-> TeamMember. They are related on Bid->CRM->TeamMember.Does a hasManyThrough relationship need to be set up?

The data returned for TeamMember looks as below

Record2IdName   Record2Id   rolename
glq qkoq    11FA6105-A015-E111-1C11-00505611001N    Project Director
glq qkoq    11FA6105-A015-E111-1C11-00505611001N    Team Member
mooqm qqrqmxxorf    56E50EN1-1F15-E111-1C11-00505611001N    Project Manager
mqkof fqqgqqrx mgroqqmqg    QF117F0C-EACA-E711-10E5-005056N11N1F    Project Quality Leader
mqkof fqqgqqrx mgroqqmqg    QF117F0C-EACA-E711-10E5-005056N11N1F    Team Member
qgr Lqmxoolq Lqrqgm Q1A0111N-1E15-E111-1C11-00505611001N    Account Director
qgr Lqmxoolq Lqrqgm Q1A0111N-1E15-E111-1C11-00505611001N    Sales Director
qgr Lqmxoolq Lqrqgm Q1A0111N-1E15-E111-1C11-00505611001N    Team Member
qqcoqgl fomx    1111QF6A-711N-E111-A1N1-00505611001N    Sales Manager
qqcoqgl fomx    1111QF6A-711N-E111-A1N1-00505611001N    Team Member
qqcoqgl fomx    1111QF6A-711N-E111-A1N1-00505611001N    Team Member
qqcoqgl q qgqgrqgm  11A1661C-A015-E111-1C11-00505611001N    Project Quality Reviewer
qqqg Olg Vqlqgm A11Q1111-1F15-E111-1C11-00505611001N    Sales Manager
qqqg Olg Vqlqgm A11Q1111-1F15-E111-1C11-00505611001N    Sales Support
xqm oorqom  1110FQ17-717N-E111-10F1-005056N11QQ1    Team Member

I'd like to be able to have the rolename an Record2IdName accessible via $bid->crm->team->manager / $bid->crm_team->director or something similar. Would scope be the best way t do this?

or is there a way in the controller to pass $team = TeamMember::where('Record2RoleId', '11A1661C-A015-E111-1C11-00505611001N'); multiple times for different types of roles?

danhorton left a reply on Can Not Access/figure Out Custom Model Attributes

Thank you

Is there a way that I can do this without 1 query per attribute, as I would be looking to retrieve around 5 users with this and I do not want 5 extra queries.

The whole of the team associated to the record is returned by $bid->crm->teammember.

Would I need to set up a Team model if I wanted to do $bid->crm->team->manager / $bid->crm_team->director etc?

danhorton started a new conversation Can Not Access/figure Out Custom Model Attributes

Hi, i'm trying to add an attribute or figure out a way to structure my data model in a different way.

I have a "TeamMember" model attached to a record, which returns the team associated to it. I can access these fine through:

            {{$teammember->Name}}  {{$teammember->Record2RoleIdName}} {{$teammember->Record2RoleId}} <br />
            @endforeach```

I am trying to add an additional collection/attribute to the TeamMember model so I can access/lookup by role name. E.g. $bid->crm->teammember->sales_manager.

I have tried to implement this via attributes added on to the CRM class, i.e.

protected $appends = ['sales_manager'];

.... public function getSalesManagerAttribute() { return $this->where('Record2RoleId', '=', 'SM'); }


However this attribute returnas a Builder instance when returned which I can not figure out:

Builder {#400 ▼ #query: Builder {#433 ▶} #model: CRM {#402 ▶} #eagerLoad: array:1 [▶] #localMacros: [] #onDelete: null #passthru: array:13 [▶] #scopes: [] #removedScopes: [] }


Any guidance appreciated.
24 Sep
8 months ago

danhorton left a reply on Database Seeding Primary Key Integrity Constraint Violation

Hi, this does technically work but it seems to generate 450 bids (50*8) and only generates actions up to and including the 50th bid created. After that there are 400 standalone bids created with no actions.

Any way to only generate 50 bids?

21 Sep
8 months ago
20 Sep
8 months ago

danhorton left a reply on Database Seeding Primary Key Integrity Constraint Violation

It seems that somehow it's bringing the actions primary key into the SQL when I haven't specified it anywhere. With it not being specified id have assumed it would have auto incremented as normal.

danhorton started a new conversation Database Seeding Primary Key Integrity Constraint Violation

Hi, i'm trying to seed my database with 50 random "bids" which will each have a number of actions assigned to each of them.

I have spent about 5 hours trying to figure this out but it all comes back to the same error.

 SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY' (SQL: insert into `actions` (`bid_id`
  , `bid_stage`, `date_assigned`, `due_date`, `assigned_to`, `owner`, `status`, `notes`, `id`, `updated_at`, `created_at`) values (
  52, LawnGreen, 1994-08-09 04:49:41, 2006-09-16 17:07:32, Mrs. Emmanuelle Bahringer III, Lulu Schimmel, Port, Distinctio eligendi
  et unde. Voluptas provident eos repellat nobis ab. Velit voluptates at sit. Nemo quo labore perspiciatis et reprehenderit rem min
  ima odit. Quae consectetur error beatae veritatis et id eum veniam. Amet ut veritatis cum. Aut necessitatibus et quod sed dolore
  et. In voluptatem rerum temporibus maxime aut. Est odio aut est ea. Perspiciatis veritatis tempore a recusandae. Voluptates tempo
  re voluptatum et suscipit. Adipisci maxime officia et quo. Nihil eum ad quis et non ea cum., 1, 2018-09-20 21:11:33, 2018-09-20 2
  1:11:33))


In Connection.php line 458:

  SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1' for key 'PRIMARY'

Here is the bids seeder

    public function run()
    {
        $bids = factory(App\Bid::class, 50)->create()->each(function ($bid){
            $actions = factory(App\Action::class, 8)->make();
            $bid->action()->saveMany($actions);
            #$bid->action()->saveMany(factory(App\Action::class, rand(1,15))->make());
        });
    }
}

Actions Seeder

    public function run()
    {
        $actions = factory(App\Action::class)->create();
    }

Actions factory

$factory->define(App\Action::class, function (Faker $faker) {
    return [
        'bid_id' => factory('App\Bid')->create()->id,
        'bid_stage' => $faker->colorName,
        'date_assigned' => $faker->dateTime,
        'due_date' => $faker->dateTime,
        'assigned_to' => $faker->name,
        'owner' => $faker->name,
        'status' => $faker->cityPrefix,
        'notes' => $faker->text(500)

Actions migration

        Schema::create('actions', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('bid_id');
            $table->foreign('bid_id')->references('id')->on('bids');
            $table->string('bid_stage');
            $table->date('date_assigned');
            $table->date('due_date');
            $table->string('owner');
            $table->string('assigned_to');
            $table->string('status');
            $table->string('notes', 500);
            $table->timestamps();