Should I create a new Git branch locally or on GitHub first?

It’s generally better to create a new Git branch locally first and then push it to GitHub. Here’s why:

1. Create the Branch Locally First

Creating a branch locally gives you full control and flexibility before sharing it with others. The workflow typically looks like this:

  1. Create the branch locally:
    git checkout -b new-branch-name
  2. Work on your changes.
  3. Commit your changes:
    git add .
    git commit -m "Description of changes"
  4. Push the branch to GitHub:
    git push origin new-branch-name

Benefits of Creating Locally First:

  • Control: You have the opportunity to make changes and commits before sharing the branch on GitHub.
  • Avoiding Mistakes: It’s easier to catch and fix any mistakes locally before making the branch visible to others.
  • History: Commits will already be in place when you push, giving collaborators more context about your changes.

2. Create the Branch on GitHub First

You can also create a branch directly on GitHub and then fetch it locally:

  1. Create a new branch on GitHub via the web interface.
  2. Fetch and check out the branch locally:
    git fetch origin
    git checkout -b new-branch-name origin/new-branch-name

When to Create on GitHub First:

  • Collaboration: If you need to create a branch quickly for collaboration (e.g., for someone else to start working on it immediately), creating it on GitHub first can make sense.
  • Protection: Sometimes, teams enforce branch protection policies (e.g., on the main branch). Creating branches on GitHub might automatically apply those policies.

Summary:

  • Local First: Provides flexibility, control, and is the most common practice for individual developers or when working on a feature before sharing it.
  • GitHub First: Useful for initiating collaboration quickly or if your team has specific branch management policies.

In most cases, creating the branch locally first is the preferred approach.

What is `protected $fillable` in a Laravel model?

In Laravel, the `protected $fillable` property is used to define an array of attributes that are mass assignable. This means these attributes can be assigned using mass-assignment techniques, such as when creating a new model instance or updating an existing one using the `create` or `update` methods.

Mass Assignment

Mass assignment is a way to assign multiple attributes to a model instance in a single step, typically using an array. For example, you might have a form where a user can submit several pieces of information at once. Instead of assigning each piece of information individually, you can pass the entire array to the `create` or `update` method.

Here’s an example of how you might use the `$fillable` property in a Laravel model:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    // Define the attributes that are mass assignable
    protected $fillable = [
        'title', 
        'content', 
        'author_id'
    ];
}

In this example, the `Post` model has three attributes (`title`, `content`, and `author_id`) that are mass assignable.

Using Mass Assignment

With the `$fillable` property defined, you can now safely use mass assignment:

// Creating a new post
$post = Post::create([
    'title' => 'My First Post',
    'content' => 'This is the content of my first post.',
    'author_id' => 1
]);

// Updating an existing post
$post->update([
    'title' => 'Updated Title',
    'content' => 'This is the updated content.'
]);

The primary purpose of the `$fillable` property is to prevent mass-assignment vulnerabilities. Without `$fillable` (or its counterpart `$guarded`), any attribute in the model can be mass assigned, which could potentially allow malicious users to update sensitive fields that they shouldn’t have access to.

Mass Assignment Vulnerability

So what is this mass assignment vulnerability? Consider a scenario where a user can submit their profile information. If the user model has an attribute like `is_admin`, and this attribute is not protected, a malicious user could submit a form with an `is_admin` field and set its value to `true`, giving themselves admin privileges.

By defining the `$fillable` property, you explicitly specify which attributes are safe to be mass assigned, thus mitigating this risk.

So, using the `$fillable` property is a best practice in Laravel to ensure that only the intended attributes can be mass assigned, enhancing the security of your application.

Should I create Git repository in public_html or different folder?

It is generally not recommended to create a Git repository directly inside the `public_html` folder of your web server. The `public_html` (or `public` or `www`) directory is typically the web server’s root directory, and its contents are accessible to the public through the internet. Placing your Git repository there could expose sensitive information and potentially compromise your code’s security.

