How to change or add a new value to a Laravel request parameter

The solution is in Part B.

Part A: ( Introduction )

Let’s first see how merge() helper function works in Laravel:

The merge method merges the given array or collection with the original array or collection. If a string key in the given items matches a string key in the original collection, the given items's value will overwrite the value in the original collection:

$collection = collect(['product_id' => 1, 'price' => 100]);
$merged = $collection->merge(['price' => 200, 'discount' => false]);
$merged->all();

// ['product_id' => 1, 'price' => 200, 'discount' => false]


In general, you can check simply using:

use Illuminate\Support\Collection;

....
if($variable instanceof Collection) {
....
}

Another example:

We know that Illuminate\Database\Eloquent\Collection extends from Illuminate\Support\Collection

Suppose:

$record = User::all();

Then:

dump($record instanceof \App\User); // returns false

While:

dump($record instanceof \Illuminate\Database\Eloquent\Collection); // returns true

This is what we usually do in a controller to validate a request:

$validated = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($request->all(), [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);

How to remove cached config, route, view file in Laravel

To see all your routes compactly, you need to do:

php artisan route:list --compact
Image for post
Image for post

php artisan optimize will do the following tasks:


How to switch from Laravel local env to production

You have one .env file, but the contents can differ on each machine you run the application.

Do not forget to run the command php artisan config:clear after you have made the changes to the .env file. Do this php artisan env, which will return the correct version and assures you about what it is actually set!

Image for post
Image for post

The role of the .env file is to allow you to have different settings depending on which machine you are running your application. So on your production server, the .env file settings would be different from your local development environment.

production and local are just environment names that you can use to turn certain testing features on or off in different places. …


Laravel Artisan Vendor Command

php artisan vendor:publish
Image for post
Image for post

Open a file/project in PhpStorm using the command line

Solution 1:

For opening your current working directory in PhpStorm

You need first open Phpstrom and then go to Tools menu and find the

Create Command-line launcher
Image for post
Image for post
Tools -> Create command line launcher

A curated list of amazingly awesome PHP libraries, resources and shiny things.

Github link


A curated list of awesome Python frameworks, libraries, software and resources.

Github : link

About

Panjeh

Web geek, Self-taught full-stack web developer, Learning Python, Laravel, Vuejs, UX/UI design, Nuclear Physicist PhD

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store