Simplified way of processing large data using chunk() in laravel » Decode Web

Simplified way of processing large data using chunk() in laravel » Decode Web

Chunking the array or collection is the best way to process large data in PHP. It efficiently manage memory issues and uses less time for script execution.

Chunking the array or collection is the best way to process large data in PHP. It efficiently manage memory issues and uses less time for script execution.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

What’s new in HTML6

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

In Laravel 6 tutorial, we learn how to use PHP artisan command interface (CLI) to clear the cache from Laravel 6.8 application. How To Clear Cache in Laravel 6.8 Application using Artisan Command Line Interface (CLI)? How to clear route cache using php artisan command? How to easily clear cache in Laravel application? How to clear config cache in PHP Laravel via artisan command? How to clear Laravel view cache? How to Reoptimized class in Laravel via artisan CLI?

Today in this tutorial, we are going to learn how to clear route cache, laravel application cache, config cache, view cache and reoptimized class in a Laravel 6.8 application using artisan command-line interface.

I’m pretty sure many of you may have found yourself gotten into the situation where you do not see changes in the view after making the changes in the app.

Laravel application serves the cached data so caching problem occurs due to the robust cache mechanism of Laravel.

But, if you are still facing this issue, then you do not have to worry further. Let me do the honour of introducing you some of the best artisan commands to remove the cache from your Laravel app via PHP artisan command line interface.

Artisan is the command-line interface included with Laravel. It provides a number of helpful commands that can assist you while you build your application.

Table of Contents

  • Clear Route Cache in Laravel
  • Clear Laravel Application Cache
  • Clear Config Cache via PHP Artisan
  • Clear Laravel View Cache
  • Reoptimized Class
Clear Route Cache in Laravel

Laravel caching system also takes routes in consideration, to remove route cache in Laravel use the given below command:

php artisan route:cache
Clear Application Cache in Laravel

Run the following command to clear application cache:

php artisan cache:clear
Clear Config Cache in Laravel

Run the following command to clear config cache:

php artisan config:cache
Clear View Cache in Laravel

Run the following command to clean your view cache:

php artisan view:clear
Reoptimize Class

Run the below command to reoptimize the class loader:

php artisan optimize

Conclusion

We have completed this Laravel 6 tutorial, In this tutorial we learned how to use php artisan command to clear the cache from your Laravel application. We have answered the following questions in this article.

  • How to clear route cache using php artisan command?
  • How to easily clear cache in Laravel application?
  • How to clear config cache in PHP Laravel via artisan command?
  • How to clear Laravel view cache?
  • How to Reoptimized class in Laravel via artisan CLI?

Now, it’s your time to let me know what do you think about this laravel 6 article. Go forth and try these super awesome artisan commands and let me know how these commands are helping you.

How to use Corcel in Laravel to CRUD Wordpress Data

How to use Corcel in Laravel to CRUD Wordpress Data

As a developer, you may find yourself in a situation where you want to make use of the data coming from your WordPress blog. And that's where Corcel comes to the stage.

Originally published by Ivica Jangelovski at https://adevait.com

Laravel and Wordpress are with no doubt the most recognized open-source projects in the PHP community. The first one gives developers complete freedom and flexibility like no other framework before. The second one is the most widely used CMS across the internet, covering more than 34% of all existing websites. Yes, it’s true, at least that’s what the WordPress core people are saying on the official site. WordPress is also highly customizable, allowing developers to extend its functionality by leveraging the Wordpress developer APIs. Both of them are the “representatives” of the PHP language in its community nowadays.

As a developer, you may find yourself in a situation where you want to make use of the data coming from your WordPress blog. And that's where Corcel comes to the stage.

Table Of Contents

  • What is Corcel?
  • Why choose Corcel over the WP REST API?
  • Installation and configuration
  • Code Samples
  • Corcel and ACF
  • Summary

What is Corcel?

Corcel is a standalone PHP package which aims to allow developers to easily fetch, manipulate or communicate (doing CRUD operations) with the data from your WordPress blog.

The codebase of Corcel consists of a collection of PHP classes built on top of Eloquent ORM (from Laravel framework), that provides a fluent interface to connect and get data directly from a WordPress database.

Why choose Corcel over the WP REST API?

