phpMick

Systems Developer at UK Manufacturing Company

Experience

42,580

20 Best Reply Awards

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

22nd January, 2018

phpMick started a new conversation Testing OAuth (Azure) Should I Be Using Mocking? • 45 minutes ago

Hi,

I want to test my code, which allows users to log in using OAuth via their Microsoft accounts.

Should I be using Mocking to do this? So, I fake the GETs to and from the Microsoft site?

Thanks,

Mick

19th January, 2018

phpMick started a new conversation Struggling With Azure OAuth • 3 days ago

Hi,

I am using: https://github.com/thephpleague/oauth2-client and https://github.com/TheNetworg/oauth2-azure to allow users to register/login using their MS accounts.

I have the first bit working: Users click the login with MS button. Request is then sent and received for auth code. Request is then sent and received for token.

I then create a new user in my application and log them in.

What should happen the second time they visit my application? Should they click the button again and somehow be logged in using the token?

Thanks,

Mick

15th January, 2018

phpMick left a reply on Validation If Blocks. • 6 days ago

Ahhh @wilk_randall , thanks, I seem to remember that's how I used to do it.

phpMick left a reply on Validation If Blocks. • 6 days ago

This question is nothing to do with validation.

I use form requests and custom rules in other parts of this application but they are not suitable here.

