Skip to main content

Laravel session value not persisting when using AJAX requests

I had a very weird issue in Laravel where a specific session value was not persisting after an AJAX request.

To give it some context, I did two AJAX requests, one after the other.

The first AJAX request sets the session value.

The second AJAX request was then not seeing that value.

The issue is a known limitation with sessions in Laravel and the way to fix this is to enable SESSION_BLOCK.

Go into your session.php and if it doesn’t exist, add this:

'block' => env('SESSION_BLOCK', false),

Then, add SESSION_BLOCK=true to your ENV file.

Attach unique in BelongsToMany Laravel/Eloquent

As you probably know, you can use the attach() method to relate two models within a BelongsToMany relationship. The only problem with attach() is that it doesn’t check to see if the relationship already exists. So, essentially, you could attach the same model more than once to a relationship, which isn’t ideal.

For instance, you have pizzas and ingredients.

You could always do something like this:

$relationship = $pizza->ingredients()->where('ingredient_id', $ingredient->id)->get();
if(!$relationship)
{
$pizza->ingredients()->attach($ingredient);
}

Or, you could use this method, which checks if the relationship exists and it doesn’t double up:

$pizza->ingredients()->syncWithoutDetaching($ingredient);

What this will do is sync the ingredient with the pizza (like the sync() method), however, if the relationship already exists, it won’t do anything. The relationship will stay, and nothing else will detach.

Unique email validation in Laravel when re-saving model

When you validate an email in Laravel, you have the ability to specify whether you want a specific model to be excluded. The reason why you would exclude a model is for example, if you have a user account and the user needs to update their first name, but their email stays the same. Typically, Laravel would re-save the whole form and also validate the email, but because that user is already using the email, it will spit back a “unique” validation error mentioning the email already exists in the system (even though it is still being used by said user.

Read More

Remove container and UL on WordPress nav menu

Sometimes you might want to programatically include other things in your menu in WordPress and to do that you will want to control the UL tags so you can insert other LI items within it.
The code below will allow you to spit out ONLY the LI tags from a WordPress Nav Menu, you will have to wrap it in UL yourself so you can add extra LI.

Read More

Install PDFTk on Ubuntu 18.04 Bionic

pdftk is missing from the official repository right now. That’s an issue for many people.

I needed PDFTK to be installed on Ubuntu (18.04) due to the need to manipulate PDFs. Unfortunately, because I used Laravel Forge, they only allow Ubuntu, and apparently PDFtk is not longer working with Ubuntu 18.04. There is a workaround! Follow the below to get it working.

There’s a workaround here: How can I install pdftk in Ubuntu 18.04 Bionic?

Quick and easy workaround

 

 

UPDATED 8/2/2020

Try sudo snap install pdftk

Set up symlink so it works!

sudo ln -fs /snap/pdftk/current/usr/bin/pdftk /usr/bin/pdftk

The below seems not to work any more.

In SSH on your Ubuntu 18.04 server, do the below:

wget http://archive.ubuntu.com/ubuntu/pool/universe/p/pdftk/pdftk_2.02-4build1_amd64.deb
wget http://archive.ubuntu.com/ubuntu/pool/main/g/gcc-6/libgcj17_6.4.0-8ubuntu1_amd64.deb
wget http://archive.ubuntu.com/ubuntu/pool/main/g/gcc-defaults/libgcj-common_6.4-3ubuntu1_all.deb
sudo dpkg -i pdftk_2.02-4build1_amd64.deb libgcj17_6.4.0-8ubuntu1_amd64.deb libgcj-common_6.4-3ubuntu1_all.deb

Then type ‘pdftk’, it should come up.

Hope that helps!

V-For dynamic buttons spinner in VUE2 – Isolate each button loading spinner (VueJS 2)

If you’re dynamically showing buttons using V-FOR and want each button to show it’s own individual spinner if clicked, watch the below video.

This will show you how to make Vue know dynamically that the specific button has been clicked, using Vue2, Bootstrap and Fontawesome.