jeudi 13 septembre 2018

Upload Multiple Images in Laravel 5.1

I have an input file

<input id="images" name="images" type="file"  multiple>

I upload 3 files

enter image description here

My controller only receive 1.

$files = Request::file('images');
$file_count = count($files);

dd($file_count); // 1


dd($files);

UploadedFile {#28 ▼
  -test: false
  -originalName: " 2018-09-13 at 11.54.24 AM.png"
  -mimeType: "image/png"
  -size: 5849
  -error: 0
}

What did I miss ?



via Chebli Mohamed

Undefined variable: request - Laravel 5.1

I have include this on top of my file

use Request;

and also,

use Illuminate\Http\Request;


When I tried use

$files = $request->file('images');
dd($files);

I kept getting

ErrorException in PortfolioController.php line 113: Undefined variable: request

Why ?

According to the doc of 5.1

https://laravel.com/docs/5.1/requests

use Illuminate\Http\Request;

should be enough.


Controller

public function update($id) {

    $files = $request->file('images');

    ...

}



via Chebli Mohamed

mardi 11 septembre 2018

MethodNotAllowedHttpException in RouteCollection.php line 200:

I have these route declaration here

//Skill
Route::get('skill','SkillController@index');
Route::get('skill/create','SkillController@create');
Route::post('skill/store','SkillController@store');
Route::get('skill/{id}','SkillController@show');
Route::get('skill/{id}/edit', 'SkillController@edit');
Route::post('skill/{id}/update','SkillController@update');
Route::delete('skill/{id}/destroy','SkillController@destroy');

With these routes, I can delete fine on local.

When I tried to delete on production, I kept getting

enter image description here

I know for sure, I had this line

Route::delete('skill/{id}/destroy','SkillController@destroy');

  • Local and Prod have the same codebase.
  • Local = Mac OS X
  • Prod = Ubuntu Server

What did I missed ?



via Chebli Mohamed

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