I am really asking about the control structure of having a list of if`` blocks which execute functions andreturn``` if the function fails, so that the following functions are not executed.


if(!functionPasses){
    return false; 
}

if(!nextFunctionPasses){
    return false; 
}

if(!thirdFunctionPasses){
    return false; 
}

I don't really like having multiple exit points from my function (unless it really makes sense).

Mick

phpMick left a reply on Validation If Blocks. • 1 week ago

In the same class.

No, these are custom validations, performed after the request has been validated.

phpMick started a new conversation Validation If Blocks. • 1 week ago

Hi,

I have notice that recently I have started writing a lot of code like this:

private function validationChecks()
{

    if(!validateSomething){
        $this->failureMessage = "a reason";
        return false;

    }

    if(!validateSomethingElse){
        $this->failureMessage = "another reason";
        return false;

    }

return true;


}

Has anyone got a better way of doing this?

Thanks,

Mick

11th January, 2018

phpMick left a reply on Breaking Out Of A Foreach • 1 week ago

I was trying that:

collect(data_get($array, '*.types.*'))->where('description', 'test')->first()['className']

But this would only return one row, what I want to do is filter out some of the array elements.

So if this is the original array, I want to maintain it's structure, but only include the elements from the 'inner array' where the description = "find".

array:2 [
  0 => array:2 [
    "provider" => "John"
    "types" => array:4 [
      0 => array:3 [
        "id" => 101
        "description" => "find"
        "className" => "One"
      ]
      1 => array:3 [
        "id" => 104
        "description" => "find"
        "className" => "Two"
      ]
      2 => array:3 [
        "id" => 105
        "description" => "dont"
        "className" => "Three"
      ]
      3 => array:3 [
        "id" => 106
        "description" => "dont"
        "className" => "Four"
      ]
    ]
  ]
  1 => array:2 [
    "provider" => "Chris"
    "types" => array:2 [
      0 => array:2 [
        "id" => "201"
        "description" => "find"
      ]
      1 => array:2 [
        "id" => "202"
        "description" => "dont"
      ]
    ]
  ]
]

This is how I am currently doing it:

 //$providers is the original array
 
 foreach($providers as $provider){
            $types = $provider['types'];
            $creatable = array_where($types,function($value,$key){
                return $value['description'] == 'find';
            });
            $filtered['provider'] = $provider['provider'];
            $filtered['types'] = $creatable;
            $building[] = $filtered;
        }
        return $building;
    }

I am hoping that there is a better way.

Thanks for looking.

Mick

phpMick left a reply on Breaking Out Of A Foreach • 1 week ago

Is there also a way which I could filter this structure, based on the description.

Mick

9th January, 2018

phpMick left a reply on Check Date Is Valid. • 1 week ago

This is how I eventually did it:

/**
 * Use the Laravel validation to validate a value.
 *
 * @param $value the value to test
 * @param $rules Laravel standard rules: 'required|numeric'
 * @return bool
 */
function validateValue($value,$rules)
{

    //needs to be an assoc
    $assoc = ['value'=>$value];

    $validator = Validator::make($assoc, [
        'value' => $rules,
    ]);

    return !$validator->fails();


}

Figured I should just use the Laravel code. Genius or lunacy?

Mick

phpMick left a reply on Execute Code After Successful Validation In A Form Request, • 1 week ago

Thanks Martin,

I have come to the conclusion that I need to just write my own validation for this. I am currently adding it all to the controller, just as you have suggested.

phpMick started a new conversation Create A Collection From Three Arrays. • 1 week ago

Hi.

Is there a simple way to do this (without just iteration)?

I have these three arrays:

'start' => 
  array (
    0 => '2018-01-24',
    1 => '2018-01-31',
  ),
  'end' => 
  array (
    0 => '2018-01-23',
    1 => '2018-01-31',
  ),
  'thing' => 
  array (
    0 => '23',
    1 => '44',
  )

What I actually want is:

  array (
    0 => array(
    'start' => '2018-01-24',
    'end' => '2018-01-23',
    'thing' => '23',
    ),
    1 => array(
    'start' => '2018-01-24',
    'end' => '2018-01-23',
    'thing' => '44',
    ),

  )

Thanks,

Mick

phpMick left a reply on Execute Code After Successful Validation In A Form Request, • 1 week ago

I want to add some extra validation.

I am validating arrays, using the standard validation:

'start.*'=>'nullable|date|before:end_date',
'end.*'=>'nullable|date|after:start_date',

Now I want to validate the 'rows' in the array against each other. I won't be modifying the data but I am hoping to add to the failure messages and set failure.

Mick

8th January, 2018

phpMick started a new conversation Add Id To Validation Message. • 1 week ago

Hi,

I have written a custom validation rule which validates multiple rows in a table (they all need to be submitted at once).

I now want to be able to identify which row caused the error, can I just add the id to the message somehow?

{"message":"The given data was invalid.","errors":{"data":["Start and end dates are not consecutive."]}}

So, I want the message to look like

{"message":"The given data was invalid.","errors":{"id":"5" ,"data":["Start and end dates are not consecutive."]}}

It looks like I could do this using $validator->after?

Thanks,

Mick

5th January, 2018

phpMick started a new conversation Check Date Is Valid. • 2 weeks ago

How can I check that my string is a valid date ("2018-01-01").

Is there a simple way, or do I need to do something like this:

if (Carbon::createFromFormat('YOUR DATE FORMAT', $stringVariable) !== false) {
    // valid date
}

phpMick left a reply on Sorting Array Structure. • 2 weeks ago

OK, I sorted this.

I took out the top level array and it now looks like it is working using a collection.

phpMick started a new conversation Sorting Array Structure. • 2 weeks ago

Hi,

Can someone please explain the easiest way to sort this structure by start_date?

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [start_date] => 5
                    [end_date] => 3
                    [thing] => 33
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [start_date] => 4
                    [end_date] => 3324
                    [thing] => 432
                )

        )

)

I am experimenting with sticking it in a collection and trying to sort that but have not made any progress yet.

phpMick left a reply on Fake JSON Data For Testing. • 2 weeks ago

Yes, but I want the dates to be random. I also want a few different rows.

phpMick started a new conversation Fake JSON Data For Testing. • 2 weeks ago

Hi,

At the moment, I have this in one of my tests:

$data = "[[{'start_date':'2018-01-01','end_date':'2018-02-01','thing':'12'}],
                    [{'start_date':'2018-03-01','end_date':'2018-04-01','thing':'8'}]]";

Basically I just need a couple of dates and a small random number, then converted to JSON for a post.

Should I just be using faker to do this, inside the test. or is there a better way?

Mick

phpMick left a reply on JQuery Save HTML Table To Database. • 2 weeks ago

Thanks,

We have decided not to start using Vue in this project yet.

We will probably move to it in the future, but for now, we just want to get things working as simply and quickly as possible.

4th January, 2018

phpMick left a reply on JQuery Save HTML Table To Database. • 2 weeks ago

How about this then?

$("#save").click(function(){

        var rows = $("#things-table").find("tr").length;
        var savingRows = [];

        for (var rowOn = 1; rowOn < rows; rowOn++) {

            var startDate = $("#things-table").find("tr").eq(rowOn).find("td").eq(0).text();
            var endDate = $("#things-table").find("tr").eq(rowOn).find("td").eq(1).text();
            var thing= $("#things-table").find("tr").eq(rowOn).find("td").eq(2).text();

            var rowData = [
                {start_date:startDate, end_date:endDate, thing: things}
            ]

            savingRows.push(rowData);

        }

    });

Then I will just POST savingRows.

phpMick left a reply on JQuery Save HTML Table To Database. • 2 weeks ago

It is a requirement that I only POST the complete table.

Here is the table:

 <table class="table table-bordered table-striped text-middle" id="things-table">
                        <tr>

                            <th>Start Date</th>
                            <th>End Date</th>
                            <th>Thing</th>
                        </tr>
                        @foreach($margins as $margin)
                            <tr>
                                <td>{{ $agreement->start_date }}</td>
                                <td>{{ $agreement->end_date() }}</td>
                                <td>{{ $agreement->thing}}</td>
                        @endforeach
                    </table>

This is how I add a row:

 $("#add-row").click(function(){

    var startDate = $("#input-start").val();
    var endDate = $("#input-end").val();
    var margin = $("#input-thing").val();


    var markup = "<tr>" +
                    "<td>"+startDate+"</td>" +
                    "<td>"+endDate+"</td>" +
                    "<td>"+thing+ "</td>" +
                "</tr>";

    $("table tbody").append(markup);
    });

Just want to save the table the simplest way possible. At the moment I am exploring using JQuery to iterate through the rows, then the elements. Yuck.

Mick

phpMick left a reply on JQuery Save HTML Table To Database. • 2 weeks ago

Nope, no form just an HTML table.

phpMick started a new conversation JQuery Save HTML Table To Database. • 2 weeks ago

Hi,

I have an HTML table which has been populated from an Eloquent collection which was passed to the view.

Users can also add rows (JQuery).

What is the simplest way to then save this new table?

I'm thinking that I need to have the table cells in an array somehow?

Thanks,

Mick

phpMick left a reply on Validating Start And End Dates. • 2 weeks ago

Of course!

Thanks @bobbybouwmann I completely forgot about building the rules up myself:

3rd January, 2018

phpMick started a new conversation Validating Start And End Dates. • 2 weeks ago

I am trying to implement these rules:

1, No start date or end date 2, No start date but has an end date 3, A start date but does not have an end date 4, A start date and an end date – ensure the end date is greater than the start date

So far I have:

'start_date'=>'nullable|date|before:end_date', 'end_date'=>'nullable|date|after:start_date'

So this works for. 1, No start date or end date 4, A start date and an end date – ensure the end date is greater than the start date

But is not possible to just supply one of the dates.

Is there any way to do this?

Thanks,

Mick

phpMick left a reply on Better Way To Get Element From Request() • 2 weeks ago

Thanks @martinbean.

I was just poking around in xdebug, trying to find a way to get the_number out.

Dot notation is working and looking much neater.

I'm in a model.

Mick

phpMick started a new conversation Better Way To Get Element From Request() • 2 weeks ago

Hi,

I just wrote this and it feel pretty horrific:

 $request = request();

  $the_number = $request->request->all()['details']['the_number'];

Is there a better way?

Mick

2nd January, 2018

phpMick started a new conversation Datepicker • 2 weeks ago

Hi Guys,

Happy New Year!

What are you using for datepickers?

I was trying to use http://bootstrap-datepicker.readthedocs.io/en/latest/

but I can't get the autoclose option to work (Chrome).

Thanks,

Mick

22nd December, 2017

phpMick started a new conversation Delete Without Using Forms Collective • 1 month ago

How can I do this, without using the Forms Collective?

  {!! Form::open([
                                    'method' => 'DELETE',
                                    'route' => ['articles.destroy', $article->id]
                                        ]) !!}
                                {!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
                                {!! Form::close() !!}

Thanks,

Mick

21st December, 2017

phpMick started a new conversation Message Box, Are We Still Using SweetAlert? • 1 month ago

Hey,

Are we still using Sweet Alert?

Just about to add it to a new project and wondered if it was still the best thing to use?

Mick

phpMick left a reply on Best Way To Reuse Form Partials For Create And Edit. • 1 month ago

As usual, I solved it, 30 seconds after posting:

{{ old( 'details.enrolment_number',$agreement->details['enrolment_number'])}}"

phpMick started a new conversation Best Way To Reuse Form Partials For Create And Edit. • 1 month ago

Hi,

There are a few posts regarding this. This is what I am currently doing.

In the create I create a new model and pass it to the view. In the edit, the model being edited is passed to the view.

In my template I am doing this:

value="{{ old('first_name', $contact->first_name) }}

So that if there is a validation error, first_name will be populated and used, alternatively the first_name from the model I passed will be used.

Is this a sensible approach? Seems OK to me?

The problem I have now, is that my value is in an array, so I am trying to do this:

value="{{ old( 'details['enrolment_number']',$agreement->details['enrolment_number'])}}"

But I don't know the syntax because of the two sets of single quotes in the double quotes. I suspect that there is a simple answer to this?

Thanks,

Mick

18th December, 2017

phpMick left a reply on Breaking Out Of A Foreach • 1 month ago

@36864 that works!

Magic, seriously, that is magical.

Thanks,

Mick

phpMick left a reply on Breaking Out Of A Foreach • 1 month ago

@Stratos yes I know, I am trying to avoid using either.

phpMick left a reply on Breaking Out Of A Foreach • 1 month ago

That would be awesome @36864 but I get:

Trying to get property of non-object

It's tricky because the id buried quite deep in the structure.

phpMick left a reply on Breaking Out Of A Foreach • 1 month ago

How can I find the className for id=104?

Here are the contents of the array:

array:2 [ 0 => array:2 [ "provider" => "John" "types" => array:4 [ 0 => array:3 [ "id" => 101 "description" => "Thing" "className" => "One" ] 1 => array:3 [ "id" => 104 "description" => "Another Thing" "className" => "Two" ] 2 => array:3 [ "id" => 105 "description" => "An Again" "className" => "Three" ] 3 => array:3 [ "id" => 106 "description" => "Why Not" "className" => "Four" ] ] ] 1 => array:2 [ "provider" => "Chris" "types" => array:2 [ 0 => array:2 [ "id" => "201" "description" => "This" ] 1 => array:2 [ "id" => "202" "description" => "That" ] ] ] ]

phpMick started a new conversation Breaking Out Of A Foreach • 1 month ago

Hey,

I am just doing this:

($providersAndTypes is an array or arrays)

$providers = collect($providersAndTypes);


 foreach($providers as $provider){
            $collected = collect($provider['types']);

            $found = $collected->where('id', $agreement_id)->first();

            //check this where not found
            if(isset($found)){
                $className = $found['className'];
                break;
            }

        }

I hate breaking out of a foreach before reaching the end. Is there a neater way? I started using a ```where`` but it started to get silly.