Instead, it is a good practice to keep your Git repository outside the `public_html` folder and use a deployment strategy to publish your code to the web server.

Here’s a common directory structure:

In this setup, the `public_html` folder contains only the files necessary to serve your website to the public. Your actual code resides in the `git_repository` folder, which is not accessible to the public.

When you’re ready to deploy a new version of your website, you can use various methods to transfer the relevant files from the Git repository to the `public_html` folder. Some common deployment strategies include:

1. Git pull (or clone) on the server: SSH into your server, navigate to the `git_repository` folder, and pull the latest changes from your remote repository. Then, copy the required files to the `public_html` folder.

2. Deploy script: Write a script that automates the deployment process. The script can pull the latest changes from the Git repository and copy the necessary files to the `public_html` folder.

3. CI/CD tools: Use continuous integration and continuous deployment (CI/CD) tools like Jenkins, Travis CI, or GitHub Actions to automate the deployment process whenever you push changes to your Git repository.

Using one of these deployment strategies will help keep your sensitive code and configuration files secure, while still allowing you to publish your website to the public_html directory for the world to access.

WordPress Custom Fields missing in admin (post or page)

This is a very specific problem so it might only work for some. After I updated WordPress and all plugins on an old site that used WordPress Custom Fields to enter some data, the Custom Fields in admin disappeared. I could add a new post but Custom Fields were nowhere to be sen, not even in the screen options.

Long story short, after doing some searching on the net, I found out it was ACF (Advanced Custom Fields) fault. In new version of the plugin they disable WordPress Custom Fields because they think you are using ACF anyway so no need for both. Not sure I agree but here is 2 ways you can try to solve it:

  1. If you activated ACF but are not using the plugin, just disable it.  WordPress Custom Fields should come back.
  2. If you want to keep both then add this code to your theme’s function.php file (the best is the child theme, if you have one)
    add_filter('acf/settings/remove_wp_meta_box', '__return_false');

WordPress Custom Fields should reappear now.

Using Laragon for Laravel projects

I am in the process of migrating one of my sites from Codeigniter (anyone remember it? It promised so much but then fell behind, especially when Laravel came out years ago) to Laravel. It’s been a while since I used Laravel but I always love developing with it.

I always use XAMPP for local development but luckily I googled for alternatives and found Laragon. What an awesome find. At first I thought it was just for Laravel, due to name Laragon but you can use it for everything.

So here is how easy it was to install latest Laravel and start local development.

  1.  Go to Laragon website and download it, full or lite. Install (also select development folder) and start the services. Now you have a running web server!I changed few preferences (I don’t want it to start with Windows but I want it to start minimized and start ALL services at once) but you can do whatever is best for you.Another great thing: It comes with my favorite Console Emulator CMDER (not my screenshot):
  2. Let’s install Laravel. Right click blue (or green) Laragon tray Icon -> Quick App -> Laravel, name the project (folder)  and Laragon will begin to install Laravel via console. If you get asked for admin permission to open console (for hosts if I remember correctly) say yes.   When it finishes you should be able to open the web at ProjectName.test . I have to say that the first time I installed Laravel the scripts in console did not finish so it didnt work. I deleted the project folder, restarted the step  2. again and then it worked – Laravel was installed!
    laragon install laravel
  3.  I noticed that phpmyadmin was not installed. It is super easy again to install it :  Right click blue (or green) Laragon Icon -> Tools -> Quick Add -> *phpmyadmin . Wait few moment and you can access it at localhost/phpmyadmin/

And that is all. I installed local web server, Laravel and phpmyadmin with few clicks.

Share localhost over the internet with ngrok

One of the more annoying things when developing on your local computer is that your clients or friends can’t see what you develop. You can upload it on your web server or you can work directly on the web server but that is inconvenient and slow. So I was very happy to discover this great tool called ngrok. There are similar tools (localtunnel etc) but this one is really easy to use.

Who is your daddy and..no..wait..what is ngrok and what does it do?

