lundi 6 février 2017

automated logout function using laravel 5.1

am a fresher developer , actually my task is to set a automatic expiry account in php laravel 5.1, i tried it with middleware but it downs the sever speed, ok give me some ideas, actuallly wat iam searching is, if i registering into a site, at that time am inserting a current time in db, after creating user log, am triying to logging , so once i regstred as a new user, that account login access will be deactivated within 24 hours , by comparing the time which you inserted time while regstr.????????



via Chebli Mohamed

samedi 4 février 2017

Laravel occasionally failing to read cache

I am using laravel caching (the remember() method) on a website with a code like this:

$postedItems = Cache::remember('home_posted_items', $this->cacheTimes['postedItems'], function() {

    /* the stuff that prepares data */

    return ['items' => $items, 'firstItemNumber' => $firstItem];
});

The problem is that sometimes (every few days, I'd say) cached file seems to become corrupted and as a result I have downtime until the cache expires (unless I clear it manually).

Here is a part of the error stack that might be relevant:

[2017-02-04 22:01:34] production.ERROR: ErrorException: unserialize(): Error at offset 131059 of 131062 bytes in /home/path/to/app/vendor/laravel/framework/src/Illuminate/Cache/FileStore.php:78
Stack trace:
#0 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'unserialize(): ...', '/home/path/to/...', 78, Array)
#1 /home/path/to/app/vendor/laravel/framework/src/Illuminate/Cache/FileStore.php(78): unserialize('a:2:{s:7:"item...')
#2 /home/path/to/app/vendor/laravel/framework/src/Illuminate/Cache/FileStore.php(47): Illuminate\Cache\FileStore->getPayload('home_posted_ite...')
#3 /home/path/to/app/vendor/laravel/framework/src/Illuminate/Cache/Repository.php(98): Illuminate\Cache\FileStore->get('home_posted_ite...')
#4 /home/path/to/app/vendor/laravel/framework/src/Illuminate/Cache/Repository.php(202): Illuminate\Cache\Repository->get('home_posted_ite...')
#5 [internal function]: Illuminate\Cache\Repository->remember('home_posted_ite...', 1, Object(Closure))
#6 /home/path/to/app/vendor/laravel/framework/src/Illuminate/Cache/CacheManager.php(318): call_user_func_array(Array, Array)
#7 /home/path/to/app/bootstrap/cache/compiled.php(6089): Illuminate\Cache\CacheManager->__call('remember', Array)
#8 /home/path/to/app/app/Http/Controllers/HomeController.php(197): Illuminate\Support\Facades\Facade::__callStatic('remember', Array)

How to solve this problem?

From experience I know that clearing the cache solves the problem. So it seems that the issue is some corruption in files. I think if I could notice "the file is unreadable" and just clear the cache (Cache::forget(...)), it should solve the problem.

What would be the best way to notice such error? It seems that all the logic of retrieving the file is hidden inside the remember() method. Should I just unwrap it and use other methods, something like the following?

if (!($postedItems = @Cache::get('home_posted_items'))
{
    // prepare data

    $postedItems = ['items' => $items, 'firstItemNumber' => $firstItem];

    Cache::put('home_posted_items', $postedItems, $this->cacheTimes['postedItems']);
}



via Chebli Mohamed

Laravel 5.1 - Eloquent parameter bindings being applied in wrong order?

I have the following update query which consists of a sub-query join (I had to use DB:Raw as I was unable to find any documentation to accomplish via any other method - unless someone can show me some other way to achieve it).

However, because the table/model being updated uses timestamps, a value for updated_at is also being added and messes up the order of the parameter bindings?

OrderItemProduct::from('order_item_product as t1')
        ->join(DB::Raw('(
           SELECT t1.product_id as product_id
                , MIN(t1.created_at) as created_at
                , MIN(t1.order_item_id) as order_item_id
             FROM order_item_product t1
      INNER JOIN order_items t2 on t1.order_item_id = t2.id
      INNER JOIN orders t3 on t2.order_id = t3.id
           WHERE t1.product_id = ?
             AND t1.available > 0
             AND TIMESTAMPADD(DAY,t1.valid_days,t1.created_at) > now()
             AND t3.user_id = ?
        GROUP BY t1.product_id) AS t2'), function($join)
        {
                $join->on('t1.order_item_id', '=', 't2.order_item_id');
                $join->on('t1.created_at', '=', 't2.created_at');

         })
        ->setBindings([$productId, $userId])
        ->decrement('t1.available');

Below is what I end up with when I die n dump the query log:

array:1 [▼
   0 => array:3 [▼
     "query" => """
       update `order_item_product` as `t1` inner join (\r\n
                 SELECT t1.product_id as product_id\r\n
  \t                  , MIN(t1.created_at) as created_at\r\n
  \t\t                , MIN(t1.order_item_id) as order_item_id\r\n
  \t\t             FROM order_item_product t1\r\n
             INNER JOIN order_items t2 on t1.order_item_id = t2.id\r\n
             INNER JOIN orders t3 on t2.order_id = t3.id\r\n
                  WHERE t1.product_id = ?\r\n
                    AND t1.available > 0\r\n
                    AND TIMESTAMPADD(DAY,t1.valid_days,t1.created_at) > now()\r\n
  \t                AND t3.user_id = ?\r\n
               GROUP BY t1.product_id) AS t2 ON `t1`.`order_item_id` = `t2`.`order_item_id` 
                   AND `t1`.`created_at` = `t2`.`created_at` 
                   SET `t1`.`available` = `t1`.`available` - 1, `updated_at` = ?
       """
     "bindings" => array:3 [▼
         0 => "2017-02-04 09:41:22"
         1 => 1
         2 => 1
     ]
     "time" => 2.9
   ]
]

As you can see inside the bindings array a value for updated_at is automatically added by eloquent at index 0 and so it's ending up being bound to the first parameter placeholder inside the query which is incorrect.

How can I fix this?



via Chebli Mohamed

QueryException in Connection.php line 662: : Integrity constraint violation: 1048 Column 'content' cannot be null

I use laravel 5.1 with this how can I do?

  1. I use laravel 5.1 with this how can I do? I use laravel 5.1 with this how can I do?

    strong textin Connection.php line 662 at Connection->runQueryCallback('insert into statuses (content, user_id, updated_at, created_at) values (?, ?, ?, ?)', array(null, '1', '2017-02-04 14:36:46', '2017-02-04 14:36:46'), object(Closure)) in Connection.php line 618 at Connection->run('insert into statuses (content, user_id, updated_at, created_at) values (?, ?, ?, ?)', array(null, '1', '2017-02-04 14:36:46', '2017-02-04 14:36:46'), object(Closure)) in Connection.php line 391 at Connection->statement('insert into statuses (content, user_id, updated_at, created_at) values (?, ?, ?, ?)', array(null, '1', '2017-02-04 14:36:46', '2017-02-04 14:36:46')) in Connection.php line 347 at Connection->insert('insert into statuses (content, user_id, updated_at, created_at) values (?, ?, ?, ?)', array(null, '1', '2017-02-04 14:36:46', '2017-02-04 14:36:46')) in Processor.php line 32 at Processor->processInsertGetId(object(Builder), 'insert into statuses (content, user_id, updated_at, created_at) values (?, ?, ?, ?)', array(null, '1', '2017-02-04 14:36:46', '2017-02-04 14:36:46'), 'id') in Builder.php line 1843 at Builder->insertGetId(array(null, '1', '2017-02-04 14:36:46', '2017-02-04 14:36:46'), 'id') at call_user_func_array(array(object(Builder), 'insertGetId'), array(array('content' => null, 'user_id' => '1', 'updated_at' => '2017-02-04 14:36:46', 'created_at' => '2017-02-04 14:36:46'), 'id')) in Builder.php line 1015 at Builder->__call('insertGetId', array(array('content' => null, 'user_id' => '1', 'updated_at' => '2017-02-04 14:36:46', 'created_at' => '2017-02-04 14:36:46'), 'id')) in Model.php line 1592 at Model->insertAndSetId(object(Builder), array('content' => null, 'user_id' => '1', 'updated_at' => '2017-02-04 14:36:46', 'created_at' => '2017-02-04 14:36:46')) in Model.php line 1561 at Model->performInsert(object(Builder), array()) in Model.php line 1452 at Model->save() in HasOneOrMany.php line 317 at HasOneOrMany->create(array('content' => null)) in StatusesController.php line 34 at StatusesController->store(object(Request)) at call_user_func_array(array(object(StatusesController), 'store'), array(object(Request))) in Controller.php line 256 at Controller->callAction('store', array(object(Request))) in ControllerDispatcher.php line 164 at ControllerDispatcher->call(object(StatusesController), object(Route), 'store') in ControllerDispatcher.php line 112 at ControllerDispatcher->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in Authenticate.php line 45 at Authenticate->handle(object(Request), object(Closure)) at call_user_func_array(array(object(Authenticate), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102 at Pipeline->then(object(Closure)) in ControllerDispatcher.php line 114 at ControllerDispatcher->callWithinStack(object(StatusesController), object(Route), object(Request), 'store') in ControllerDispatcher.php line 67 at ControllerDispatcher->dispatch(object(Route), object(Request), 'App\Http\Controllers\StatusesController', 'store') in Route.php line 203 at Route->runWithCustomDispatcher(object(Request)) in Route.php line 134 at Route->run(object(Request)) in Router.php line 708 at Router->Illuminate\Routing{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102 at Pipeline->then(object(Closure)) in Router.php line 710 at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 673 at Router->dispatchToRoute(object(Request)) in Router.php line 635 at Router->dispatch(object(Request)) in Kernel.php line 236 at Kernel->Illuminate\Foundation\Http{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 139 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in VerifyCsrfToken.php line 50 at VerifyCsrfToken->handle(object(Request), object(Closure)) at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in ShareErrorsFromSession.php line 49 at ShareErrorsFromSession->handle(object(Request), object(Closure)) at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in StartSession.php line 62 at StartSession->handle(object(Request), object(Closure)) at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37 at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in EncryptCookies.php line 59 at EncryptCookies->handle(object(Request), object(Closure)) at call_user_func_array(array(object(EncryptCookies), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) in CheckForMaintenanceMode.php line 44 at CheckForMaintenanceMode->handle(object(Request), object(Closure)) at call_user_func_array(array(object(CheckForMaintenanceMode), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 124 at Pipeline->Illuminate\Pipeline{closure}(object(Request)) at call_user_func(object(Closure), object(Request)) in Pipeline.php line 102 at Pipeline->then(object(Closure)) in Kernel.php line 122 at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 87 at Kernel->handle(object(Request)) in index.php line 53 at require_once('/var/www/sample/public/index.php') in server.php line 21

this is the end!



via Chebli Mohamed

vendredi 3 février 2017

Leverage browser caching for Laravel 5.1 application

Recently I was checking my web application loading speed (which is build using Laravel 5.1). And I came across Leverage browser caching. So I googled it to find a solution to this and I found a solution but it doesn't seem to work.

It was mentioned to add the following code:

<IfModule mod_expires.c>
  ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
  ExpiresDefault "access plus 1 month"

# cache.appcache needs re-requests in FF 3.6 (thanks Remy ~Introducing HTML5)
  ExpiresByType text/cache-manifest "access plus 0 seconds"

# Your document html
  ExpiresByType text/html "access plus 0 seconds"

# Data
  ExpiresByType text/xml "access plus 0 seconds"
  ExpiresByType application/xml "access plus 0 seconds"
  ExpiresByType application/json "access plus 0 seconds"

# Feed
  ExpiresByType application/rss+xml "access plus 1 hour"
  ExpiresByType application/atom+xml "access plus 1 hour"

# Favicon (cannot be renamed)
  ExpiresByType image/x-icon "access plus 1 week"

# Media: images, video, audio
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/jpg "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType video/ogg "access plus 1 month"
  ExpiresByType audio/ogg "access plus 1 month"
  ExpiresByType video/mp4 "access plus 1 month"
  ExpiresByType video/webm "access plus 1 month"

# HTC files (css3pie)
  ExpiresByType text/x-component "access plus 1 month"

# Webfonts
  ExpiresByType application/x-font-ttf "access plus 1 month"
  ExpiresByType font/opentype "access plus 1 month"
  ExpiresByType application/x-font-woff "access plus 1 month"
  ExpiresByType image/svg+xml "access plus 1 month"
  ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

# CSS and JavaScript
  ExpiresByType text/css "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"

  <IfModule mod_headers.c>
    Header append Cache-Control "public"
  </IfModule>

</IfModule>

in .htaccess inside the public directory. But I still get recommendation for Leverage browser caching. Please help me with this. I am unable to find a solution to this.

Sites where I checked for page loading speed are:

  1. Google page speed
  2. gtmetrix.com

Thank you



via Chebli Mohamed

Laravel: Change format of Carbon dates in models when converted to JSON

Currently, when I convert a model to JSON, all Carbon date fields are casted like so:

"end_time": {
    "date": "2017-02-03 23:59:00.000000",
    "timezone_type": 3,
    "timezone": "Europe/London"
}

I want it casted using the Atom notation. This can be done in carbon like so:

$order->end_time->toAtomString()

where $date is a Carbon date.

How can I make a model convert dates in the atom format when converting it to JSON?

I am aware that it is possible to append data like so: http://ift.tt/2kq3Q5e

But this does not change the format of an existing value?



via Chebli Mohamed

jeudi 2 février 2017

Memcached - "No Memcached servers added" after upgrading to ubuntu 16.04

Does upgrading from ubuntu 14.04 to 16.04 affect multi-server memcached?

I have a Laravel 5.1 application which utilizes memcached to manage sessions across multiple servers, specifically two web servers and a database server.

After upgrading the OS on each server from ubuntu 14.04 to 16.04 and installing the latest version of mariadb (10.1.*), our laravel application now returns the following error:

[RuntimeException] No Memcached servers added.

My config files have not changed and neither have the ip addresses of any of the servers. Debugging laravel's MemcachedConnector.phpshows the server details being passed to the Memcached class but the output of Memcached::getVersion() returns false.

I am at a loss as to why this has suddenly stopped working, any assitance would be appreciated.



via Chebli Mohamed