I had a lecturer at Uni who would say that we should only use a for if we wanted to iterate through all the items, otherwise we should be using a do or while.

Mick

phpMick left a reply on Search Through An Array Of Assocs For A Value. • 1 month ago

Thanks @burlresearch that is exactly what I was looking for.

Collections rock.

phpMick started a new conversation GET Parameters Testing • 1 month ago

Hi,

How should I be doing this?

$test = $this->get("/agreements/create",['provider' => $provider, 'type'=>$type]);

Why can't I see this in the documentation?

Thanks,

Mick

phpMick left a reply on Chrome Blocked Localhost With Error Your Connection Is Not Private • 1 month ago

Change you application to be something like app.test instead of whatever you are using.

15th December, 2017

phpMick started a new conversation Search Through An Array Of Assocs For A Value. • 1 month ago

Hi,

There must be a better way of doing this:

  $searchArray = [
            ['title' => 'ONE','description' => 'gfdsgsfd'],
            ['title' => 'TWO','description' => 'gfdsgsfd'],
        ];

      
  
        foreach($searchArray as $element){
            
            if($element['title'] === $value){
                $passed = true;
            }
            
        }



Thanks,

Mick

14th December, 2017

phpMick left a reply on Hide And Show Partials Based On Chosen Option In A Select. • 1 month ago

