phpMick

Systems Developer at UK Manufacturing Company

Experience

46,240

22 Best Reply Awards

  • Member Since 2 Years Ago
  • 136 Lessons Completed
  • 2 Favorites

17th April, 2018

phpMick left a reply on Store URL, Then Return Back After A Redirect. • 1 week ago

Yes, but sometimes, I want to go to the create group page, without redirecting to the previous.

In normal use, the show page comes after the create.

phpMick started a new conversation Store URL, Then Return Back After A Redirect. • 1 week ago

Hi,

Not sure how to describe this.

I want to store a place in a workflow, so that I can return back to it, after being directed somewhere else.

eg:

Adding a new user. 1, Choosing users group from a dropdown. 2, Pick the "add a new group option" - this then sends the user, to the new group page. 3, When new group is added, I want to go back to the page for adding a new user, so that they can choose the new group.

Should I just store the URL in session, then look for it, after adding the new group?

Just wondered if there was a clever way of doing this?

Thanks,

Mick

3rd April, 2018

phpMick started a new conversation Remove Empty Key From JSON. • 2 weeks ago

Does anyone have a clever way to do this?

I have some JSON (coming from https://github.com/lazychaser/laravel-nestedset), this is a small part of it:

1","description":"culpa","parent_name":null,"children":[]}]}]

I want to remove the children element when it is empty.

So, if it looks like this "children":[] remove it.

I could replace it with a space by using str_replace, I just wondered if there was anything better?

Thanks,

Mick

26th March, 2018

phpMick left a reply on Too Few Arguments To Function. • 4 weeks ago

Yes that got it, I tried use but missed the parenthesis.

Thanks.

phpMick started a new conversation Too Few Arguments To Function. • 4 weeks ago

Will someone please point out the problem with this code, I'm sure it's obvious but I can't see it. I thought this was how I was supposed to pass a parameter?

$allGroups = Cache::remember($keyName,1440 , function ($userGroupId) {

            return self::getGroupHierarchy($userGroupId);

        });

Type error: Too few arguments to function App\Models\UserGroup::App\Models\{closure}(), 0 passed in /home/vagrant/code/portal/vendor/laravel/framework/src/Illuminate/Cache/Repository.php on line 327 and exactly 1 expected (View: 

23rd March, 2018

phpMick started a new conversation How To Load/store Large Hierarchy. • 1 month ago

Hi,

Just wondered if anyone had any ideas about this, before I jump in.

1, I have a complicated hierarchy (user groups) which I need to build. This will be built by a recursive function which needs some logic which I cannot think of another way to achieve.

It needs to traverse down a tree (https://github.com/lazychaser/laravel-nestedset) but it needs to stop when it hits certain types of nodes.

I don't really want to do this with multiple SQL queries, so I am thinking of bringing back the tree, into a collection, then traversing through the collection somehow. This should be quicker because I won't be doing all the SQL queries.

Any thoughts on how to do this, has anyone done anything similar?

2, I then need to store this structure, so that I don't need to keep recreating it. I was originally thinking cache but as this structure is specific to the user, would it be better in the session?

Thanks,

Mick

22nd March, 2018

phpMick left a reply on Filled Form Not Validating • 1 month ago

As above.

I would rename the fields in the form, then they can go straight in the db.

Is this what everyone else does?

phpMick left a reply on Where To Put Redirect Login After Login. • 1 month ago

I was asking where to put the logic (for a conditional redirect), not where to change the redirect route.

The answer is add an authenticated method to the LoginController and put the logic in there.

phpMick left a reply on Testing Login Redirect. • 1 month ago

I had to do this to get that code working (L5.5):

$this->actingAs($user);

$test = $this->get(route('home'));

No, authenticated in LoginController still does not get called.

If I do this from the GUI, it works, this is only failing in test.

Mick

phpMick started a new conversation Testing Login Redirect. • 1 month ago

Hi,

I have some code with a redirect for some users on login: (https://laracasts.com/discuss/channels/general-discussion/where-to-put-redirect-login-after-login) .

I am now struggling to tests this, how can get this code to run in a test?

This is where I am:

 public function user_without_group_gets_message_page()
    {

        $user = create(User::class,['user_group_id' => null, 'role' => User::USER, 'password' => 'password']);

        //try posting to login

        $test = $this->post(route('login'),[
            'email' => $user->email,
            'password' => 'password'

        ]);

    }

Authenticated() in LoginController is not called. Any ideas why?

Mick

phpMick left a reply on Where To Put Redirect Login After Login. • 1 month ago

This is nothing to do with permissions.

This is how I solved it:

 protected function authenticated($request, $user)
    {
        if ( is_null($user->user_group_id )) {

            Auth::logout();

            session(['message' => 'blah blah blah]);

            return view('message-page');

        }else {
            return redirect('/home');
        }
    }

phpMick left a reply on Where To Put Redirect Login After Login. • 1 month ago

Thanks,

I just want to know the best place to put the login that will kick them out and redirect.

phpMick started a new conversation Where To Put Redirect Login After Login. • 1 month ago

If my user does not belong to any user groups, I want to kick them out with a redirect to a message page.

Where is the best place to put this logic?

Thanks,

Mick

20th March, 2018

phpMick left a reply on Function Name Must Be A String - View Composer • 1 month ago

Got it:

View::composer('partials.nav.right-menu-admin', function ($view) {
            $view->with('userGroups', UserGroup::all);
        });

Swapped $view()-> to be $view->.

Not sure where that came from?

Mick

phpMick started a new conversation Function Name Must Be A String - View Composer • 1 month ago

Hey,

What am I doing wrong here:

  View::composer('partials.nav.right-menu-admin', function ($view) {
            $view()->with('userGroups', UserGroup::all);
        });

Thanks,

Mick

phpMick started a new conversation Collection In Navbar. • 1 month ago

I need a collection (user groups) in my navbar on all pages.

Is this a job for a view composer?

Mick

14th March, 2018

phpMick left a reply on Forcing A Unique Rule To Ignore A Given ID In Form Request • 1 month ago

Yes, this is pretty much what I did, thanks guys.

phpMick left a reply on Validation • 1 month ago

Yes, I create a form request for each model and abstract the validation into that, just keeps my controllers neater.

13th March, 2018

phpMick left a reply on Forcing A Unique Rule To Ignore A Given ID In Form Request • 1 month ago

Yes, I use the same form request for both store new and update existing.

phpMick left a reply on Forcing A Unique Rule To Ignore A Given ID In Form Request • 1 month ago

The only problem is that it fails for creating new users.

Trying to get property of non-object

Because $user is not set.

I guess the only way round this is to check if there is a $user and choose to include the rule or not?

I don't think I had to this last time I did this?

phpMick left a reply on Forcing A Unique Rule To Ignore A Given ID In Form Request • 1 month ago

How do I use that in my array?:

return [
                'name'=>'required',
                'email'=>'required|email|unique:users,email'
}

I need to get this in somehow?:

Rule::unique('users')->ignore($user->id),

Mick

phpMick left a reply on Forcing A Unique Rule To Ignore A Given ID In Form Request • 1 month ago

Yes I am binding, yes that works!

phpMick left a reply on Forcing A Unique Rule To Ignore A Given ID In Form Request • 1 month ago

It's not the authenticated user I need, it is the one I am currently editing.

phpMick started a new conversation Forcing A Unique Rule To Ignore A Given ID In Form Request • 1 month ago

Can someone please remind me how to do this in L5.5?

I am editing a user, the email field is unique.

Like this:

Validator::make($data, [
    'email' => [
        'required',
        Rule::unique('users')->ignore($user->id),
    ],
]);

but in a form request.

This is what I have:

 return [
                'name'=>'required',
                'email'=>'required|email|unique:users,email'
}

It looks like I need the $user->id but I don't have the $user in my form request.

Pretty sure I have done this before.

Mick

9th March, 2018

phpMick left a reply on I Am Losing All Data On My Local Laravel Database • 1 month ago

Great, did you sort this?

I run my tests from PhpStorm, using an SQLite database.

phpMick left a reply on How To Debug A Redirect. • 1 month ago

I think it is. If you are logged in and go to this page, it redirects to home.

Not sure where this happens.

phpMick left a reply on How To Debug A Redirect. • 1 month ago

No, but I was logged in.............

phpMick started a new conversation How To Debug A Redirect. • 1 month ago

Is this even possible?

I get a 302 back to home, when I visit password/reset (it shows in route:list).

How can I work out what is happening and why it is redirecting?

phpMick left a reply on TDD For Event Listener. • 1 month ago

Swapped it for a factory make, then ``toArray()```.

phpMick left a reply on TDD For Event Listener. • 1 month ago

It's named incorrectly, it is just making an array.

Not sure why I'm not using a factory, I will revisit that.

Thanks.

phpMick left a reply on TDD For Event Listener. • 1 month ago

This is the finished test:

/**
     * @test
     * @throws \Exception
     */
    public function create_user_sends_email()
    {
        Mail::fake();

        $user = $this->signInMasterAdmin();

        $customer = $this->createAndSelectChildCustomer($user);

        $newUser = $this->createUser($user);

        $this->POST(route('users.store'),$newUser);

        Mail::assertSent(NewUser::class, function ($mail) use ($newUser) {
            return $mail->hasTo($newUser['email']);
        });

    }

phpMick left a reply on TDD For Event Listener. • 1 month ago

Brilliant, thanks!

I have now written my next test, which does not have

$this->expectsEvents(UserCreated::class); and the listener is getting called.

Thanks for your help, you have saved me a lot of time.

phpMick left a reply on TDD For Event Listener. • 1 month ago

Nope I'm just doing the POST which calls the code which triggers the event.

phpMick left a reply on TDD For Event Listener. • 1 month ago

That certainly sounds like the correct strategy.

Funny thing is that my event is firing but the listener is not getting called (when running tests).

When the application runs, the listener gets triggered correctly.

I wonder if this is phpUnit configuration?

Mick

phpMick left a reply on Event Or Queue • 1 month ago

Thanks almost got this working now , using the murze.be method with some modification.

8th March, 2018

phpMick left a reply on Event Or Queue • 1 month ago

I'm trying to follow this:

https://murze.be/sending-a-welcome-mail-with-laravel-53

But, it isn't quite working (token is now being hashed).

I can't help feeling like I am reinventing the wheel, do I really need to do this?

I just want to send a welcome e-mail with a password reset. Seems so similar to a standard password reset, except the message is different.

phpMick left a reply on I Am Losing All Data On My Local Laravel Database • 1 month ago

Are you doing any testing?

phpMick left a reply on Event Or Queue • 1 month ago

I only gave Martin the best answer because he needs the points :-).

Yes, got the event working OK, thanks.

Trying to do the listener now, I'm going to skip the queue for now, then come back to it when I have the mail and the reset working.

phpMick started a new conversation TDD For Event Listener. • 1 month ago

Ok, my event is getting fired.

How on earth do I then test the listener is doing what it should?

Thanks,

Mick

phpMick left a reply on Event Or Queue • 1 month ago

Great, thanks.

So we are basically saying that the answer is both.

Fire an event and then the listener puts a job in a queue.

This way it is just a background task.

phpMick started a new conversation Event Or Queue • 1 month ago

Quick question:

When an admin creates a new user, they are sent a welcome email. I'm not sure where to handle this.

Should I:

1, fire an event when they are created and send the welcome email from the listener? 2, place a job which handles sending the e-mail and place it in a queue?

Thanks,

Mick

7th March, 2018

phpMick left a reply on Can I Directly Access Request Variables? • 1 month ago

My current project had a few of these in and I wondered why:

$request->input('name');

phpMick started a new conversation Can I Directly Access Request Variables? • 1 month ago

Is it ok to do this:

$request->group_id

PhpStorm keeps scolding me for using magic methods.

Mick

phpMick left a reply on Getting Value From Vue Component Into HTML. • 1 month ago

Thanks Mike,

Not done Vue for a few months and it's taking a while to get back into it.

phpMick left a reply on Getting Value From Vue Component Into HTML. • 1 month ago

"Huh? How can it not appear in the DOM? Are you sure about that?"

No..... :-) I think it wasn't getting set.

phpMick left a reply on Getting Value From Vue Component Into HTML. • 1 month ago

I am using a standard HTML POST, when the submit button is clicked. (Outside of the Vue app).

I need Vue to update a DOM element which will be included when I POST the form.

An element in a parent component could be modified from a child but it will wont be included when I submit the form. Nothing inside the <div id="app"> is.

phpMick left a reply on Getting Value From Vue Component Into HTML. • 1 month ago

I don' think that this is possible.

Nothing inside <div id="app"> ever actually appears in the DOM, so won't get in the POST.

I guess the only answer is AJAX?

6th March, 2018

phpMick started a new conversation Getting Value From Vue Component Into HTML. • 1 month ago

I have a component which I am using like this:

<div id="app"> {{--The Vue application--}}
     <bss-tree options-string="{{$userGroups}}"
        title="{{$title}}"
        initial-id = "{{$user->user_group_id}}">
                                      
      </bss-tree>
</div> {{--app--}}

The component has a data attribute called selectedID.

How can I get this from the component, into the HTML, so that it will be included in the POST when I submit the page?

I’m sure there is a simple way of doing this?

Thanks,

Mick

phpMick left a reply on Rename A JSON Key. • 1 month ago

No problem, nice one, I like that solution.

phpMick left a reply on Rename A JSON Key. • 1 month ago

You mean like this:

$userGroups = UserGroup::select("id", "name AS label","type","_lft","_rgt","parent_id","properties","parent_name")
                    ->get()
                    ->toTree()
                    ->toJson();

Cool.

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.