mercredi 1 juillet 2020

When copying data to another table, parent_id issue in Laravel

I am using Laravel + MySQL.

I have following three tables:

templates table
id
name

template_pages table
id
template_id
parent_id
name
content


user_pages table
id
user_id
parent_id
name
content

When user picks one template, it will get pages from template_pages table.

Here, I want to create the new records to user_pages table.

I could get pages rows from template_pages table with template_id which user picked.

But when I insert them to user_pages table, parent_id wasn't match as I expected.

One user can get several same templates.

Please help me how I can solve this issue.

Thank you



via Chebli Mohamed

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

mardi 30 juin 2020

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