mercredi 1 juillet 2020

Laravel not returning the correct error message to my AngularJS application

I have a controller in Laravel 5.8 that creates a campaign from all requested input. It runs through a try/catch routine and all going well, spits me out the returned database record.

try {
    
   
    // Create the new Campaign record
    $campaign = Campaign::create( $request->all() );
    
    
}
    
    
catch( exception $e ) {
    
    
    return response()->json( '(' . $e->getCode() . ')' . ' ' . $e->getMessage(), 400 );
    
    
}
    
    
return response()->json( $campaign, 200 );

However, I have come across a problem where Laravel is returning a 'generic' error to my AngularJS application.

When I attempt the $http request in my AngularJS frontend, I get the following error returned: (0) Array to string conversion (Bad Request - Error 400).

When I attempt the request through a 3rd party app (such as Google Advanced REST Client), I get a different error: (22007) SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2020-07-05T15:30:04.095Z' for column 'StartTime' at row 1

I have tried removing the try/catch and even tried removing the $e message, but I still get the same generic error returned to Angular.

Could someone please tell me why I cannot see the correct error here. I am trying to give reasonable feedback back to my users, but that generic error helps no one. I spent ages looking for an array conversion error in my code, but having the correct error to begin with would've saved me a lot of time debugging.



via Chebli Mohamed

Message: "CSRF token mismatch.", exception: "Symfony\Component\HttpKernel\Exception\HttpException",…}

I am A PHP developer and currently moving towards Laravel framework as per my task I have to complete the realtime table using ajax but I am sticking with an error which is CSRF token mismatch error please help me to resolve the error I am posting shortcode only

JAVA Script

<script>

 function getMessage() {
        $.ajax({
           //var data = {"_token": $('#token').val()},
           type:'POST',
           url:'/getMsg',
           headers: {'XSRF-TOKEN': $('meta[name="_token"]').attr('content')},
           success:function(data) {
              $("#msg").html(data.msg);
           }
           
        });
        
     }
    
  </script>

Route Path

Route::post('/getMsg','CustomerSearchController@doAjaxTest');

Controller code

public function doAjaxTest(){
    $msg = "<b>Message over ajax This test is Successful</b>.";
    return response()->json(array('msg'=> $msg), 200);
}

HTML CODE

<center>
 
 <input type = "hidden" name = "_token" value = '<?php echo csrf_token(); ?>'>
 <table>
 <tr>
 
 <td><label>Enter Place Name</label></td>
 <td><input type="text" class="form-control" id="placename" name="placename" placeholder="Name Of Place"/></td>
 </tr>
 <tr>
 <td>
 <input type="submit" value="Get Message" onclick="getMessage()" />
 </td>
 </tr>
 </table>

 <br>
 <!--
 <div class="panel panel-default table-responsive">
            <div id="dataTag"><b>All the Details according to department will be displayed</b></div>
            
      </div>
 -->
 <div id = 'msg'>This message will be replaced using Ajax. 
     Click the button to replace the message.</div>
  </center>

I don't know why it is showing me CSRF token mismatch when headers contain tokens once solved i can have some realtime action please help out



via Chebli Mohamed

SQLSTATE[HY000] [1045] Access denied for user 'lagaikha_cricketuser'@'lin1505.byte90.net' (using password: YES) Laravel 5.2 on Live Web Server

I am trying to login on my website which is built in laravel 5.2 but I am getting this error of access denied for user on my live web server. I have also put credentials in .env file that are shown below:

APP_URL=http://lagaikhaiexchange.com
    
    DB_CONNECTION=mysql
    DB_HOST=lagaikhaiexchange.com
    DB_PORT=3306
    DB_DATABASE=mydatabase
    DB_USERNAME=myuser  
    DB_PASSWORD=mypassword

and I also gave the all privileges to user lagaikha_cricketuser But I am unable to find that why my application is not connecting to database. Although On localhost, the same code working fine and doing everything. Any help would be appreciated... Thank you



via Chebli Mohamed

laravel - how can I call a function stored in a controller after saving to a database?

I'm following a tutorial on Laravel, adding to a DB via a form. At the end of a function that saves to a DB, it returns back to the page where the form is, but I want to be taken to another page where the information is displayed. In the tutorial I created a controller with a function that returns a view containing all the database info - that element works fine however I can't seem to find a way of calling this function directly after saving to the database. I can also return any other view which just displays static view ( just html with no data handling ). Is what I'm trying to achieve possible?

  public function store(){        

        $li = new \App\LTest1();
        $li->creator = request('creator');
        $li->title = request('title');
        $li->views = request('views');
        $li->save();
        return back(); // this works
       // return view('info');  // this works
       //return ('Listings@showList');   this doesnt work = how do i call a function in a controller???
    }
// routing

Route::get('info', function () {
    return view('info'); // i can get to this static page from my store() function
});

Route::get('thedataviewpage', 'Listings@showList'); // you can route to this but not from the store() function


via Chebli Mohamed

The process "mysql -u

