phpMick

Member Since 5 Years Ago

Experience Points
61,360
Total
Experience

3,640 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
149
Lessons
Completed
Best Reply Awards
34
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 13
61,360 XP
Apr
09
3 days ago
Activity icon

Replied to Test TokenCan()

I'm just sticking the middleware back in for this test and creating a token with the correct scope.

Activity icon

Replied to Test TokenCan()

I don't see how that helps?

Activity icon

Started a new Conversation Test TokenCan()

Hi,

I'm trying to test some code which is protected by tokenCan() (https://laravel.com/docs/8.x/passport#checking-scopes-on-a-token-instance).

How can I test this code? The tokenCan() always fails because middleware is disabled in my tests (and therefore there isn't a token).

Cheers,

Mick

Mar
30
1 week ago
Activity icon

Started a new Conversation Profanity Filter.

Has anyone done any profanity filtering/validation recently?

I'm wondering if https://github.com/snipe/banbuilder is the right choice?

Cheers,

Mick

Mar
29
2 weeks ago
Activity icon

Replied to Update Has Many Which Has Many In One Hit.

This is has many though isn't it?

Activity icon

Started a new Conversation Update Has Many Which Has Many In One Hit.

A Topic has many Mails which have many SentMails.

Mails have a topic_id. SentMails have a mail_id.

How can I update sent_mails->sent using Eloquent?

Cheers,

Mick

Mar
16
3 weeks ago
Activity icon

Replied to Laradock Or Not?

This was asked 8 months ago, for a new project.

We went with Laradock, I had previously used Docker without it.

I think it's great, saves a lot of time.

It's not slow for me on Ubuntu but Docker was horrific on Windows.

Cheers,

Mick

Mar
08
1 month ago
Activity icon

Replied to MergeWhen And Nulls

Trying to get property 'uuid' of non-object

It always tries to get the uuid:

$this->mergeWhen(false, [
                'thing_uuid' => $this->thing->uuid
            ])
Activity icon

Started a new Conversation MergeWhen And Nulls

Hi,

What is the correct way to do this in a resource:


$this->mergeWhen($this->thing->exists(), [
                'thing_uuid' => $this->thing->uuid
            ])

I get this:

Call to a member function exists() on null

So it tries to get the uuid even when the condition fails.

What is the correct way to handle this?

Mar
04
1 month ago
Activity icon

Replied to Add Attribute To Collection For Returning.

I'm doing both now. I originally started using resources but struggled when I needed to pass the $contact.

Activity icon

Replied to Add Attribute To Collection For Returning.

Doesn't matter.

They get stripped out because they are not in $visible!

Activity icon

Replied to Add Attribute To Collection For Returning.

They get lost here:

return $things;

This is an API and this is where it returns the response.

The new fields get lost when the collection of models is serialised.

Activity icon

Started a new Conversation Add Attribute To Collection For Returning.

Is there a way to do this:

$things = $user->things;

$things->each(function ($item, $key) use ($contact){
      $item->read = $item->latest_message->getReadStatus($contact);
 });

return $things;

$item->read does not appear in the response. Is there any way to get it in? I was thinking of appending an accessor but I need to pass the $contact to it.

Mar
03
1 month ago
Activity icon

Replied to Eloquent OrderBy Field From Related Model.

ooooooo I dunno about that, someone else might sweep in with a better way :-)

Activity icon

Replied to Eloquent OrderBy Field From Related Model.

Yeah, I think that looks good.

Thanks.

Activity icon

Replied to Eloquent OrderBy Field From Related Model.

Yeah, that's what I tried: Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.

Activity icon

Started a new Conversation Eloquent OrderBy Field From Related Model.

A user has threads containing messages, so I can do this:

$user->threads->load('messages')

How can I order by the messages->created_at. I want threads with recent messages to be first.

Cheers,

Mick

Feb
25
1 month ago
Activity icon

Replied to Assert Number Of E-mails Sent.

Yes, agreed. I was trying to do that but had something wrong. Works with your example.

Thanks.

Feb
24
1 month ago
Activity icon

Started a new Conversation Assert Number Of E-mails Sent.

Is there a way to assert the number of e-mails that are sent?

I have two recipients, one has unsubscribed. I want to assert that only the subscribed recipient gets the mail.

Feb
17
1 month ago
Activity icon

Replied to Laravel Factories - Different Types Of Models.

