mardi 18 avril 2017

Ranking retrieved data using loop in laravel

I have a collection of data $all_scholar_grade that hold values of grades.These grades in the second collection are not arrange from the highest order.This cant be done in ascending order because it is not a query.How do i arrange it or rank from the highest to lowest using loop or foreach? I'd tried figuring it out but i cant get the logic.Hope anyone could help me here.

    $all_scholar_id = array();
    $all_scholar_grade = array();
    $count = 0;
    foreach ($card_id as $id) {

        $card = ScholarCard::find($id);
        if ($card->scholar_grade_level == 12) {
            if ($card->scholar_GPA == 100 || $card->scholar_GPA == 99 || $card->scholar_GPA == 98 || $card->scholar_GPA == 97 || $card->scholar_GPA == 96 || $card->scholar_GPA == 95 ) {
             $card_gpa = 1.0;
            }elseif ($card->scholar_GPA == 94) {
                $card_gpa = 1.1;
            }elseif ($card->scholar_GPA == 93) {
                $card_gpa = 1.2;
            }elseif ($card->scholar_GPA == 92) {
                $card_gpa = 1.3;
            }elseif ($card->scholar_GPA == 91) {
                $card_gpa = 1.4;
            }elseif ($card->scholar_GPA == 90) {
                $card_gpa = 1.5;
            }elseif ($card->scholar_GPA == 89) {
                $card_gpa = 1.6;
            }elseif ($card->scholar_GPA == 88) {
                $card_gpa = 1.7;
            }elseif ($card->scholar_GPA == 87) {
                $card_gpa = 1.8;
            }elseif ($card->scholar_GPA == 86) {
                $card_gpa = 1.9;
            }elseif ($card->scholar_GPA == 85) {
                $card_gpa = 2.0;
            }elseif ($card->scholar_GPA == 84) {
                $card_gpa = 2.1;
            }elseif ($card->scholar_GPA == 83) {
                $card_gpa = 2.2;
            }elseif ($card->scholar_GPA == 82) {
                $card_gpa = 2.3;
            }elseif ($card->scholar_GPA == 81) {
                $card_gpa = 2.4;
            }elseif ($card->scholar_GPA == 80) {
                $card_gpa = 2.5;
            }elseif ($card->scholar_GPA == 79) {
                $card_gpa = 2.6;
            }elseif ($card->scholar_GPA == 78) {
                $card_gpa = 2.7;
            }elseif ($card->scholar_GPA == 77) {
                $card_gpa = 2.8;
            }elseif ($card->scholar_GPA == 76) {
                $card_gpa = 2.9;
            }elseif ($card->scholar_GPA == 75) {
                $card_gpa = 3.0;
            }elseif ($card->scholar_GPA <= 74) {
                $card_gpa = 5.0;
            }
        }else{
            $card_gpa = $card->scholar_GPA;
        }
        if ($card_gpa >= $scholarship->scholarship_gpa_to || $card_gpa <= $scholarship->scholarship_gpa_from) {
            $all_scholar_grade[] = $card_gpa;

            $all_scholar_id[] = $card->scholar_id;
        }

        $count++;         
    }

Output of my dd($all_scholar_id, $all_scholar_grade)

array:3 [▼
  0 => 29
  1 => 34
  2 => 35
]

array:3 [▼
  0 => 1.5
  1 => 2.1
  2 => 1.3
]   



via Chebli Mohamed

Unknown column in database by eloquent query

I am creating a criteria for Señor high and tertiary. Im trying to retrieve data that meets the condition but my query resulted to an error.

Example:

If this child is grade 12 and his final grade point average is 85 then this should be converted to college grading system(see conditions).

See the error below.Anyone knows why im getting this error and why saying unknown column even if i dont have such column?