I have an application in Laravel 5.3 and it has a bunch of integration tests. So when integration tests run, it creates a test database by running the migrations which are 268 in total. So when I run this particular test alone it works fine but when I run a batch of them together, this error pops up:

Symfony\Component\Process\Exception\ProcessTimedOutException: The process "mysql -u -p < /tmp/c2eecb1b530b0f6eb5062f7ab1c3dcdf" exceeded the timeout of 60 seconds.

I have tried to increase the timeout to 120 like this:

$process = new Process($command);
$process->setTimeout(120);
$process->run();

But the same error pops up. What could be the issue here? what do I need to get rid of this error?



via Chebli Mohamed

Getting 422 Unprocessable Entity while submitting Form with ajax in Laravel

I'm submitting my form using ajax in laravel

Output:

{"message":"The given data was invalid.","errors":{"name":["The name field is required."],"image":["The image field is required."]}}

View:

   

        <div class="row">
                                <div class="col-sm-6">
                                    <div class="form-group">
                                         <h2 class="card-inside-title">Name</h2>
                                        <div class="form-line focused">
                                    
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="row clearfix">
                                <div class="col-sm-6">
                                    <div class="form-group">
                                         <h2 class="card-inside-title">Image</h2>
                                        <div class="form-line focused">
                                        
                                        </div>
                                    </div>
                                </div>
                        </div>
    <button class="btn btn-info add_button btn-xs p-3 hide" type="button"><i class="material-icons">add</i> Add</button>
    </div>

   

Script:

$('#frmadd').submit(function(e) {
      e.preventDefault();
        var formData = new FormData($(this)[0]);
        $.ajaxSetup({
          headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
          }
        });
        $.ajax({    
          url: APP_URL+'/add',
          type: 'POST',
          data: formData,
          processData: false,
          contentType: false,
          success: function(response)
          {

          },
          error: function(data)
          {
     
          }
          });
});


via Chebli Mohamed

mardi 30 juin 2020

I want the follower key to be true too... in my mutual friend query

Here is the query. Basically i am passing two parameters user_id (logged in user) and friend_id so the query will fetch all friends imagine you are click on your own friend and seeing the friend on another screen some are mutaul friend.. in this query mutaul is fine.. but when i want to follow the person that is not followed by the firned_id i need that to be true when there is exists data against the user_id

here im checkign relationship between user_id=334 and friend_id=312 image i have friend_id have these friend 1,2,3 and user(logged in user) has 3, 4, 5 now 3 is mutal friend that is toally fine and my ismutaul=1

now there is a record against the friend 2 in followers table in database so friend 2 actually is the friend of 312 and 312 not following. so how can i make it true because 334 is already follwoing him i need the isfollower varaible to be true.

im stuck and i dont know what to do any help please?

SELECT 
    users.id AS user_id,
    users.username,
    CONCAT('/',
            IF(users.photo = '',
                'default_image.png',
                users.photo)) AS user_image,
    users.phone,
    (SELECT 
            COUNT(user)
        FROM
            meal_rating
        WHERE
            user = users.id AND comment <> '') AS checkin_count,
    (CASE
        WHEN
            uf.receiver = (SELECT 
                    receiver
                FROM
                    user_friends
                WHERE
                    sender = 312 AND receiver = uf.receiver
                        AND user_friends.receiver IN (SELECT 
                            receiver
                        FROM
                            user_friends
                        WHERE
                            sender = 334))
        THEN
            1
        ELSE 0
    END) AS isMututal,
    CASE
        WHEN
            uf.receiver = (SELECT 
                    receiver
                FROM
                    user_followers
                WHERE
                    sender = 312 AND receiver = uf.receiver)
        THEN
            1
        ELSE 0
    END AS isfollowers,
    users.*
FROM
    `users`
        INNER JOIN
    `user_friends` `uf` ON `users`.`id` = `uf`.`receiver`
WHERE
    `uf`.`sender` = 312 
UNION SELECT 
    users.id AS user_id,
    users.username,
    CONCAT('/',
            IF(users.photo = '',
                'default_image.png',
                users.photo)) AS user_image,
    users.phone,
    (SELECT 
            COUNT(user)
        FROM
            meal_rating
        WHERE
            user = users.id AND comment <> '') AS checkin_count,
    (CASE
        WHEN
            sender = (SELECT 
                    sender
                FROM
                    user_friends
                WHERE
                    receiver = 312 AND sender = uf.sender
                        AND sender IN (SELECT 
                            sender
                        FROM
                            user_friends
                        WHERE
                            receiver = 334))
        THEN
            1
        ELSE 0
    END) AS isMututal,
    CASE
        WHEN
            uf.sender = (SELECT 
                    sender
                FROM
                    user_followers
                WHERE
                    sender = 312
                        AND user_followers.receiver = uf.sender)
        THEN
            1
        ELSE 0
    END AS isfollowers,
    users.*
FROM
    `users`
        INNER JOIN
    `user_friends` uf ON `users`.`id` = `uf`.`sender`
WHERE
    `uf`.`receiver` = 312


via Chebli Mohamed