Monday, August 13, 2018

Laravel Models Eloquent Dynamically change table name

Some times you need to use one model for many tables, that means you want the same model but with many tables . So the property table will be dynamically set .to achieve this all you have to do is to use a trait that do the job as shown in the following code:

 

 /* DynamicTable.php */ 
 
trait DynamicTable
{
    protected $connection = null;
    protected $table = null;

    public function bind(string $connection, string $table)
    {
        $this->setConnection($connection);
        $this->setTable($table);
    }

    public function newInstance($attributes = [], $exists = false)
    {
        // Overridden in order to allow for late table binding.

        $model = parent::newInstance($attributes, $exists);
        $model->setTable($this->table);

        return $model;
    }

}


let's say our Model is called Profile :
require_once("DynamicTable.php"); 
 
class Profile extends Illuminate\Database\Eloquent\Model {
 
 use DynamicTable;
 
 public $timestamps = false; 
   
} 
 

Now, when you need to retrieve Model you will create a new object of this model and assign table and connection if the connection is different from default:
profiel1 = new Profile;
prfoile->setTable ("profile01");
profile->setConnection("mysql");
$results = $profile->find(123);

To insert data you can do the same .
Some retrieving commands will not work like all(), but you can use where(1,1) and it will do the job.

Saturday, July 21, 2018

Using native Nodejs Modules in Electronjs in Windows


First of All prepare package.json file to do the job and add the following script option:

rebuild": "electron-rebuild -f -w  sqlite3 

Here We are taking sqlite3 as an Example for the native Nodejs Module that we want to use it with electron App.
Install electron-rebuild :

npm install --save-dev electron-rebuild

Now,before you can compile node module to serve electron app you have to run the command :

npm install --global --production windows-build-tools

This command will install Python2.7 (Python 3 is not supported) and windows build tools . you may get this error :

events.js:183   throw er; // Unhandled 'error' event
Error: spawn powershell.exe ENOENT.

This is because the npm cannot run Powershell Command ,So to solve this error Add PowerSell Folder to the environment system variable Path:
%SystemRoot%\system32\WindowsPowerShell\v1.0

Then restart windows.

Go to this folder on your system : C:\Program Files (x86)\MSBuild

And find what folder available their.  if you have folder (14) and this folder has (Bin) folder with a file called MSBuild.exe in it , then you can use MS version 2015.
If there is a folder called (15) that has (Bin) folder with a file called MSBuild.exe in it , then you can use MS version 2017. let say you have the folder (14).then run this command :

npm config set msvs_version 2015

if you didn't choose the right msvs version you may get a dump Error which has code and errno :

npm ERR! code ELIFECYCLE
npm ERR! errno 4294967295

or ,if the version is not the right one. you may get the error :

 MSBUILD : error MSB4132 

Also Expected error is :

gyp ERR! stack Error: Can't find Python executable , you can set the PYTHON env variable
 
but this is a clear error and will disappear only after success of this command :

 npm install --global --production windows-build-tools
 

And by Adding the Python Folder to the Path variable that you have to do it by your self.

Sunday, April 22, 2018

wordpress refuse to install plugins through dashboard

WordPress showed many permission problems when we tried to install it on a fresh install of Unix -- Ubuntu 16.04, the first and most annoying problem is the plugins from WordPress.org will not install locally and the WordPress continue to complain of access problem and asking for ftp credentials.

So, to solve this issue the first thing to do is change the ownership of WordPress folder to the  www-data:

sudo chown -R www-data:www-data /var/www/html


and change the chmod for the WordPress folder to allow write and read for the wp-content folders


the most important step to avoid this problem is to add this option to wp-config.php

define('FS_METHOD', 'direct');

Here is a nice blog post which explain how to install WordPress the right way :

install-wordpress-ubuntu-16-04

Friday, April 20, 2018

Error: More than one instance of bitcore-lib found

this error was shown when I tried to run bitcored on linux after adding new service which was the bitcore wallet service , the problem happened after each sub module trying to run its own copy of bitcore-lib, the bitcore.node depends on version "bitcore-lib": "^v0.13.19", and the bitcore-wallet-service depends on version "bitcore-lib": "^v0.15.0". So we have to remove one of these dependencies from the package in order to solve this problem:

npm uninstall --prefix /mynode/node_modules/bitcore-wallet-service -S bitcore-lib


Sunday, March 4, 2018

Keep the process of a program running after closing the shell terminal

to keep the process running after the shell terminal close on ubuntu 16.04 use the following
command:

nohup my_command > my.log 2>&1 &

if you don't want to log the output of this command to a log file the replace my.log by
/dev/null, So it will be like the following:

nohup my_command > /dev/null 2>&1 &

if you want to find the process after a while and want to kill the process then, list all
the running processes:

ps -ef

you will find the command in the CMD column of the diplayed list. Now find the process Id from
the PID column and insert the kill command

sudo kill pid

How to Secure your ubuntu server - essential steps




create new user $ adduser username
$ usermod -aG sudo username
$ echo 'username ALL=NOPASSWD ALL' >> /etc/sudoers

$ nano /etc/ssh/sshd_config
change ssh port from 22 to 9022
#Port 22
Port 9122

Disable Root Account
change in /etc/ssh/sshd_config
PermitRootLogin yes
to
PermitRootLogin no


Add firewall rules :
sudo iptables -I INPUT 1 -i lo -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 9122 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
sudo  iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -I INPUT -p udp  --dport 111 -j REJECT
sudo iptables -A INPUT -j REJECT

well be updated later .

Wednesday, February 28, 2018

VirtualBox on Windows 8 VM network settings

On Windows 8 , for VirtualBox virtual machine to communicate with host computer and the world you need to adjust the network parameters. as in the image below





and be sure that interface file in the unix guest set to dhcp .