johncarter

johncarter

Member Since 2 Years Ago

Hertfordshire / London

Experience Points 7,100
Experience Level 2

2,900 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 68
Lessons
Completed
Best Reply Awards 0
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.

11 Sep
4 days ago

johncarter left a reply on Change Validation Message :attribute Placeholder For Array Inputs

Thanks for everyone's help. I managed to get it working by:

php artisan make:request StoreLines

Then adding the following method:

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreLines extends FormRequest
{

    public function rules()
    {
        return [
            // Your rules... e.g. 'line.*.description' => ['required', 'max:255'],
        ]
    }

    public function messages()
    {
        $messages = [];
        foreach ($this->request->get('line') as $line => $requestData) {
            foreach ($requestData as $input => $value) {
                $messages['line.' . $line . '.' . $input . '.required'] = 'The bloody ' . str_ireplace('_', ' ', $input) . ' on line ' . ($line + 1)  . '  is missing';
                $messages['line.' . $line . '.' . $input . '.min'] = 'The ' . str_ireplace('_', ' ', $input) . ' on line ' . ($line + 1)  . '  is must be a minimum of :min.';
            }
        }
        return $messages;
    }
}

One thing that is really irritating is that the error message array gets modified after it is returned and loses its order, so not all errors per line are grouped. But it's not that important.

johncarter left a reply on Change Validation Message :attribute Placeholder For Array Inputs

@sti3bas and @tykus both these solutions look like great options.

I am quite new to Laravel, so could you just tell me where the code is dropped in.

@tykus I presume the closure goes like this, if so I still get the old message.

$validatedData = request()->validate([
    //...
    'line.*.description' => 'required',
    'line.*.retail_price' => function ($attribute, $value, $fail) {
        [$group, $position, $name] = explode('.', $attribute);
        $fieldName = Str::replace('_', $name);
    
        // required rule
        if (!$this->validateRequired($attribute, $value)) {
            $fail('The ' . $fieldName . ' on line ' . $position + 1 . ' is required.');
        }
    
    
        // min rule
        $minValue = 0;
        if (!$this->validateMin($attribute, $value, [$minValue])) {
            $fail('The ' . $fieldName . 'on line ' . $position + 1 . ' must be at least ' . $minValue);
        }
    },
    // ...
]);

I read https://laravel.com/docs/6.0/validation#using-closures but not sure that docs method works when using $validatedData = request()->validate([//**]);

johncarter left a reply on Change Validation Message :attribute Placeholder For Array Inputs

Thanks @bobbybouwmann - the index is what I really need because the UI doesn't really lend itself to inline error messages.

Do you have any recommended reading on building it bespoke?

For now I will hack a JS string manipulation until I get the custom validation working.

10 Sep
5 days ago

johncarter started a new conversation Change Validation Message :attribute Placeholder For Array Inputs

I have a nested array of inputs being posted to a controller.

$validatedData = request()->validate([
    'line.*.image' => 'image|max:500',
    'line.*.description' => 'required',
    'line.*.retail_price' => 'min:0|required',
    'line.*.quantity' => 'min:1|required',
]);

When validation fails I receive an error message that looks like this:

The line.0.retail_price field is required.

How do I replace that to read something like:

The retail price on line 1 is required.

05 Sep
1 week ago

johncarter left a reply on VueJS In My Blade Templates ( But Not SPA )

Thanks for clarification.

