Category Archives: PHP

Anything related to PHP and any Frameworks associated.

Eloquent helper functions, filters and mutators

I have been looking through the internet how I can make my coding more efficient specially when using Eloquent ORM. Eloquent returns a collection that can be filtered using various helper functions.

I would say how I can make my programming skills be more “functional” instead of the traditional for each loop. Check the link out for using helper functions once collection has been returned. 

I will also add Accessors/Mutators basically when you set or access a field value , it will automatically returns a different format. For basic example, $users->firstname original value is “Sarah” if you used an accessor function defined in your model, you can automatically make the value apply any function such as strtolower(value) to make it return lowercase.

I find this link helpful: 

Love at first Laravel Eloquent

Its been a while since I posted been a month it seems?

Well I have been doing a lot of developing and I just love Laravel’s Eloquent component. When doing small web applications, you dont wanna go full Laravel on that stuff. You would wanna just go small like Lumen small, when I say Lumen like micro-framework Lumen. Laravel is a full blown framework, if you just want a small application you can never go wrong with a micro-framework, there are a lot out there. Im currently working on Slim 2 Framework although very old, I needed it to support really old PHP 5.2.

I have learned alot, on my first month on the job. Namely composer and Im using alot of package manager lately. NPM and bower front end development stuff. But anyways I would like to share you links on how to make ORM Eloquent and using 2 databases without Laravel altogether.

First is how to use Eloquent ORM on your own project follow this link: 

Then how to use 2 database connections, this is really big if you are using SQL Server and MySql hand on hand you need to be able to switch between your eloquent database. Follow link:

Bootstrapping – Relative Path trailing slash / (PHP)

When developing a site with bootstrapping in mind, (by the way bootstrapping is a technique in web development when all request gets directed to one page that is usually index.php for example a request to page, , etc. is handled by index.php with the help of a mod_rewrite script in the root directory. And creates a slug url for SEO.) when this is done an issue will occur relating to relative path and pointing to static files in your site such as images, css and scripts especially . Imagine this scenario:
Examine the file structure


When someone request a url from your site eg. “http://domain/user” and in the page your css points to “css/style.css”, your page will come out alright but when they add a slash at the end “/” “http://domain/user/” like so, your browser will interpret the slash as a folder location to “user/” so if your path to your css is “css/style.css”, your browser will look for the file in “user/css/style.css” which in this case the folder doesnt exist and so your website will come out really messed up.

So remember to fix this when you are developing a site based on bootstrapping. You can fix this either by .mod_rewrite or simply via your PHP script by checking the trailing slash at the end from the REQUEST_URI variable.
Until next time.

PHP Header Function – Redirect – Exit

I was developing my site when something weird happened , mostly sessions get UNSET in the end of my php script, but when developing In the middle script a redirect function like the “header(‘location: http://mydomain/etc’);” I assumed once redirected the any script below it wont run anymore but I was wrong.

Then as soon as the header function has been called, the script below it ,has triggered as well while performing redirect. At first I didnt know what was wrong with it until I just unknowingly added EXIT; after the header function and everything works fine.

So for my reference ALWAYS REMEMBER TO PUT an exit function AFTER the redirect function so that there wont be any weird issues developing your site along the way. Especially when your developing using PHP.