Ngrok is a cool little program that creates a tunnel from the public internet (http://subdomain.ngrok.com) to a port on your local machine. You can give this URL to anyone to allow them to try out a web site you’re developing without doing any deployment. Of course your local server has to run at the time of sharing but that is not a problem – you just leave your PC on.

How do I run this ngrok?

With ngrok it is super simple to expose a local web server to the internet. On Windows you just download ngrok.exe, unzip it some folder and then in command prompt (cmd) you just tell ngrok which port your web server is running on.

This is the most simple way (This opens port 8080 on your local machine to the internet). Type this in your command prompt:

ngrok 8080

You will see something like this :

Tunnel Status                 online
Version                       0.11/0.8
Protocol                      http
Forwarding                    http://345355bc.ngrok.com -> 127.0.0.1:8080
Web Interface                 http://localhost:4040
# Conn                        0
Avg Conn Time                 0.00ms

Of course the port (in this case 8080) has to be the port that your localhost is running on. Now just give the generated URL (in our example http://345355bc.ngrok.com) to your client or friend and let them test out your application.

You can do a lot more with ngrok, like inspecting your traffic, XML/JSON syntax checking, replaying requests, requiring passwords to access your tunnels, accessing your tunnels over HTTPS, forwarding non-HTTP services, forwarding to non-local services and more.

Check out ngrok here.

How to remove horizontal scrollbar in Twitter Bootstrap tab function

While building my new admin/CMS with Laravel 4 and Gebo Admin (really cool twitter bootstrap responsive admin template) I encountered weird problem while using bootstrap tabs –  after I added “enhanced select” (Country dropdown) suddenly a horizontal scroll bar appeared!

How to remove horizontal scrollbar in tab function twitter bootstrap

I am not sure if this is 100% best solution (I am not that keen on digging into all this .js stuff) but the easiest way to fix this is to open Bootstrap.min.css, search for .tab-content and then replace

overflow: auto;

with

overflow: hidden;

Reload your page and the horizontal scrollbar is gone.

Missing XML tag This required tag is missing. Please add it and resubmit ror.xml

Why do I need sitemap?

Sitemaps are a very useful way to  allow a webmaster to inform search engines about URLs on a website that are available for crawling. If you are using WordPress or some other CMS you can simply install a plugin (or maybe XML sitemap generation is already inbuilt module) and it will automatically create a sitemap for you. But sometimes you want to create a sitemap by yourself. There are several free services that allow you to create sitemaps for free, like xml-sitemaps.com , rorweb.com and so on. After you create a sitemap you should upload it to your website and submit to to Google,Yahoo etc.

That was a little sitemap introduction and now to the problem I encountered.I am not going to go into discussion if ROR sitemaps are better than Google/XML sitemaps or even needed (I don’t really use it but some people might find it useful). I am just describing solution to a possible problem you might encounter when making a ROR sitemap.

Google Webmaster Tools and ror.xml error

I built a ROR sitemap using xml-sitemaps.com site,uploaded to my site  and added it in Google Webmaster Tools admin and Google showed me this error:

Missing XML tag
This required tag is missing. Please add it and resubmit.

Parent tag: channel
Tag: description

At first I thought that xml-sitemaps.com ROR generator is outdated so I used one at rorweb.com/rormap.htm but I got the same error.  After searching the net I found the solution is very simple:

Open the ror.xml file in your text editor and find this (the beginning of the document,it might look a little different)

<channel>
<title>ROR Sitemap for http://www.yoursite.com/</title>
<link>http://www.yoursite.com/</link>
<item>

and paste the description line between the title the link lines so it will look like this:

<channel>
<title>ROR Sitemap for http://www.yoursite.com/</title>
<description>ROR Sitemap for http://www.yoursite.com/</description>
<link>http://www.yoursite.com/</link>
<item>

Save the file,reupload to your server and resubmit to Google Webmaster Tools (refresh the page) and the ‘Missing XML tag This required tag is missing. Please add it and resubmit’ error should disappear.