Thanks all, nice to see example of both.

Cheers.

phpMick started a new conversation Hide And Show Partials Based On Chosen Option In A Select. • 1 month ago

Hi,

Has anyone got an example of how do do this?

I could have maybe 25 partials which represent different forms. The user will select an option and then be presented with the correct form.

I was thinking the easiest way is just to use JQuery.

Don't really want to use Vue in this project yet. (But could be persuaded if it is simpler. )

Thanks,

Mick

11th December, 2017

phpMick left a reply on How To Test Soft Deletes? • 1 month ago

30 seconds after posting as usual.

Thanks anyway.

phpMick left a reply on How To Test Soft Deletes? • 1 month ago

I know. I'm a muppet:

$this->assertSoftDeleted($table, array $data);

29th November, 2017

phpMick left a reply on Xdebug And Homestead • 1 month ago

Configured PHPStorm and debugging is working OK.

Not sure why it doesn't look like it's loaded.

Mick

phpMick started a new conversation Xdebug And Homestead • 1 month ago

Hi,

I am just setting up a new development environment. Win7 and Homestead.

I have Homestead installed and working but am now trying to get Xdebug working.

It looks like it is installed but not loaded?

php-xdebug is already the newest version (2.5.5-3+ubuntu16.04.1+deb.sury.org+1).

But when I look in my php.ini, there is no mention of xdebug?

What am I doing wrong?

Mick

23rd November, 2017

phpMick left a reply on AssertSee Missing. • 1 month ago

Thanks,

Yes, it should be this:

  $this->get($thread->path())
            ->assertSee($thread->title)
            ->assertSee($thread->body);

phpMick started a new conversation AssertSee Missing. • 1 month ago

Hi,

I am working through Let's Build a Forum and am getting this error:

Call to undefined method Tests\Feature\CreateThreadsTest::assertSee()  

On this bit:

    public function an_authenticated_user_can_create_new_forum_threads()
    {

        
        $this->actingAs(factory('App\User')->create());

        $thread = factory('App\Thread')->make();

        $this->post('/threads',$thread->toArray());

        $this->get($thread->path());

        $this->assertSee($thread->title);

    }

Version 5.4.36

Mick

Edit Your Profile
Update

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