SQLSTATE[42S22]: Column not found: 1054 Unknown column '1.5' in 'where clause' (SQL: select * from scholar_cards where scholar_card_id = 93 and 1.5 between 1.2 and 1.9)

    $count = 0;
    foreach ($card_id as $id) {

        $card = ScholarCard::find($id);
        if ($card->scholar_grade_level == 12) {
            if ($card->scholar_GPA == 100 || $card->scholar_GPA == 99 || $card->scholar_GPA == 98 || $card->scholar_GPA == 97 || $card->scholar_GPA == 96 || $card->scholar_GPA == 95 ) {
             $card_gpa = 1.0;
            }elseif ($card->scholar_GPA == 94) {
                $card_gpa = 1.1;
            }elseif ($card->scholar_GPA == 93) {
                $card_gpa = 1.2;
            }elseif ($card->scholar_GPA == 92) {
                $card_gpa = 1.3;
            }elseif ($card->scholar_GPA == 91) {
                $card_gpa = 1.4;
            }elseif ($card->scholar_GPA == 90) {
                $card_gpa = 1.5;
            }elseif ($card->scholar_GPA == 89) {
                $card_gpa = 1.6;
            }elseif ($card->scholar_GPA == 88) {
                $card_gpa = 1.7;
            }elseif ($card->scholar_GPA == 87) {
                $card_gpa = 1.8;
            }elseif ($card->scholar_GPA == 86) {
                $card_gpa = 1.9;
            }elseif ($card->scholar_GPA == 85) {
                $card_gpa = 2.0;
            }elseif ($card->scholar_GPA == 84) {
                $card_gpa = 2.1;
            }elseif ($card->scholar_GPA == 83) {
                $card_gpa = 2.2;
            }elseif ($card->scholar_GPA == 82) {
                $card_gpa = 2.3;
            }elseif ($card->scholar_GPA == 81) {
                $card_gpa = 2.4;
            }elseif ($card->scholar_GPA == 80) {
                $card_gpa = 2.5;
            }elseif ($card->scholar_GPA == 79) {
                $card_gpa = 2.6;
            }elseif ($card->scholar_GPA == 78) {
                $card_gpa = 2.7;
            }elseif ($card->scholar_GPA == 77) {
                $card_gpa = 2.8;
            }elseif ($card->scholar_GPA == 76) {
                $card_gpa = 2.9;
            }elseif ($card->scholar_GPA == 75) {
                $card_gpa = 3.0;
            }else {
                $card_gpa = 5.0;
            }
        }else{
            $card_gpa = $card->scholar_GPA;
        }
        $scholar_id2s = ScholarCard::where('scholar_card_id','=',$card->scholar_card_id)->whereBetween($card_gpa,[$scholarship->scholarship_gpa_to, $scholarship->scholarship_gpa_from])->get();

        $count++;         
    }



via Chebli Mohamed

dimanche 16 avril 2017

Cannot pass blade variable in onChange function in select