I have this working now, using states.

Still not sure that it is the best way.

Activity icon

Replied to Laravel Factories - Different Types Of Models.

Yes, that looks right.

So what is the best way to create two different types of model?

So something like this:

Factory that creates emails:

$factory->define(Message::class, function (Faker $faker) {

   //just for example
    $emailType = 1;  
    $smsType = 2;

    return [
        'subject' => $faker->sentence,
        'content' => $faker->sentence,
        'message_type_id' => $emailType
    ];
});

Then I want to create another factory that makes smss.

I'm sure I've done this before, to create different types of users.

I had two factories and they had arguments, so I could pass user or admin.

Cheers,

Mick

Feb
16
1 month ago
Activity icon

Started a new Conversation Laravel Factories - Different Types Of Models.

I'm trying to create a model factory which creates different types of models. I'm trying to do this using states.

https://laravel.com/docs/7.x/database-testing#factory-states

Is this right:

$factory->state(ScheduledMessage::class, 'email', [
    'message_id' => \factory(Message::class,['message_type_id' => $emailType])->create()->id,
]);

$factory->state(ScheduledMessage::class, 'sms', [
    'message_id' => \factory(Message::class,['message_type_id' => $smsType])->create()->id,
]);


$factory->define(ScheduledMessage::class, function (Faker $faker) use ($emailType) {
    return [
        'parent_id' => null,
    ];
});

I am getting this Error : Maximum function nesting level of '256' reached, aborting! so it looks like it is recursing somehow?

Cheers,

Mick

Feb
10
2 months ago
Activity icon

Replied to Iterate Through Collection And Return Results Of Function.

Brilliant. I love a good one liner.

(Although it makes a collection, not an array).

Activity icon

Started a new Conversation Iterate Through Collection And Return Results Of Function.

Hi,

I currently have this:

        foreach ($collection as $model){
            $array[] =  $model->getName();
        }

Is there a better way to do this? Using map or each?

Cheers,

Mick

Jan
27
2 months ago
Activity icon

Replied to Returning One-to-many Results.

Yeah, I already use them for returning models or collections of model.

Just wondered if it's the right thing to do here. I already have a template resource which returns just the template model without all the related models.

Activity icon

Replied to Returning One-to-many Results.

Now that is what I'm talking about!

Awesome, I really appreciate you taking the time to look at this. All the data is in there that I need.

I just need to get rid of the fields I don't want, what's the best way to do that? only()?

Activity icon

Replied to Returning One-to-many Results.

Now I'm using a real example :-(

Tables

templates

sent_mails

template_id recipient_id

recipients

events

sent_mail_id

Relationships

  • A template has many sent_mails.
  • A sent_mail has one recipient.
  • A sent_mail has many events.

What I need to do is retrieve the recipients and the events for a template. This the results I am trying to get to:

 {
    "recipient": "joe blogs",
    "events": [
      {
        "sent": "1/1/2001",

      },
      {
         "opened": "2/1/2001"
      }
    ]
  },

Not sure how I am going to do this. I think maybe nested eager loading should get me further.

Mick

Activity icon

Replied to Returning One-to-many Results.

Yes, I think that is what I was after (I think I need to start watching Laracasts again!). Just wonder if it will still work when I add in the other tables/relationships.

Activity icon

Started a new Conversation Returning One-to-many Results.

I'm getting confused here and maybe someone can help me.

I will start with a simple example and add the complexity later.

Let's say that I have Users and Posts.

I want to return all my users and details about their posts.

Like this:

[
{
"user": "joe blogs"
"posts":
	[
		"name":"test", "topic":"cars",
		"second":"test", "topic":"boats",
	]
}

{
"user": "jane smith"
"posts":
	[
		"name":"whatever", "topic":"beer",
		"second":"next", "topic":"wine",
	]
}
]

I don't need this exact format.

Can someone point me in the right direction please?

This is in an API and I am using resources, so maybe I could do something like this: https://laravel.com/docs/8.x/eloquent-resources#relationships

Mick

Activity icon

Replied to How To Run Laravel Project On Localhost ?

If you want the easiest way, I would suggest Homestead.

Activity icon

Replied to SQL Gurus? How Can I Pivot This?

Is that better than using CASE?

Activity icon

Replied to SQL Gurus? How Can I Pivot This?

