How-to articles, tricks, and solutions about PHP

How to reload/refresh model from database in Laravel?

In Laravel, you can use the refresh method on a model instance to reload the model's attributes from the database.

Symfony2: Redirecting to last route and flash a message?

In Symfony2, you can redirect to the last route using the redirect function and passing in the referer option:

How to select year and month from the created_at attributes of database table in laravel 5.1?

In Laravel 5.1, you can use the selectRaw method to select the year and month from the created_at attribute of a database table.

How to get last insert id in Eloquent ORM laravel

In Laravel, you can use the save method provided by Eloquent to insert a new record and retrieve the last insert ID.

Insert data into multiple tables using one form

To insert data into multiple tables using one form in PHP, you can use a single SQL statement with multiple INSERT INTO clauses, or you can use multiple individual SQL statements, one for each table.

How to install php-redis extension using the official PHP Docker image approach?

To install the php-redis extension using the official PHP Docker image, you can use the docker-php-ext-install script that comes with the image.

Issue reading HTTP request body from a JSON POST in PHP

In PHP, you can read the body of an HTTP request using the file_get_contents('php://input') function.

PHP generate file for download then redirect

In PHP, you can use the header() function to send a "Content-Disposition" header to prompt the user to download a file.

Execute root commands via PHP

It is generally not recommended to execute root commands via PHP, as it can pose a security risk to the system.

"call to undefined function imagecreatetruecolor" error in PHP & pChart

The "call to undefined function imagecreatetruecolor" error in PHP typically occurs when the GD library, which is required to create image resources, is not installed or enabled on the server.

Multiple Inheritance in PHP

In PHP, multiple inheritance is not supported in the traditional sense, where a class can inherit from multiple classes.

Where do we use the object operator "->" in PHP?

The object operator "->" is used in PHP to access a property or method of an object.

Facebook SDK returned an error: The "state" param from the URL and session do not match

This error message appears when the SDK is being used to make an OAuth request to Facebook, and the "state" parameter in the URL doesn't match the "state" value that was stored in the user's session.

reliable user browser detection with php

There are several ways to detect a user's browser using PHP.

Fatal error: Class 'NumberFormatter' not found

This error message is indicating that the PHP script is trying to use a class called "NumberFormatter" but it cannot be found.

How to Format Laravel Blade Code in Visual Studio Code?

You can use the Laravel Blade Snippets extension for Visual Studio Code to format Blade code in your Laravel projects.

Simple example to post to a Facebook fan page via PHP?

Here is an example of how you can post to a Facebook fan page using the Facebook PHP SDK:

PHP : Remove object from array

Here is an example of how to remove an object from an array of objects in PHP:

PHP Fatal error when trying to access phpmyadmin mb_detect_encoding

A PHP Fatal error is a type of error that occurs when the PHP interpreter encounters a serious problem and is unable to continue executing the script.

How are \r \t and \n different from one another?

The characters "\r" (carriage return), "\t" (tab), and "\n" (newline) are all special characters used in strings in the PHP programming language.

Best way to document Array options in PHPDoc?

When documenting an array option in PHPDoc, you can use the @var tag followed by the type array and a short description of the option.

How to access an application parameters from a service?

In PHP, you can access application parameters from a service by injecting the ParameterBag object into the service class constructor.

How to get a Youtube channel RSS feed after 2015 April 20 (without v3 API)?

After April 20, 2015, YouTube discontinued support for the RSS feed feature in the version 2 of their API.

How do I generate a custom menu/sub-menu system using wp_get_nav_menu_items in WordPress?

To generate a custom menu/sub-menu system using wp_get_nav_menu_items in WordPress, you can follow these steps:

Uploading files with SFTP

To upload files using SFTP (Secure File Transfer Protocol) in PHP, you can use the SSH2 extension, which allows you to connect to an SFTP server and perform various operations such as uploading, downloading, and deleting files.