lundi 6 mars 2017

Upgraded Laravel 5.1 to 5.2, CSRF and Sessions not persisting on request

So I decided to upgrade from Laravel 5.1 to 5.2 and in the process my user authentication has broken. I use database sessions and I upgraded them as per the instructions from Laravel. (See "Sessions" section) and it is saving to the DB. However when I try and login it resets my CSRF and erases my session. In fact I have noticed that every time any request happens (even ajax) it will update with a new session. Attempting to login creates 5 sessions for me and does not log me in, any ideas?



via Chebli Mohamed

Add custom 500 error page only for production in Laravel

I want to have a custom 500 error page. This can be done simply by creating a view in errors/500.blade.php.

This is fine for production mode, I no longer get the default exception/ debug pages when in debug mode.

Therefore, my question is: how can I have a custom 500 error page for production, but the original 500 error page when debug mode is true?



via Chebli Mohamed

dimanche 5 mars 2017

two integer column merge and sortBy using laravel 5.1

in laravel 5 how to sortBy and merge two column i have column "price" and "price_sale" both are integer. i want to ascending or descending price and price_sale together return as collection. thanks you



via Chebli Mohamed

samedi 4 mars 2017

simplexml_load_file() error is showed some every X hours

I have an Ubuntu 16.04 server, there I installed PHP7+Nginx+MySQL. Also I have a web application developed in Laravel 5.1 in which I take some uploaded files (by the user, they content XML) and read them, I use xml_load_file() to do this, in my local environment everything work great! Then, in the server when I just cloned the project there I noticed than that function was not working, instead it returned this error:

simplexml_load_file(): I/O warning : failed to load external entity "/tmp/phpSFeOBP"

So, I researched and noticed that I had to enable allow_url_fopen in the php.ini file, so I did it and restarted the server using:

sudo systemctl restart php7.0-fpm

And then it started to work great, but, something strange it happening now, every some hours the server gives the error, and I have to be restarting the php7.0-fpm over and over again, and the php.ini file is the same, it's not modified, so I don't really know what is,what should I do or even why is happening.

To be more especific, this is the code that I have in my project to read the file (and the file exist,so it's not the problem):

ini_set('allow_url_fopen', 'on');//I set this to ensure,but nothin happens
$xml = simplexml_load_file($file);



via Chebli Mohamed

Generating a report in Laravel times out - make it more efficient

Lately I've been having to download the database and run this script locally in order to make it work. I could probably get into the server and allocate more memory but I'd rather improve the script if possible. There must be a more efficient way.

I have a table of invoices with orders which contain have items. I need to pull up all invoices in a certain date range that have orders which have items where tax was collected. The script below works, but takes forever and times out on the live server, only runs on my dev machine.

        $invoices = \App\Invoice::whereHas('orders', function($q)
        {
            $q->whereHas('items', function($q)
            {
                $q->where('tax_billed', '>', '0');
            });
        });             
        $invoices->whereBetween(session('report.datesearch'), [session('report.search_date_start'), session('report.search_date_end')]);
        $invoices = $invoices->get();

I thought maybe I can go the other way, and find all items where tax_billed > 0, but then how do I pare that down by the invoice date, which is stored two tables up the parental chain in the invoices table?

Relationships:

class Invoice extends Model {
    public function orders()
    {
        return $this->hasMany('App\Order', 'invoice_id') -> orderBy('created_at', 'asc');
    }
}

class Order extends Model {
    public function items() {
        return $this -> belongsToMany('App\Item', 'order_item', 'order_id', 'item_id')-> withPivot('id','variant_id','quantity','quantity_received','quantity_delivered','notes', 'createdby_user_id', 'weight', 'cost_billed', 'tax_billed', 'tax_rate', 'calc_by_weight', 'track_units')->orderBy('name');
    }
}

and the obvious reverse: ITEMS belongsTo ORDERS belongsTo INVOICES



via Chebli Mohamed

vendredi 3 mars 2017

Created_at or updated_at not displaying full dateTime

In my laravel app i need to compare 2 dateTime, the created_at and the updated_at, but i notice that when i call this properties of the record is not disaplayed the seconds, onlu hour and minutes, i need to get the seconds, what i need to change or include to show all info? I beend checking on the documentation and didnt found anything.

Ex:

$calculation->created_at

output:

2017-03-03 21:03

And in database is:

2017-03-03 21:03:22



via Chebli Mohamed

Response to preflight request doesn't pass access control check Laravel and Ajax call

I have a REST api made in Laravel 5.1 hosted in a remote server. Now, I', trying to consume that API from another website (that I have in local).

In Laravel I set the required lines to send the CORS headers. I also tested the API using Postman and everything seems to be ok!

In the Frontend

Then, in the website I sent the POST request using ajax, with this code:

var url="http://ift.tt/2mUN9Py";
var data=$("#my-form").serialize();
    $.ajax({
                    type: "POST",
                    url: url,
                    data: data,
                    headers: { 'token': 'someAPItoken that I need to send'},
                    success: function(data) {
                        console.log(data);
                    },
                    dataType: "json",
                }); 

Buy then I get this error in the console:

XMLHttpRequest cannot load http://ift.tt/2mUN9Py. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

In the Backend

In the API I set this (using a Laravel Middleware to set the headers):

return $next($request)
            ->header('Access-Control-Allow-Origin', '*')
            ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');

So, I'm confused about where is exactly the problem.

  1. In the server? but then why with Postman work fine?
  2. Is in the Ajax call? so, then what should I add?


via Chebli Mohamed