cbojer

Member Since 5 Years Ago

Aalborg

Developer at Eventspace

Experience Points 5,000
Experience Level 2

5,000 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 192
Lessons
Completed
Best Reply Awards 2
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.

24 Jul
4 years ago

cbojer left a reply on If(Auth::attempt($data)) Fails Every Time My Username And Password Is Right Then Also

I believe the Auth::attempt method checks whether the password provided when hashed, is matching the password in the database. So while the password from the data array and the database are equal, you are comparing the hashed version of the password with the non-hashed version in the database.

First, let me ask: Why are you encrypting the password before comparing it?

Usually, one would handle this by passing the clear password to Auth::attempt and then storing a hashed version of the password in the database, often set by a mutator on the User model.

23 Jul
4 years ago

cbojer left a reply on Testing Login With User Factory

So, it works if you do it in the browser, but not in your test?

If that is the case, are you sure that you are not accidentally double hashing your password or something like that? If you have a setPasswordAttribute on your User model that automatically hashes the password, then that could be the problem.

Otherwise, I'll need to know more in order to help you.

cbojer left a reply on Eloquent Polymorphic Relationship

Have you made sure to run composer dump-autoload after creating the Player Model?

09 Jun
4 years ago

cbojer left a reply on How To Set Up Administrator And User Authentication

I'm currently using the following in an application, and it's working just fine:

Have a role/type column on your user model. E.g. for Administrators it might have the value "admin" and for regular users it might be "user".

Then when you are authenticating users, you can authenticate with extra conditions, e.g. check whether the user is an administrator for admin login:

if (Auth::attempt(['email' => $email, 'password' => $password, 'role' => 'admin']))
{
    // The user is an admin
}

Also, I would recommend making a few methods on the User model, that checks whether the user is an admin or a regular user:

public function isAdmin()
{
    return $this->role === 'admin';
}

With this, you can easily check in your controllers or views, whether the user is an admin or a regular user, and then act accordingly, e.g. by serving a different view for one of them.

Hope this helps you on your way.

cbojer left a reply on How To Set Up Administrator And User Authentication

In order to help you we will need more information.

How have you set up the models? Do you have a model for Administrators and a model for Users, or do you have a single User model with roles or a type of Administrator and User?

How is the code that does the authentication set up now?

This should allow us to get started helping you.

cbojer left a reply on What Is Best Practice For Identifying The User With Queued Jobs

I will have to agree with @pmall here. The queue shouldn't be using sessions, rather it should just be handed the user id so it can update what needs to be updated. If it requires a restructuring of a bit of code, I would say it's for the better in the long run, since this is probably an indication of your code being tighly coupled with Laravel's Guard.

31 Mar
4 years ago

cbojer left a reply on Setting Attributes Not Included In Mass Assignment

I like to create a named constructor on the object:

/** Message model */
public static function fromUser(array $attributes, $userId) {
    $message = new Message();
    $message->fill($attributes);
    $message->user_id = $userId;
    return $message;
}

And then from the controller:

$message = Message::fromUser($request->all(), $userId);
$message->save();

In reality, I would be more specific when creating the entity than just passing an array of attributes, like this:

public static function fromUser($message, $title, $userId) {
    $message = new Message();
    $message->message = $message;
    $message->title = $title;
    $message->user_id = $userId;
    return $message;
}

This makes it more explicit, and ensures that you provide the right attributes for the message. It does, however make your controller code a little less clean:

$message = Message::fromUser($request->get('message'), $request->get('title'), $userId);
$message->save();
09 Mar
4 years ago

cbojer left a reply on Multi-step Setup/form And Middleware

@toniperic this is somewhat equal to what I've thought about, only saving it on a separate table. I don't like the idea of adding the field to the model, as I don't really feel that it belongs there, but it would certainly still be more clean than checking for all the attributes/relationships on the model in the middleware.

cbojer left a reply on Multi-step Setup/form And Middleware

My bad for not being more specific! I'm talking about when a user actually leaves the website without completing the setup, only to return to it at a random time later. In this case saving it in the session wouldn't work.

