mardi 11 septembre 2018

how to control multiple ajax requests in laravel 5.2

I am working on an application where different ajax requests fires depending upon different actions.

For example, there is a chat window having send button. When i click on that button an empty message is sent with ajax, successfully. It work nice. But when I hit the send button too many times, at start some requests respond 200 (ok) but then it respond 500 (internal server error). Due to this the other requests that are going continuously like updateLastActivity also disturb. The preview of the error in developer's tool is: Whoops like something went wrong.

Note: When I make this chat system in core PHP, it work fine. There is no internal server error when I send too may requests.

Here is the code I am using

    //the following code is used to send the message
 $(document).on('click','.send_message_bt',function(event){
     event.preventDefault();

        var id=$(this).data('id');
        var name=$(this).data('name');
        var message=$("#message_field-"+id).val();



    $.ajax({
        //headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
         headers: { 'X-CSRF-TOKEN': {!! json_encode(csrf_token()) !!} },
        url:'',
        type:'POST',
        data:{
          id:id,
        message:message
           },
        success:function(data,status){
        //clear the message field value
        $("#message_field-"+id).val('');

        //update the chat history
        fetchChatHistory(id,name);


    },
        error:function(response){
           if(response.status==401){
                  alert('You are not logged in!');
                  window.location=window.location.href;
                   }

           }

});

});

here is the back end code

public function sendMessage(Request $request){
    $message=new Userchatmessage();
     $message->message=$request->message;
     $message->sender_id=Auth::user()->id;
     $message->receiver_id=$request->id;
     $message->save();
     return response('success');
}

How to fix this issue.



via Chebli Mohamed

lundi 10 septembre 2018

is Collection a Laravel instance or PHP instance?

is collection a laravel instance or PHP instance. I always hear people say a Eloquent return a laravel collection so I want clearance on this



via Chebli Mohamed

dimanche 9 septembre 2018

Laravel: Do one query update and then, if that succeeds, do the next, otherwise, do neither

So, I want to update two different user accounts with the same value.

Scenario: User 1 transfers money to User 2:

$transfer_from = $account->decrement('balance', $amount);

$transfer_to = Account::where('user_wallet_address', $receiver_wallet_address)->increment('balance', $amount);

But, what I want, is something like this:

$account->decrement('balance', $amount)->$transfer_to;

As in if the decrement succeeds, then update the other user's balance, otherwise, both should fail

Thoughts?



via Chebli Mohamed

Where Should I place an ID which I require in all my requests? Session or Cookie or any other option.

Where Should I place an ID which I require in all my requests? Session or Cookie or any other option. please help

I am calling an API once user logedIn and it gives me an ID now I require this Id in most of the other calls. what should I Do? where to put this ID?



via Chebli Mohamed

mercredi 5 septembre 2018

Return Download Docx File in Laravel 5

I'm trying to create a url that return word doc file when user visit.


I have tried

public function resume()
{

    $file= public_path(). "/docs/John's Resume.docx";
    $headers = array(
        'Content-Type: application/msword',
        'Content-Disposition:attachment; filename="cv.docx"',
        'Content-Transfer-Encoding:binary',
        'Content-Length:'.filesize($file),
    );

    ob_end_clean();
    return Response::download($file,"John's Resume.docx", $headers);

}

I kept getting

FileException in BinaryFileResponse.php line 97:
File must be readable.

enter image description here


Questions

How would one go about and debug this further ?


I'm open to any suggestions at this moment.

Any hints/suggestions / helps on this be will be much appreciated!



via Chebli Mohamed

Force response()->download() return HTTPS url

I have switched my Laravel 5.1 to HTTPS and everything seems fine, except the file download part.

The problem is response()->download() returns a HTTP link instead of HTTPS and I get a mixed content in Chrome, so the link is blocked.

And some code:

$headers = array
            (
                'Content-Type' => 'application/vnd.android.package-archive'
            );
return response()->download(config('custom.storage') . $apk->generated_filename, $apk->filename, $headers);



via Chebli Mohamed

lundi 3 septembre 2018

vsmoraes/laravel-pdf show() function outputting codes on page

When I try to output a .pdf file using vsmoraes/laravel-pdf as below

 $pdf
     ->load($html)
     ->filename($booking->ReservationNumber . ".pdf")
     ->show();

I am getting an output like below:

enter image description here

This happens only in production environment and it's working fine in my local development environment, what could be the issue here?



via Chebli Mohamed