Good question. And the answer is the efficiency and speed that Corcel provides. It's simple. This package communicates directly with the WordPress database to give all it needs, while the REST API is calling WP and WP is calling the database (duplicating actions unnecessary). And that's not very likely to be efficient enough. Someone already raised that question so you can read more on this question on this GitHub issue. But let's go and try it.

Installation and configuration

As I said above, Corcel can be used as a standalone package on every PHP project, but the following examples are only for a Laravel installation. For more details about including Corcel in your other (not Laravel) PHP project, you can read the documentation on GitHub (the package repository has very clean and well-written documentation along with some usage examples).

Install with composer

The installation is pretty straightforward using the well-known composer. Run the following command from your terminal in order to install Corcel as a dependency:

composer require jgrossi/corcel

Now that we have Corcel added as dependency let's do the configuration work.

Configuration

Open your config/app.php file and include the CorcelServiceProvider at the bottom of the providers list:

'providers' => [

    Corcel\Laravel\CorcelServiceProvider::class,

]

Next step is to run the following artisan command from your terminal:

php artisan vendor:publish --provider="Corcel\Laravel\CorcelServiceProvider"

This will create the config/corcel.php configuration file in your config directory. We’ll get back to it later (after the main database config).

Database configuration

Open the main database configuration file config/database.php. Here you already have the connections array. In order to configure the params for the WordPress database that we'll be using, you’ll need to add one more element to it like this:

'corcel' => [ // You can name this connection name as you wish 

        'driver'    => 'mysql',

        'host'      => 'localhost',

        'database'  => 'wordpress_database',

        'username'  => 'root',

        'password'  => 'root',

        'charset'   => 'utf8',

        'collation' => 'utf8_unicode_ci',

        'prefix'    => 'wp_',

        'strict'    => false,

        'engine'    => null,

],

Now go back to the config/corcel.php file and add the name of the connection that you just created to the connection element (in our case it’s named corcel) and that's all (regarding the configuration of course)! Now let's see some usage examples from Corcel.

Code Samples

Corcel has few predefined models for the Posts, Pages, Meta, and all other entities. But you can extend those very easily. I'll provide a few examples of using Corcel to fetch Posts and Metadata.

Posts

Create a model called Post.php and at the top of it use the Corcel core Post model:

use Corcel\Model\Post as Corcel;

Get a specific post by ID:

Post::find($id);

Get the latest posts from the post types post, cptname and othercpt that are assigned to the taxonomies category and category2:

Post::status('publish')
           ->whereIn('post_type', ['post', 'cpt_name', 'other_cpt'])
           ->whereHas('taxonomies', function ($query) {
               $query->whereIn('taxonomy',['category', 'category2']);
           })
           ->newest()
           ->get();

A method inside the model that returns the URL of the post featured image with the size that's given as parameter:

public function getPostThumbnailUrl($size = '')
{
   if ($this->thumbnail !== null && $this->thumbnail->size($size) !== null) {
      return $this->thumbnail->size($size)['url'];
   }

   return '';
 }

Metadata

According to the Codex, the post metadata is the "administrative" information you provide to viewers about each post. This information usually includes the author of the post, when it was written (or posted), and how the author categorized that particular post. Of course, this is only by default. Developers usually extend this data to fit their needs. One example of using the custom metadata is leveraged by YoastSEO - a plugin that helps you optimize your post SEO scores. It adds a unique meta description for each post in the <head> tag. One way to fetch this data to your post view in Laravel is to create a method in the Post model:

public function getYoastDescription()
   {
       return array_key_exists('_yoast_wpseo_metadesc', $this->getMetasAttribute()) ? $this->getMetasAttribute()['_yoast_wpseo_metadesc'] : '';
   }

As you can see this method is already using some methods that are written in the Corcel's core. We are just using them to make things cleaner in our view:

@section('metaDescription', $post->getYoastDescription())

The above will print the <meta> tag with the content attribute filled with the value that returned our method.

Corcel and ACF

There is also a way to get data from the ACF (Advanced custom fields) plugin. This plugin is essentially storing data as Metadata but in a more convenient way. In order to use it, you'll have to install it as a standalone package because it's not included in the main one that I wrote above. For more details check the repo for this package and you'll find the instructions there.

Summary

So far we have covered some ways of using Corcel. But there are many other examples that you can make use of that can be found in the official documentation on GitHub. We saw why you should use Corcel over WP REST API to fetch data into Laravel and how that benefits you. 

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