cbojer started a new conversation Multi-step Setup/form And Middleware

Hey,

I'm building an app, where we have a setup wizard, that helps us get our users filling out the different information that is needed before they can start using the site. In this setup, there is 5 different steps that they need to complete. They cannot use the app, before they have completed these 5 steps, so I've of course set middleware up that checks whether the setup has been completed.

The problem, however, is if users were to not complete the setup(as will probably happen from time to time). In this case, I would need to redirect them back to the first uncompleted step in the setup. My first idea of getting this done, was to create a middleware implementation for each step in the form, that will check whether the step was completed by querying the model and/or its relations(this feels bad!), and if it was, then check for all the subsequent steps, until you find a step, that hasn't been completed and then redirect them there, but I can't help but feel like I'm overcomplicating it here or not doing it gracefully.

How would you attack the problem? Another solution I've fiddled with was creating a setup table, that links to the model and keeps track of the last completed step, but I can't really decide whether this is any better.

Thanks in advance, Casper

05 Mar
4 years ago

cbojer started a new conversation Version Conflict - Composer

I'm building a site, and making use of Adam Wathans eloquent-oauth package, which requires guzzlehttp version 5.0.

The problem, however, is that the Laravel Mailer bugs with this version of guzzle, and wants me to use version 4.0(as suggested by another one who had the same issue on this forum before).

What do I do here? I'd very much like to be able to use the oauth library, but I do of course also need to be able to send mails. Is there anyway around this issue?

27 Feb
4 years ago

cbojer started a new conversation Not Showing The Two Latest Videos

Hello,

I noticed today, that while the two new videos "Hooking into Mandrill" and "Fast Workflow with Generators" have been posted to the site, they're not visible when i go to the latest video's tab, where the newest video being shown is "Delivery".

Anyone else have the same problem?

Casper

20 Jan
4 years ago

cbojer left a reply on Day Month & Year

Using a macro would in my opinion be the way to go. No need to use unnecessary jquery to do this, as the macro can be created in a very short time.

cbojer left a reply on Eager Loading With Custom Where Queries On Both Relations

As far as i can see, you are saving the status in the CHAR type, which means that it will be a string, but you are comparing it to an integer(0). If you are sure you want to save the status as CHAR, then try putting the 0 within single quotation marks:

$query->where('status', '=', '0');

cbojer left a reply on How To Deal With Relationships On Entities Without Extending Eloquent?

Perhaps give them a boolean attribute like the exists attribute on the Eloquent model, and set that to true once it has been saved to the database and once you fetch them from the database.

08 Jan
4 years ago

cbojer left a reply on Email: 502 Error After Embedding Image With Mandrill Driver

Seems like indeed you are right! Thanks a lot!

cbojer left a reply on Email: 502 Error After Embedding Image With Mandrill Driver

Asset returns the full path as a string for me when i dump it, and the link works in the browser. Will try changing it though, and report back.

cbojer left a reply on Email: 502 Error After Embedding Image With Mandrill Driver

I get a 502 Bad Gateway Nginx error, and the log gives me the following:

[2015-01-08 14:02:23] staging.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Method Swift_Message::__toString() must not throw an exception' in /home/forge/staging.arrange-it.dk/vendor/laravel/framework/src/Illuminate/Mail/Transport/MandrillTransport.php:0

Stack trace:
#0 [internal function]: Illuminate\Exception\Handler->handleShutdown()
#1 {main} [] []
07 Jan
4 years ago

cbojer started a new conversation Email: 502 Error After Embedding Image With Mandrill Driver

Hello fellow Laravel users,

After using Laravel's method to embed images in email, I'm getting a 502 when i try to send the email. Before i embedded images in the email, it worked fine. I'm using the Mandrill driver, and the code in the view is as follows:

{{ $message->embed(asset('path/to/image')) }}

Does anyone know what could be causing the 502 and how I could possibly fix it?

Thanks in advance,

Casper

