oliverbusk

oliverbusk

Member Since 1 Year Ago

Experience Points
10,540
Total
Experience

4,460 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
69
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.

Level 3
10,540 XP
Nov
26
2 months ago
Activity icon

Started a new Conversation Laravel Store API Usage For Each User

Hi everyone!

I have a basic SaaS app where my users can access my API. I have three plans:

Free: 500 requests/month Pro: 2500 requests/month Business: 15000 requests/month

I am trying to figure out how to best validate for the above.

Right now I just have a simple ApiController that is calling below on every request hitting an API endpoint:

public function updateRequests()
{
    $userId = auth('api')->user()->id;

    DB::table('users')
        ->where('id', $userId)
        ->update([
            'requests' => DB::raw('requests + 1'),
        ]);
}

As you can see the above simply increment a requests column for the specific user on each request.

With this I am not able to check if the user is within his/her usage limit of a given month.

I was thinking of adding a api_requests table, that simply holds:

id | user_id | created_at | updated_at

Because then I can somehow group by created_at for a given month and check whether or not that the specific user is allowed to make additional requests for that month.

However, a drawback of this approach is that this table will quickly get bloated.

Any ideas on how I can solve this issue?

Oct
17
4 months ago
Activity icon

Replied to Passing Model Through A Prop, Or Getting It By An API?

This is what I settled for:

I pass the documents id through to the modal:

@click="$modal.show('showDocument', {document: '{{($document->id)}}'})"

In my controllers show method, I have this:

public function show(Document $document)
{
  
    return response()->json([
            'document'    => $document,
    ], 200);
}

In my Vue view, the axios get request is simply hitting the show method:

getDocument: function (document) {
      axios.get('documents'/ + document).then((response) => {
                this.document = response.data.document;
      }).catch(error => {
                console.log(error);
      })
},

To ensure that this specific show method can only be accessed from an AJAX request, I have created a custom middleware:

RequestIsAjax.php:

    public function handle($request, Closure $next)
    {
        if (! $request->ajax())
            return abort(403);

        return $next($request);
    }

And applied it to my controller, like so:

$this->middleware('ajax', ['only' => ['show']]);

Above seems to be working. However, I am still unsure if this is the most optimal way to go about this? Any input would be appreciated!

Oct
16
4 months ago
Activity icon

Replied to Getting Resources Through Route Model Binding

I mean probably? I was just thinking, that since a document belongs to a stream, the URL should reflect this?

Activity icon

Replied to Getting Resources Through Route Model Binding

OK, that makes sense. I thought that this was handling in the route model binding.

Would I need to define, for example a Policy, for both my Stream and Document model?

Activity icon

Started a new Conversation Getting Resources Through Route Model Binding

I have below two models:

Stream and Document

A Stream can have many documents, and a document belongs to a Stream.

I have these two resources defined in my routes file:

Route::resource('streams', 'Stream\StreamsController');
Route::resource('streams/{stream}/documents', 'Stream\StreamDocumentsController');

Let's say I have below data in my database:

streams

id |  name 
1  | Awesome Stream
2  | Also awesome Stream

And two documents - one in each stream.

documents

id | stream_id | name 
1  | 1         | Document1.pdf
2  | 2         | Document2.pdf

Now, if I visit the first stream and the document attached to it:

/streams/1/1

All is good. I can see the document information. However, if I visit the second stream - but with the id of the first document, like:

/streams/2/1

I can still see the document. How can this be? Since the document.id.1 belongs to the first stream and not the second?

Activity icon

Started a new Conversation Passing Model Through A Prop, Or Getting It By An API?

I have a web application, where users can upload different documents. I have a model, simply called Document.

I then have an index view, so users can view all their uploaded documents:

index.blade.php

@foreach($documents as $document)
   <ul>
      <li><a href="#" @click="$modal.show('showDocument', {document: json_encode($document)})">{{$document->name}}</a></li>
   </ul>
@endforeach

Now, I want to do so when users click on a specific document, a modal will open up on the current page. This is done with the following Vue code:

@click="$modal.show('showDocument', {document: json_encode($document)})"

This works fine. The modal opens up and the document object is attached to it.

My question is - is this the best approach? Consider I have 50 uploaded documents and on top of that, I would need to access relationship data on the Document model.

Another approach I have been thinking about was simply just to pass the id of the document to the modal, and then simply GET the document information by an API when the modal is loading:

@click="$modal.show('showDocument', {documentId: '$document->id'})"

And then, on the Vue view:

getDocument: function () {
      axios.get(this.route).then((response) => {
                this.document = response.data.document;
      }).catch(error => {
                console.log(error);
                this.document = {};
      })
},

Not sure if it really makes any difference (performance or design-pattern wise?)

Any input is appreaciated!

Oct
14
4 months ago
Activity icon

Started a new Conversation Carbon - Get Past And Next 4 Weeks

I am trying to show the previous 4 weeks and next 4 weeks (in numbers), like below:

Current week: 42

Week: 38
Week: 39
Week: 40
Week: 41
Week: 42
Week: 43
Week: 44
Week: 45
Week: 46

