Showing posts with label Laravel. Show all posts
Showing posts with label Laravel. Show all posts

Thursday, 16 December 2021

Laravel deployment - prevent .env variable being exposed

https://stackoverflow.com/questions/46407009/how-to-hide-env-passwords-in-laravel-whoops-output 

https://laracasts.com/discuss/channels/laravel/laravel-has-a-security-hole

When deploying variable, in .env file, APP_DEBUG must be set to false,


if set to true the laravel developer friendly exception page will reveal all .env variables probably includes your database username pwd, or mail server username pwd.



when APP_DEBUG is set false in .env file

run 

// remove cache

php artisan cache:clear

// reload config

php artisan config:clear


Never set database username and pwd in .env file,  set in 



ALSO Change the default landing page from resources/views/welcome.blade.php to empty so hackers dont know it laravel

Tuesday, 23 November 2021

Laravel how to send email with smtp with google, and unitTest

 // https://stackoverflow.com/questions/32515245/how-to-to-send-mail-using-gmail-in-laravel

Laravel to use facades\Mail in laravel with google smtp , 

first in .env configure (config/mail.php uses these values)

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=myemail@gmail.com
MAIL_PASSWORD=apppassword
MAIL_ENCRYPTION=tls

// Next in Google, turn on less secure app
https://help.dreamhost.com/hc/en-us/articles/115001719551-Troubleshooting-GMAIL-SMTP-authentication-errors


// NOTE: when testing mail():: send with Laravel phpUnit, no actual email will be send . Only return true or throw exception.
To see actual effect, has to test with net work request or curl

Wednesday, 11 August 2021

Vue and Laravel how to start up a project from git 2021

 

Following is required to start Vue 2 Dev Project

npm
Vue CLI
Vue.JS(Vue2)

Start Vue Dev project front end server in development mode

$ git clone https://github.com/vueDevProject.git
$ git checkout Branch
$ cd Branch/VueAdminUI
$ npm install
// Set API URL
$ vim Branch\VueAdminUI\src\setting.js
// const Url = 'BackEndURL; $ wq! // Default dev mode port is 9527 // Change dev mode port $ vim Branch\VueAdminUI\vue.config.js
// devServer : { // port:9527 // }, $ wq! $ npm run serve

Laravel 5.3 back End Project


Following is required to start Laravel back end project

PHP 7.0 || PHP 7.2 || PHP 7.3
PHP EXTENSIONS : MYSQL, CURL, MBSTRING, XML
PHP MYSQL : apt-get install php7.<yourversion>-mysql
PHP CURL : apt-get install php7.<yourversion>-curl
PHP MB String: apt-get install php7.<yourversion>-mbstring
PHP DOM : apt-get install php7.<yourversion>-xml
COMPOSER (Make sure your composer is running your installed PHP 7.* version)

Start Laravel dev project back end server in development mode

$ git clone https://github.com/LaravelBackEndProject.git
$ git checkout Branch
$ cd Branch/LaravelBackEndFolder
$ composer install $ cp .env.example .env // Generate laravel application key in .env file $ php artisan key:generate // Generate jwt key in .env file $ php artisan jwt:secret // Open .env file and fill in the following parameters $ vim .env // (IMPORTANT)If you specified DB connection parameters in configs/database.php, remove all copied DB_* environment variables // (IMPORTANT)If DB_* environment variable is set, database.php configs will read environment variable on line 16 // (Mandatory) Fill in APP_URL, this should be your machine IP and Port which your laravel application is serving. You can specify Laravel development port by php artisan serve --host=0.0.0.0 --port=8080 // (Mandatory) Fill in APP_SERVER_BASE_URL, this should be your machine IP where your server application is using to serve.This variable serves as the base address for file upload. An example value can be http://10.0.0.106 // (Mandatory) Fill in FILE_SPECIFIC_PATH, this servers the specific directory path for file upload storage. An example value can be /home/appUploads/. Ubunutu permission set up required, please contact Joey for detail // (Optional) Fill in CAPTCHA_PRIV, this is google captcha private key only used for when interacting with frontend // (Optional) Fill in SERVER_ADDR, this is front end server address, do not need to be specified, if front end is not running // Clears config file cache for config file to be updated $ php artisan config:clear // Clears application cache $ php artisan cache:clear // Adding tables to db (first time only) // Check config/dabase.php to ensure the credentials && database name for db is correct $ php artisan migrate $ php artisan db:seed // End of adding tables to db // Starts Laravel dev server running at port 8080 at your machine IP $ php artisan serve --host=0.0.0.0 --port=8080 // Run Unit Test ./vendor/bin/phpunit

Sunday, 8 August 2021

Laravel Eloquent orWhere

 https://stackoverflow.com/questions/16995102/laravel-4-eloquent-query-using-where-with-or-and-or

# for orWhere to work, you have to have a where first


$a = 1;
$b = 1;
$c = 1;
$d = 1;
Model::where(function ($query) use ($a, $b) {
    return $query->where('a', '=', $a)
          ->orWhere('b', '=', $b);
})->where(function ($query) use ($c, $d) {
    return $query->where('c', '=', $c)
          ->orWhere('d', '=', $d);
});

Or you can do 

Model:where()->orWhere()



Tuesday, 3 August 2021

Laravel Eloquent insert() vs update() in return value

 Laravel Eloquent update()

it will return int.  Int return values will be the number of affected rows resulted by update(). 0 means no row. 


"An update query returns the number of rows that where affected by the update query."

https://stackoverflow.com/questions/38551397/laravel-dbupdate-only-return-0-or-1



Larave Eloquent insert()

It will return boolean (0 or 1). 0 means insert failed, 1 means success

"Yeah, Laravel will return a boolean saying if the query succeeded or not, so in your case just true. You can see that here:"

https://laracasts.com/discuss/channels/eloquent/return-value-from-dbinsert


https://github.com/laravel/framework/blob/8dde9527f42f061eaa68e32692d864d63835c57b/src/Illuminate/Database/Query/Builder.php#L2644

/**
* Insert a new record into the database.
*
* @param array $values
* @return bool
*/
public function insert(array $values)
/**
* Update a record in the database.
*
* @param array $values
* @return int
*/
public function update(array $values)

Wednesday, 28 July 2021

Larevel 5.7 eloquent increment, decrement simple and complete

// Simple way without where condition  

https://laravel.com/docs/5.7/queries#increment-and-decrement

DB::table('users')->increment('votes');

DB::table('users')->increment('votes', 5);

DB::table('users')->decrement('votes');

DB::table('users')->decrement('votes', 5);

You may also specify additional columns to update during the operation:

DB::table('users')->increment('votes', 1, ['name' => 'John']);



// Complex way with where condition 

https://stackoverflow.com/questions/29816123/increment-columns-in-laravel

DB::table('my_table')
   ->where('rowID', 1)
   ->update([
       'column1' => DB::raw('column1 + 2'),
       'column2' => DB::raw('column2 + 10'),
       'column3' => DB::raw('column3 + 13'),
       'column4' => DB::raw('column4 + 5'),
   ]);

Friday, 9 July 2021

Laravel Eloquent how to use increment to increment column value

https://laravel.com/docs/5.7/queries#increment-and-decrement

 DB::table('users')->increment('votes');


DB::table('users')->increment('votes', 5);

DB::table('users')->decrement('votes');

DB::table('users')->decrement('votes', 5);

https://stackoverflow.com/questions/37666135/how-to-increment-and-update-column-in-one-eloquent-query
Product::where('product_id', $product->id)
    ->update([
      'count'=> DB::raw('count+1'), 
      'last_count_increased_at' => Carbon::now()
    ]);