23 Dec
4 years ago

cbojer left a reply on Repository Pattern With Eager Loaded & Nested Resources

I like this trick that i learned from Culttt:

Allow all of your repository methods (or those that make sense anyway) to accept an optional array of relationships you want to eager load as an argument. For example on your SurveyRepository's findById method:

public function findById($id, array $relationships = []) {
    return Survey::with($relationships)->find($id);
} 

It very easy to implement and allows you to keep your repositories lean, instead of having multiple methods doing basically the same thing just eagerloading one or more different relationship(s).

25 Sep
4 years ago

cbojer left a reply on Pagination Bug With Search

Tried that, but it's still not working. Seems odd. Guess it is fixed then, though :)

cbojer left a reply on Pagination Bug With Search

Hmm, its still not fixed on my end. Anyone else who would care to check up on it? Could just be some caching or something like that.

24 Sep
4 years ago

cbojer started a new conversation Pagination Bug With Search

When you search, and there are enough results for it to be paginated, the pagination links don't include the query string.

For instance, try to search for "git" and click on page 2. The link is now search?page=2, which redirects to /lessons. It should instead be search?q=git&page=2

15 Sep
4 years ago

cbojer started a new conversation Validation With Special Characters

I'm creating a website in my native language(Danish) and we have some special characters that we use in our language, which are part of the ISO-8859-1 charset.

The problem I have is, that when validating e.g. names or places, I would like to make sure that they are alphanumeric. But if the name or place contains one of the special characters, validation will fail.