PHP with Laravel for beginners - Become a Master in Laravel

Projects in Laravel: Learn Laravel Building 10 Projects

Laravel for RESTful: Build Your RESTful API with Laravel

Fullstack Web Development With Laravel and Vue.js

Laravel 5.8 Tutorial for Beginners

Laravel 5.8 Ajax CRUD tutorial using Datatable JS

Laravel 5.8 Tutorial from Scratch for Beginners

Upgrading Laravel To 6.0 From 5.8

Laravel 6 Release New Features and Upgrade


7 Best Features That Make PHP Laravel Framework A Winner

7 Best Features That Make PHP Laravel Framework A Winner

PHP is one of the most popular web development language used for app and website development. Twenty million plus domains including big, small, startups and so on are using it. The popularity of PHP is since it suits the needs of all business types.

Moreover, it has a big community that helps developers solves issues with coding and create a web application quickly.

In this blog, we will talk about a popular PHP framework- Laravel. Laravel framework is one of the most important web frameworks offered by PHP. This platform is feature rich, and aids in creating high quality and performance oriented web applications. It is new but packed with a plethora of features that helps build the best of apps you see in the market.

Laravel- An Introduction

Taylor Otwell is the brain behind Laravel. It is an open-source PHP web framework that is free. It uses the model-view-controller (MVC) and architectural pattern to develop web applications. The popular PHP framework was released in June 2011. The current version of Laravel in the market is 5.6.

Market Statistics of Laravel

Graph showing the use of Laravel in different industry:

All the above graphs show that Laravel is in high demand. The main reason for its high market share is that its well-designed tool lets the developer write fewer codes. Fewer codes, in turn, guarantee less human error and better quality app.

PHP Laravel Framework ensures the building of app that has high value and productivity. The no of repetitive code is also less that is why Laravel Development framework is the go-to for many app development companies.

Features that make PHP Laravel Framework the best

Here are seven features that make Laravel development framework the best amongst all PHP frameworks.

Light-weight templates

Laravel web development framework has the best and most light-weight inbuilt templates. It helps the developer in creating awesome layouts using dynamic content. Moreover, it has multiple widgets that incorporate CSS and JS code. Designs of the templates of this framework are such that you get a simple layout but with distinctive sections.

Artisan

The popular PHP framework Laravel has a built-in tool for command line called Artisan. The tools aid the developer to perform most of the repetitive programming functions that they don’t want to do manually. It is also used to build the database structure and skeleton code. Developers can use to build database migration for managing the database system easily. Artisan also helps PHP developers to create their own commands and do various things with it.

Libraries & Modular

Laravel framework has numerous object-oriented libraries and other libraries. These pre-installed libraries make it popular as they are not present in any other PHP framework. One of the most popular preinstalled libraries is Authentication library. It is easy to integrate and use and many features like checking active users, resetting the password, cross-site request forgery and so on. Moreover, it divides itself into individual modules that use modern PHP principles. It aids the developers in building responsible and handy web applications.

MVC architecture

Laravel development framework’s MVC pattern ensures clear in presentation and logic. The MVC architecture support aids in enhancing the performance allows better documentation and has many other in-built functions.

It has a huge migration system for databases

Laravel has a migration system. It helps in expanding the structure of the database of the web application. It expands the database without the need to re-create it every time you make any changes to it. Due to this feature, there is less chance of data loss. Not only can you change the database with Laravel, but it also uses PHP for it instead of SQL.

Unit-Testing

Another feature that makes PHP Laravel framework preferable is the way it does unit testing. With Laravel, you can run multiple tests to check that the new changes don’t break anything in the web application. Using Laravel developers are confident about app launching as it takes care of known failure. Also, it is easy to write unit tests for codes.

Security

When you are in the middle of web application development, ensuring its safety is your biggest concern. That is why; developer takes all precaution to keep the security intact. With Laravel, programmers feel safe as it takes care of the security within its own framework. It uses hash and salted password to ensure the safety meaning you can never save it as plain text in a database. Moreover, it utilizes “Bcrypt Hashing Algorithm” to encrypt password.

Conclusion:

These features make PHP Laravel framework popular amongst developers who prefer it above other frameworks. It has numerous features that ease the job of developers. Also, this web development framework lets the developer create their own command. Moreover, they can create an infrastructure specially designed for their web application.