This is the code I have so far:

$this->week = '42'

$date = Carbon::now();
$date->setISODate(date('Y', time()), $this->week);

$start = $date->copy()->subWeeks(4)->week();
$end = $date->copy()->addWeeks(4)->week();

$weeks = CarbonPeriod::create($start, '1 week', $end);

And then in my blade view, I have this:

@foreach($weeks as $week)
              <a href="?week={{$week}}">
                        <div>{{\Carbon\Carbon::create($week)->week()}}</div>
              </a>
@endforeach

However, above prints out the following 38 weeks.

Any idea on how I can print out the past 4 and next 4 weeks?

Oct
02
4 months ago
Activity icon

Started a new Conversation Design Pattern Question

Hi all!

I have a question, I hope you can assist me with, regarding a design pattern in my web application.

I have a web application where users can upload documents to what I call a stream. A stream can then have fields, which will ultimately be used to parse something in the uploaded document.

So currently, this looks like:

A stream can have many documents. A stream can have many fields. A document can have many field parsing results.

This all works fine with standard relationships.

Now, I am trying to build a new feature, where inside a stream, documents can be routed to multiple different "layout routes".

So consider a stream called "My Invoices". Inside of this, a user has created two layout routes:

  1. Invoices from Acme Inc.
  2. Invoices from Machinery Inc.

Now, here comes the tricky part I think: If the stream has routes enabled (like in above example), the fields attached it will still be needed. However, now users can also define fields for each layout route.

So: A stream can have many fields A route can have many fields

My current setup: Currently, I have a stream table, a fields table and a route table.

My question is regarding the fields. Should I simply just add a column on my current field table called route_id, that if null indicate that the field does not belong to any route (and therefore only to the Stream). And if it have an ID, it indicates that it belongs to a specific route.

OR should I create a new table called route_fields, that works the same way as the fields table, but instead of referring to the streams, it refers to the routes table?

I am trying to wrap my head around this, but I simply cannot figure out the best way...

Oct
01
4 months ago
Activity icon

Started a new Conversation Fire Off Event When Job-chain Has Finished

I have below Listener called ProcessDocumentFields. My listener class implements the ShouldQueue.

In this, I fire off two jobs by using the withChain method as below:

ParseFieldContent::withChain([
        new ParseFieldRules($event->document, $fields),
])->dispatch($event->document, $fields);

Now, I want to conditionally fire off an event, which ultimately trigger another job.

The thing is, both jobs (ParseFieldContent and ParseFieldRules) must have finished running.

#If below is true, fire off
if ($event->document->stream->settings()->get('field_routing')) {
   event(new DocumentDefaultRulesWasProcessed($event->documents));
}

The above event, will trigger the same two jobs, but I have to do some other checks first:

#EventServiceProvider.php
DocumentDefaultRulesWasProcessed::class => [
     ProcessDocumentRouteFields::class,
],

Inside my ProcessDocumentRouteFields, I have:

foreach ($event->routes as $route) {
        if ($this->assertIfRouteFieldsShouldBeHandled($route, $event->document)) {
            ParseFieldContent::withChain([
                new ParseFieldRules($event->document, $route->fields, false),
            ])->dispatch($event->document, $route->fields);
        }
}

As you can see in above, I run the two same jobs (ParseFieldContent and ParseFieldRules) - but only if a certain assertion is true.

My question is: how can I ensure, that the jobs placed in ProcessDocumentFields runs first, then fier off the event, that triggers the jobs in ProcessDocumentRouteFields?

Sep
30
4 months ago
Activity icon

Started a new Conversation Multiple Forms, Different Models

I am trying to figure out how to update a set of rules, that belongs to what I call a "route". Consider below two routes:

Route::resource('streams/{stream}/routes', 'Stream\StreamRoutesController');
Route::resource('streams/{stream}/routes/{route}/rules', 'Stream\StreamRoutesController')->only(['update']);

The first one is for my routes. Here I can create, read, update or delete any routes from my web application.

The thing is, a route can contain a set of rules (hasMany):

# Route.php
 public function rules()
{
    return $this->hasMany(RouteRule::class);
}

Now, I am trying to figure out how I can mass update all the available rules on a route, by going to my route settings:

streams/{stream}/routes/{route}/edit      | App\Http\Controllers\Stream\[email protected]

On above page, users can:

  1. Modify the given route
  2. Modify the given route rules (all of them, at one time)

My problem is, on this edit page, I will then have two edit forms. The first for the route and the second for the route rules:

#edit.blade.php

<!-- for the route -->
<form method="POST">
@csrf
@method('PATCH')

<input type="text" value="$route['name']}" name="name">
<input type="text" value="$route['url']}" name="url">

<button type="submit">Save Route</button>
</form>

<!-- for the rules -->
<form method="POST">
@csrf
@method('PATCH')

@foreach($rules as $rule)
<input type="text" value="{$rule['value']}" name="value[]">
@endforeach
<button type="submit">Save Rules</button>
</form>

My question is: how can I structure this? Should all of the logic go in the [email protected] method?