So basically my question is, how do you run an alpha numeric check via RegEx(I'm a newb at RegEx), that will allow for the special characters, being "æ", "ø", "å" etc.

02 Sep
4 years ago

cbojer left a reply on Forge And Emails

Okay. Thanks for your input guys! I will probably end up using either Google Apps or Zoho depending on what my partners are willing to spend on it.

cbojer left a reply on Forge And Emails

No problems, I'd actually prefer it, but it's a matter of cost. So no, i didn't get an account when it was free :) But it seems like Zoho mail offers what i want, I'll try that.

cbojer left a reply on Forge And Emails

Alright. I'm already using Mandrill for sending email.

Do you know any alternatives to Google Apps, that are free tier? I don't need a whole lot of email adresses or anything complicated from the get-go, so free would be ideal.

cbojer started a new conversation Forge And Emails

So I have bought a server from DigitalOcean and I'm using Forge to help me manage it. I'm sort of new to managing a server, having only done it from a traditional CPanel before.

The first problem I've run into is how to setup Emails for the server. It seems like Forge doesn't have an easy way of doing it, so how do you recommend I set up the server, so that it can send and receive email?

Which tools you use for this and any tutorials or tips and tricks are welcome!

cbojer started a new conversation Forge And Emails

So I have bought a server from DigitalOcean and I'm using Forge to help me manage it. I'm sort of new to managing a server, having only done it from a traditional CPanel before.

The first problem I've run into is how to setup Emails for the server. It seems like Forge doesn't have an easy way of doing it, so how do you recommend I set up the server, so that it can send and receive email?

Which tools you use for this and any tutorials or tips and tricks are welcome!

cbojer started a new conversation Forge And Emails

So I have bought a server from DigitalOcean and I'm using Forge to help me manage it. I'm sort of new to managing a server, having only done it from a traditional CPanel before.

The first problem I've run into is how to setup Emails for the server. It seems like Forge doesn't have an easy way of doing it, so how do you recommend I set up the server, so that it can send and receive email?

Which tools you use for this and any tutorials or tips and tricks are welcome!

29 Aug
4 years ago

cbojer left a reply on Advanced Query With Eloquent And Query Builder

So what i ended up doing, was eager loading the polymorphic relationship manually. Instead of using the with method, i would get all the Packages, and their id and type. I would then filter through the info and create a new query for each model type and use the whereIn method to only load the models that met the Package queries expectations. The queries performed are basically the same that Laravel uses, the only difference, is that I'm now able to constrain the eager loaded relationship.

In case anyone is interested or happens to come across the same problem, here is an example:

 $packages = Package::all();

// First we group by type. This is prep for using the lists method. You'd think we could use that right at the beginning, but we can't use the type as the key, since that would result in each subsequent item of the same relation overriding the last one. We can't use the id either, since packages of different types could easily have the same id. So we group them by type first.
    $packages = $packages->groupBy('package_type');
// Then we loop through all the package types, with the type being the key.
    $packageTypes = [];
    foreach($packages->all() as $type => $packages) {

        $collection = new Collection($packages);
//  Now we get the list of id's
        $packageIds = $collection->lists('package_id');
// And store them with the type holding an array of it's corresponding ids
        packageTypes[$name] = $packageIds;
    }

    $packageResults = new Collection();
 // We loop through the types and create a new model for each one.
    foreach($this->packageTypes as $name => $ids) {
        $model = new $name;

// We get model instances that match our id, and here you can scope/constrain the query all you want for your eager loaded models. To end, i push the results onto a new collection.
        $packageResults->push($model->whereIn('id', $ids)->available($bookingDate)->get());
    }
28 Aug
4 years ago

cbojer started a new conversation Advanced Query With Eloquent And Query Builder

I'm trying to put together a query using Eloquent, but it's proving to be quite difficult due to the fact that polymorphic relations are involved, many of the smart eloquent methods for easy queries aren't supporting that.

As a quick overview, I have a Package model, with a polymorphic one-to-many packageable relation. These packageables all have bookings, and the bookings relationship is also a polymorphic relationship.

I'm trying to run the following query:

Package::with(['packageable' => function($q) use($bookingDateStart,     $bookingDateEnd) {
            $q->whereHas('bookings', function($q) use($bookingDateStart, $bookingDateEnd) {
                    $q->whereBetween('start_time', [$bookingDateStart, $bookingDateEnd]);
            }, '<', 1);
        }])->get();

So, as you can see, I'm getting all packages, with the eager loaded relations. The problem, comes when i need to try and constrain the eager loaded packageable relationship. This relationship is a polymorphic one, and Laravel therefore can't find the bookings relationship on the packageable instance.

What i want, is, as the code states, to only eager load the instances that doesn't have a booking within the given starting time.

So, i tried this out instead:

Package::with(['packageable' => function($q) use($bookingDateStart,     $bookingDateEnd) {
            $q->whereNotExists(function($subq) use($bookingDateStart,     $bookingDateEnd) {
              $subq->select(\DB::raw(1))->from('bookables')
              ->join('bookings', 'bookables.booking_id', '=', 'bookings.id')
              ->whereRaw('bookings.bookable_id = packages.package_id')-    >where('bookable_type', 'packages.package_type')
->whereBetween('start_time', [$bookingDateStart, $bookingDateEnd]);
});
}])

But for some reason Laravel doesn't constrain the query at all it seems, when it's a polymorphic relation.

So I'm kinda stuck. Does anyone have any idea about how I could get the wanted data with Eloquent, or will i have to resolve to the query builder?

How would you go about tackling this?

27 Aug
4 years ago

cbojer left a reply on Getting All From A Polymorphic One-to-many Relation

For some reason I had completely forgot about using with. Duh. Thanks guys!

cbojer started a new conversation Getting All From A Polymorphic One-to-many Relation

So, I have a a situation where i need to get all the instances from a polymorphic relation.

I have a Package Table that can be many different types of packages. Let's call the relationship packageable.

It could, as an example, be an instance of Soccer or an instance of Basketball.

So i need to be able to get all the different packages for display in a view.

I know i can get the package model from for instance the Soccer model by accessing the polymorphic relation, like this:

$soccer = Soccer::find(1);

$soccer->package;

And that I can access for instance the Soccer model from a specific Package via the packageable relationship, like this:

$package = Package::find(1);

$package->packageable;

But how do I get all Packages, that means both Soccer, Basketball, and whatever package types i might have?

Any help will be appreciated. Thanks in advance.