I actually watched that series up to episode 10, I think the answer to this question is in episode 15. ( https://laracasts.com/series/learn-vue-2-step-by-step/episodes/15 )

johncarter left a reply on VueJS In My Blade Templates ( But Not SPA )

Thanks! I think I'm getting confused by Vue library docs where they reference implementation like this:

export default {
    name: 'app', // ??
    data: []
}

And I was expecting this:

new Vue ({
    el: '#app', // or #vue-section-1
    data: []
}

How and why are they different?

johncarter started a new conversation VueJS In My Blade Templates ( But Not SPA )

I want to make use of Vue.js in my Blade templates as a replacement for jQuery / plain JS.

How do I go about using Vue in sections of my Blade files rather than creating a full SPA?

Do I set a #app wrapper then bind it all to that or init the Vue component as separate instances of Vue?

<div id="app">
    <main>
        @yield('content')
    </main>
    <aside>
        @include('section.to.apply.vue.to')
    </aside>
</div>

<script>
var app = new Vue({
  el: '#app',
  data: {},
})
</script>

Or;

<div id="vue-section-1"></div>

<div id="vue-section-2"></div>

<script>
var vueSection1 = new Vue({
  el: '#vue-section-1',
  data: {},
})

var vueSection2 = new Vue({
  el: '#vue-section-2',
  data: {},
})

</script>

Is either fine? Or am I going about this the wrong way?

Any additional reading / videos on implementation would be just lovely. Thanks!

01 Sep
2 weeks ago

johncarter left a reply on Where To Put Access Logic Vs Business Logic

I think you are right: QuoteRequestBidController is definitely the way forward. Just need to refactor and I think it will all fall into place.

Really appreciate the help.

31 Aug
2 weeks ago

johncarter left a reply on Where To Put Access Logic Vs Business Logic

Thanks @stephens would you say that a Policy is the best practice here, despite it containing business logic?

30 Aug
2 weeks ago

johncarter left a reply on Where To Put Access Logic Vs Business Logic

Thanks for your response, again @mikenewbuild.

One of the problems with using a Policy is that I don't know how to show an error to the user depending on what condition it fails on.

Is there a way to do that? Or in that case should I use a Controller?

johncarter left a reply on Best Practice For $model->status Where Derived From Model Attributes

That's great, thanks @mikenewbuild. I will reorder as suggested.

Just a quick follow up:

If I am using those statuses expired, has_bids, etc in a Blade template and I want to use conditional CSS, icons and nice looking strings, what's the best method?

  • Blade component / include e.g. @include('_partials.statuses.quote-requests')
  • resources/lang file with translation {{ __( 'statuses.quote-requests.' . $quoteRequest->status ) }}
  • Or pass an array back from the model return ['css' => 'text-red-500', 'string' => 'Sorry this quote request has expired.']?

johncarter started a new conversation Best Practice For $model->status Where Derived From Model Attributes

I have two models Bid and QuoteRequest.

They have the relationships:

  • Bid->belongsTo('QuoteRequest')
  • QuoteRequest->hasMany('Bid')

The QuoteRequest->status is derived by knowing :

  • Is it expired?
  • Has it had any Bids attached to it?
  • Has a Bid been accepted?
  • Has a Bid been confirmed?

Could someone let me know if it's best practice to add this method to the QuoteRequest model:

public function status()
{
    if ( $this->isExpired() ) return 'expired';
    if ( $this->hasBids() ) return 'has_bids';
    if ( $this->hasAcceptedBid() ) return 'bid_accepted';
    if ( $this->hasConfirmedBid() ) return 'bid_comfirmed';
}

I did think to set a status in the database, but duplicates data because the status can be derived from the other columns / relationships.

Thanks for any help / criticism.

johncarter started a new conversation Where To Put Access Logic Vs Business Logic

I have the following business rules:

Only activated suppliers can create a Bid.

Suppliers can only create a Bid on an QuoteRequest when:

  • the QuoteRequest hasn't expired
  • they haven't already created a bid on the QuoteRequest
  • another bid hasn't been "accepted" for that QuoteRequest

Can someone tell where these rules belong. I did have them all in Policies/QuoteRequestPolicy.php but I feel like I am stretching the usage of a policy.

Should some of the logic be in the controller and/or the model?

Thanks!

01 Jul
2 months ago

johncarter left a reply on Accessing Site Without Domain

@geordienz how did you end up fixing this. I deleted the default site and created a new site with the root domain as a domain I hadn't yet setup DNS for.

Even though the new site was setup as the only site on forge when I visited example.com I got the 404 too.

I ended up recreating a site with the root domain as "default" then deployed my code there - seems to fix it. Do you have a better solution?