jeudi 1 septembre 2016

Laravel 5.2 slow database inserts

I have a Amazon RDS db.t2.large database and connect to it via laravel's forge on digital ocean. I run laravel 5.2.

I have made an artisan command which is filling database settings at every release. Forge timed out after 2 minutes. To check if it was Forge that was slow, i used putty to login in the server and manually rolled out a release. That showed me that it are the manual created artisan commands.

I have an array of +/- 200 elements that i want to insert in a database.

$arrayvalues[] = ['group' => 'groupname', 'key' => 'key_in_group', 'translation' => 'translation_of_key'];
$arrayvalues[] = ['group' => 'groupname', 'key' => 'key_in_group', 'translation' => 'translation_of_key'];

foreach($arrayvalues as $key => $value){

    $findkey = MyModel::where('group', $value['group'])->where('key', $value['key'])->first();

    if(isset($findkey->id) AND $findkey->id > 0){
        //found, insert new translation

        LanguageSystemTranslation::firstOrCreate(['language_id' => 2, 'language_system_key_id' => $findkey->id, 'value' => $value['translation'], 'user_id' => 1]);

    }
    else{
        //ignore

    }


}

There are indexes on group and key. As wel as on language_id and language_system_key_id.

The jobs are defined as "helper" in the " App > Helpers" folder.

The job alone takes 200 seconds. On my local computer it takes 1-2 seconds.

Any idea what the issue can be?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2bMwrMK
via IFTTT

Issue while doing Unit Testing for Code Performance

I was testing my code to check how it will behave if we think that 100 users submitted their registration all at once !!!

My code is in PHP Laravel 5.2 and JQuery Ajax is below.

for (i = 0; i < 100; i++) {
    var 
     data={'Role' : "Role"+i},
     request = $.ajax({
        url:                'http://localhost:1234/Practise/public/api/SaveRoleApi',
        type:               "POST",
        data:               JSON.stringify(data),
        contentType:        "application/json; charset=utf-8",
        async:  true,
        success: function(d){ console.log(d); }
     });
}

Out of 100, I am not successful in submitting more then 88 records.

I am using MySQL Database.

Am I doing something incorrect?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ciIQMC
via IFTTT

Ajax Request not accessing Controller, Laravel

I have a simple post request that is currently working if I dont specify a controller in my route file. When I change the route to access a controller I have specifically created for AJAX requests it returns a 500 Internal server error.

My Route.php looks like this:

Route::post('ajaxTagStore', 'AjaxController@postAjaxTag');

My Controller looks like this:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

class AjaxController extends Controller
{
public function postAjaxTag(Request $request) {
  if(Request::ajax()){
    return Response::json(Request::all());
  }
}

}

and lastly my AJAX code looks like this:

$.ajaxSetup({
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
});

$(document).ready(function(){
  $('#tag-btn').click(function(){
    var fname = $('#firstname').val();
    var lname = $('#lastname').val();
    var token = $('#token').val();

    var dataString = $('#tag').serialize();

    $.ajax({
      type: 'POST',
      url: '/ajaxTagStore',
      data: dataString,
      success: function(response){ // What to do if we succeed
        console.log(response);
        $('#postRequestData').html(response);
      }
    })

  });
});

As I said it works if I don't specify a controller and just use this:

Route::post('ajaxTagStore', function(){
 if(Request::ajax()){
  return Response::json(Request::all());
 }
});

Any suggestions as to why this is? (I am using a csrf-token also)



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2c84uBg
via IFTTT

Laravel Global Scope with Dynamic Parameter

We're having issues using a global scope with a dynamic query parameter. The global scope is based on the manager ID, but $model is empty, and $this refers to the manager scope not the model so $this->id is an undefined property. Is there a way to do something like this:

public function apply(Builder $builder, Model $model)
{
    return $builder->where('manager', $model->id); // $this->id
}

I'm assuming that $model is supposed to be the manager model, but since it is empty and I can't find any documentation on it I'm not entirely sure. This is our global scope method in the Manager model:

protected static function boot()
{
    parent::boot();

    static::addGlobalScope(new ManagerScope);
}



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2c4jhyv
via IFTTT

Laravel: middleware to determine which controller to use

I am building a site which loads all pages and content via javascript while also manipulating the browser address bar (giving the illusion of a normal, navigable site with each page at its own URL). As a fallback, and for the benefit of search engines, the pages must also be able to be loaded normally at their respective URLs.

To do this, I need to let Laravel know if the page data is being requested via an ajax call or normal HTTP request. This- I presume- would be a situation where I would use Middleware. I want to be able to process the pages using two different controllers; one for ajax, one for HTTP.

ie:

if (Request::ajax()){
    forward request to ajax page controller
}else {
    forward request to standard page controller
}

Is this possible to handle with middleware? All examples I can find seem to assume that the controller is already a given.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2bY9JUa
via IFTTT

Displaying posts of users the user follows through Laravel relationships

I would like to display the posts of everyone the current user follows, ordered by date desc.

I have a many to many relationship supplying all the people the user is following.

$users = User::find(Auth::user()->id)->follow()->get();

I have a one to many relationship displaying the posts for any user.

$updates = App\User::find(?????)->updates()->orderBy('created_at', 'desc')->get();

The question mark's shows where the followers ID's need to be placed.

I can put the above query inside the for each loop but that obviously works its way through each follower rather than all posts in date order.

I suspect I may need to set a new relationship and work from the beginning. Can anyone advise.

Thank you.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2c7ImXx
via IFTTT

Running multiple Laravel queue workers using Supervisor

I using Laravel queues using a database driver and supervisor to keep a queue worker running all the time:

[program:laravel_queue]
command=php artisan queue:listen --timeout=1800 --tries=5
directory=/var/app/current
stdout_logfile=/var/app/support/logs/laravel-queue.log
logfile_maxbytes=0
logfile_backups=0
redirect_stderr=true
autostart=true
autorestart=true
startretries=86400
EOB

Some of the queue tasks can take around 10 minutes to complete.

I have 2 parts to the question:

1) How can i edit the above script to run multiple (e.g. 3) queue workers on the same queue.

2) Is there a way of scaling the number of queue workers running based on the number of jobs waiting to be processed?

The reason for question 2 is that we have batches of busy times and then lots of quiet times, so i don't really want to be wasting resources with 3 listeners running the whole time.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2bLCWzh
via IFTTT