Like this?:

	SELECT Date(at),
       SUM( CASE WHEN message_status_id =5 THEN 1 ELSE 0 END) AS 'fives',
       SUM( CASE WHEN message_status_id =6 THEN 1 ELSE 0 END) AS 'sixes'
Activity icon

Started a new Conversation SQL Gurus? How Can I Pivot This?

Hi,

Crikey, my SQL skills are fading.

How can I pivot this data:

count is just a count of ids

date, event, count
2020-12-27,click,14
2020-12-27,open,19
2020-12-28,open,18
2020-12-28,click,22

so that I can get this:

date, click-count, open-count
2020-12-27,14,19
2020-12-28,18,22

I think I'm going to try with a CASE.

Cheers,

Mick

Jan
22
2 months ago
Activity icon

Replied to Model Losing Attributes After Using SetRawAttributes()

And after a bit more thought, I decided to save both the mutated content and original content to separate fields in the db (in a mutator).

My show and edit routes require the original content and I don't want to "unmutate" it for that

Jan
21
2 months ago
Activity icon

Replied to Model Losing Attributes After Using SetRawAttributes()

Yeah OK, I get you. I will give that a whirl.

Simple always gets my vote.

Thanks.

Activity icon

Replied to Model Losing Attributes After Using SetRawAttributes()

Updated the original post.

Activity icon

Started a new Conversation Model Losing Attributes After Using SetRawAttributes()

When I do this

$thing->setRawAttributes(['content' => "hi"]);

my $thing model seems to lose it's other attributes. I tried setting the sync parameter and it did not make any difference.

Any ideas?

Updated to add context I am actually doing this in an Observer when the model is created. I want to set the raw attribute at this point. so that I can bypass the mutator.

Jan
13
2 months ago
Activity icon

Replied to Collection With Created_at From Pivot Table.

Thanks, I used a resource in the end.

Activity icon

Replied to Collection With Created_at From Pivot Table.

Yeah, sorry. I updated my last post.

Activity icon

Replied to Collection With Created_at From Pivot Table.

Hurrah (post updated)!

The timestamps are not include because they are not in my $fillable array!

If I remove the $fillable array they appear.

Now, how do I get the invited.created_at to be on the same level as the user fields?

Activity icon

Replied to Collection With Created_at From Pivot Table.

I have the relationship like this:

return $this->belongsToMany(User::class)->as('invited')->withTimestamps();

So I can see the intermediate model in the Collection, the question is how to get it in the JSON.

Activity icon

Started a new Conversation Collection With Created_at From Pivot Table.

Hi,

I have tables users and groups and therefore group_user.

group_user has timestamps and they are included in the group->users relationship

Now I want the created_at to be included in the JSON response when requesting the users which belong to a group.

So I have a function which basically does this:

return $group->users;

So, the JSON should look like this:

[{,"first":"Stephanie","last":"Roberts","mobile":"+123456","email":"[email protected]", "created_at":"*****"}]

The created_at should be when the pivot record was created, not the user record.

I will also rename the created_at field.

Jan
08
3 months ago
Activity icon

Replied to Full Text Search.

Cheers, I already did that.

Activity icon

Replied to Full Text Search.

Hi @michaloravec,

No. I didn't use your code. I don't like pasting code I don't understand.

Can you explain why this doesn't work?

SELECT * FROM
    thingies WHERE
    MATCH (title) AGAINST ('*sing' IN BOOLEAN MODE);

Cheers,

Mick

Activity icon

Replied to Full Text Search.

So it looks like LIKE is better than MATCH because I can search for %sin% ?

Activity icon

Replied to Full Text Search.

Then why doesn't this work:

select * from
              thingies where
      MATCH (title) AGAINST ('usin') 

but this does:

select * from
              thingies where
      MATCH (title) AGAINST ('using') 

Activity icon

Replied to Full Text Search.

Is MATCH better than LIKE?

How can I use a wildcard with MATCH?

eg Searching for the word testing, I want to be able to use the search term esti

the same as this:

select * from thingy WHERE title LIKE '%earc%';
Activity icon

Replied to Full Text Search.

Yeah, so just use a MATCH, I will give that a go. Cheers.

Activity icon

Started a new Conversation Full Text Search.

I have a field which I need to do a text search on (MySQL).

What is the current thinking on how should i do this? Probably looking for a simple solution, this may be the only time it is used in this API.

Cheers,

Mick