I have a select option as below.

 @foreach($destinations as $destination)
{!! Form::select('destination', $counts, $destination->ordering, array('onchange'=>'fetch_select(this.value,$destination->id)') !!}
@endforeach

I am trying to pass a variable called $destination->id in onchange function as above which will trigger a function in java-script later on. However, I am not able to pass this variable like i have done above. Throws a lot of errors. It says

Uncaught SyntaxError: Unexpected token >

in the inspect console.

I tried with:

 'onchange'=>'fetch_select(this.value,destination->id)'
'onchange'=>'fetch_select(this.value,"$destination->id")'
'onchange'=>'fetch_select(this.value,(destination->id))'
'onchange'=>'fetch_select(this.value,($destination->id))'

None of them works .. How can i get around this. With Regards



via Chebli Mohamed

mercredi 12 avril 2017

PHP - URL gets malformed during redirect

So, I have an image link that has this href:

http://ift.tt/2nFL0w1

This is processed like so (I use laravel):

function out (Request $request) {

    $url = $request->target;

    $qs = $request->except('target');
    if ( !empty($qs) ) {
        $url .= strpos($url, '?') !== false ? '&' : '?';
        $url .= http_build_query($qs);
    }

    return redirect($url);
}

Most of the time, this works. However, lately, we've been experiencing an issue where param1 and param2 are attached to the URL in a seemingly infinite loop causing us to hit a 414 Request URI too long Error.

The problem is that it happens so randomly that I really don't know where to check because I added a checker before the return statement.

if ( substr_count($url, 'param1') > 1 ) {
    $file = storage_path() . '/logs/logger.log';
    $log = "[ " . date("d-m-Y H:i:sa") . " ] [ {$request->ip()} ] - {$url} \n";
    file_put_contents($file, $log, FILE_APPEND);
}

And it hasn't logged a single hit. Even after our testers experienced the bug.

Is it possible that the receiving application is breaking the URL somehow? What information should I be looking out for? Have you seen an issue like this before?

Is it the http_build_query that could be causing this and that my checker just doesn't work as expected (though, I did test it and it logged my test URL).

Any help on the matter would be great.



via Chebli Mohamed

mardi 11 avril 2017

Modeling and Seeding Difference in Laravel

I am confused what's the difference of seeding and by using model in laravel which both saving data into the database. Why not just use Model? or Why not Seed? When and why they used?



Answers will be appreciated.



via Chebli Mohamed

lundi 10 avril 2017

Laravel 5.1 get Orders that have been completed

I have an Order model and an OrderStatus model. An Order can have multiple statuses but only one 'currentStatus' (this is defined by the created_at timestamp on the pivot table order_status).

Order model (orders table)

OrderStatus model (order_statuses table)

order_status pivot table (order_status table)

How can I define a query scope (or similar) to get all the Orders of a certain status; i.e. 'completed'?

The following doesn't work as it checks all statuses of the orders, even ones in the past.

public function scopeCompleted($query)
{
    return $query->whereHas('statuses', function ($q) {
        $q->where("order_statuses.status", 'complete');
    });
}



via Chebli Mohamed

vendredi 7 avril 2017

User creation does not work on laravel 5.1

actually i try to migrate some users from a excel file (.xlsx) to login in my aplication. But the code doesn't work. The strange part is that the same code it's implemented for migrate payroll from a excel file (.xlsx) and this one work. I try many options, change some code, but it doesn't work. Maybe I'm letting some error out and someone else can see it. I would appreciate the help.

My ImportController.php from users

public function cargar_datos_users(Request $request)
    {
        $file= $request->file('file');
        $original_name=$file->getClientOriginalName();
        $r1=Storage::disk('files')->put($original_name,  \File::get($file) );
        $route=  storage_path('files') ."/". $original_name;
        if($r1)
        {
            Excel::selectSheetsByIndex(0)->load($route, function($sheet)
            {
                $sheet->each(function($row)
            {
                $user = new User;
                $user->full_name = $row->nombreempleado;
                $user->user = $row->empleado;
                $user->password = bcrypt($row->empleado);
                $perfil = 1;
                $user->perfil = $perfil;
                $user->save();
            });
            return view("administrator.contracts.adminpayrolls")->with("msj","Usuarios Cargados Correctamente");             
        }
        else
        {
            return view("administrator.contracts.adminpayrolls")->with("msj","Error al subir el archivo");
        }
    }

My ImportController.php from payroll

public function cargar_datos_payrolls(Request $request)
    {
        $file= $request->file('file');
        $original_name=$file->getClientOriginalName();
        $r1=Storage::disk('files')->put($original_name,  \File::get($file) );
        $route=  storage_path('files') ."/". $original_name;
        if($r1)
        {
            Excel::selectSheetsByIndex(0)->load($route, function($sheet)
            {
                $sheet->each(function($row)
                {
                    $payroll = new Payroll;                                             
                    $payroll->lapso = $row->lapso;
                    $payroll->concept_payroll = $row->conceptonomina;
                    $payroll->payroll_concept_name = $row->nombreconceptonomina;
                    $payroll->id_employee = $row->empleado;
                    $payroll->hours = $row->horas;
                    $payroll->value = $row->valor;
                    $payroll->date_generation = $row->fechageneracion;
                    $payroll->date_from = $row->fechadesde;
                    $payroll->date_to = $row->fechahasta;
                    $payroll->balance_fee = $row->saldocuota;
                    $estado = 1;
                    $payroll->estado = $estado;
                    $payroll->save();
                });
            });
            return view("administrator.contracts.adminpayrolls")->with("msj","Usuarios Cargados Correctamente");             
        }
        else
        {
            return view("administrator.contracts.adminpayrolls")->with("msj","Error al subir el archivo");
        }
    }

My app/User.php

class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

    protected $table = 'users';

    protected $fillable = ['full_name', 'user', 'password','perfil'];

    protected $hidden = ['password', 'remember_token'];
}

The plugin for migrate from excel is http://ift.tt/1n2QXpH As I said, the same structure work on payroll migrate, but don't work on users migrate, any comments will be appreciate.



via Chebli Mohamed