Friday, December 1, 2017

VirtualBox Cannot ping VM from host - Destination host unreachable.


Virtualbox  on windows host  after create a new virtual machine and choose the network to be bridged , I can reach the world and ping the host machine and the router , but when I try to ping the virtual machine from the hot it respond with "Destination host unreachable".
The solution is simple I was using static IP for the virtual machine , changing the ip on the virtual machine  to be static will solve this issue.

Sunday, November 12, 2017

array_key_exists(): The first argument should be either a string or an integer


array_key_exists(): The first argument should be either a string or an integer In Php when you get this error , that means you are dealing with an Array , and your code trying to get some keys out of this Array, but instead of passing a single parameter (string or an integer) to the function that do the job,you are passing something else.
Example :

$arr = [
'color' => 'Red',
'price' => '50'
]

Now, when you try to check if the color is red you are going to use the code :

if($arr['color'] === 'Red') echo "Red";

and that's ok and no error will be thrown, but the following code:

if($arr['color','price'] === 'Red') echo "Red";

will throw an Error Exception.as the error given above.

Thursday, October 26, 2017

Change all posts permalinks in wordpress when move site from server to server


great trick to change all posts permalinks in wordpress when move site from server to server
UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');

Reference

Saturday, October 14, 2017

jQgrid not inserting php returned json data into grid

After return json data from php file , the jQgrid not inserting the json data into the grid , jqgrid built the grid and assigned columns header names , but not the data .
debugging the http response  using firefox developer tools and found the json data syntax is right and no errors there. So what is the problem.

the problem here is that jQgrid requires extra elements in the response other than the real data , which are as follows:

  "page": 2,//current page
    "total": 2,//number of pages
    "records": 11,//# of records in total

So, you have to prepare the json data before sending back to the client to be in the following form:
  
You json should look something like
{
    "page": 2,//current page
    "total": 2,//number of pages
    "records": 11,//# of records in total
    "rows": [//array of data
        {
            "id": "101",//id for this row of data
                "cell": [
                "101",
                "Sundale",
                "OTTP",
                "652",
                "6",
                "65",
                "656665",
                "986346654",
                "823343454",
                "554332"
            ]
        }
    ]
}

Friday, October 13, 2017

Laravel - eloquent - Logging All queries into logfile

Sometimes you need to know the SQL interpretation of your eloquent query to check performance or even check the SQL query syntax is valid , you may try a laravel debugging package like Laravel-Debugbar, But whilst this package is great , it still cannot be good tool when it comes to SPA (single page application) which run tens of requests  , but this package only returns information about each request , which means the next request will write over the previous request debugging information.

So , in this case we need to log all request information , specially the SQL queries to a log file using the Example mentioned in Laravel documentation.

<?php

namespace App\Providers;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        DB::listen(function ($query) {
            // $query->sql
            // $query->bindings
            // $query->time
        });
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

Thursday, October 5, 2017

Laravel : Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, null given

This Error Happened when I tried to register a new user  using RegistersUsers  trait in :

App\Auth\RegisterController

Method : create

this method must return an instance of the created (registered) user from the mentioned method , which is by default return this instance as follow :

return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

look to the word return in the beginning .
if you manipulated the original code in this method and forgot to return an instance of the created user then Laravel will through this error..

Wednesday, October 4, 2017

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `` add constraint `_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade)

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `profiles` add constraint `profiles_user_id_foreign` foreign
 key (`user_id`) references `users` (`id`) on delete cascade).


Ok , this Error on laravel migration for a new table, when trying to add a foreigen key to profile table references User id  in users table,  what will solve this issue is to change the user_id type to match the data type of (id) in users table :

$table->integer('user_id')
to
$table->integer('user_id')->unsigned()->nullable();

Also, this error can happen, if the referenced table is not created yet. So, if the users table in this example is not exist in the database. then the same error (1215) will be triggered .