dimanche 12 août 2018

Optimize laravel project with extracting methods

I am working on a ongoing laravel project which uses laravel 5.1. There is a process that takes over 8 seconds. within that process, there is a method that has almost 250 lines of code.In optimization perspective, if I extracted that code into several methods, will it be faster than usual or will it take much time?



via Chebli Mohamed

vendredi 10 août 2018

App.css in laravel moves to previous state

Whenever I change something in app.css and then in app.scss and run non run development, all my changes in app.css file vanished and previous state of the file appeared. How to save the changes in app.css in laravel



via Chebli Mohamed

Ionic formData append showing null in server

I am trying to upload an image using formData. The api is working fine. But the data is displaying null in the server. My function is

  capture_dl_front(){
    this.camera.getPicture(this.cameraOptions)
      .then(imageData => {
        this.customer.dl_front = normalizeURL(imageData);
        this.upload_dl_front(imageData);
      }, error => {
        this.func.showAlert('Error',JSON.stringify(error));
      });
  }
  upload_dl_front(imageFileUri: any): void {
    this.file.resolveLocalFilesystemUrl(imageFileUri)
      .then(entry => (<FileEntry>entry).file(file => this.readFile_dl_front(file)))
      .catch(err => console.log('Error',JSON.stringify(err)));
  }
  private readFile_dl_front(file: any) {
    const reader = new FileReader();
    reader.onloadend = () => {
      const imgBlob = new Blob([reader.result], { type: file.type });
      this.dl_front_imageUri = imgBlob;
      this.dl_front_imageName = file.name;
      alert(this.dl_front_imageName)
      const img = new FormData();
      img.append('image', this.dl_front_imageUri, this.dl_front_imageName)
      this.api.test(img).then(data=>alert("final: "+data))
    };
    reader.readAsArrayBuffer(file);
  }

and my api function is

 test(image){
    let headers = new HttpHeaders({
      'Content-Type': 'application/x-www-form-urlencoded',
    });
    return new Promise( resolve => {
      this.http.post(url, image, { headers: headers})
        .subscribe(
          data => {
            resolve(data['message']);
          },
          error => {
            resolve(error.statusText);
          }
        );
    });
  }

and i am getting the file in my laravel server as

$image = $request->file('image');

but i am getting null in the image parameter.

What am i doing wrong here?



via Chebli Mohamed

jeudi 9 août 2018

Add ID and Type from supplemental tables to parent table using SQL (MySQL & Laravel Polymorphic relations)

Note. Also posted on Database Administrators

I have one table that records all sales and fourteen supplemental tables which contain extra information about a sale. The fourteen supplemental tables are for all intents and purposes the same. They were created long ago when the initial developer thought there would be more differences but actually now that the project has matured they are more similar than they are different. They are different however, and as such I need to keep them separate.

Current structure

Sales table

| id      | customer_id | customer_ref                         | ... |
|---------|-------------|--------------------------------------|-----|
| 1237567 | 354         | a6143f8c-b679-47be-9bc0-52457842913c | ... |
| 1237568 | 867         | ref89b72                             | ... |
| ...     | ...         | ...                                  | ... |

Supplemental table 1 Class: App\SuppOne

| id   | customer_id | customer_ref                         | ... |
|------|-------------|--------------------------------------|-----|
| 2857 | 10372       | 2016-07-01-ab5d09cc37ca              | ... |
| 2858 | 354         | a6143f8c-b679-47be-9bc0-52457842913c | ... |
| ...  | ...         | ...                                  | ... |

Supplemental table 2 Class: App\SuppTwo

| id    | customer_id | customer_ref | ... |
|-------|-------------|--------------|-----|
| 90488 | 867         | ref89b72     | ... |
| 90489 | 1024        | 0000080992   | ... |
| ...   | ...         | ...          | ... |

There are no foreign keys on the tables to join the sales table to the supplemental tables but there is a 'customer_id' and 'customer_reference' which are unique to both the sales tables and also the supplemental tables but they are not consistent. This is what is currently used to join the two as-and-when I need to get more information about a given sale.

I'm using Laravel 5.1 and a MySQL database and I'd like to add two fields to the sales table; supplemental_id and supplemental_type in order to quickly and efficiently create a polymorphic relation.

Desired structure

Sales table

| id      | supplemental_id | supplemental_type | customer_id | customer_ref                         | ... |
|---------|-----------------|-------------------|-------------|--------------------------------------|-----|
| 1237567 | 2858            | App\SuppOne       | 354         | a6143f8c-b679-47be-9bc0-52457842913c | ... |
| 1237568 | 90488           | App\SuppTwo       | 867         | ref89b72                             | ... |
| ...     | ...             | ...               | ...         | ...                                  | ... |

I need to add these two fields to each of the sales records but I am unsure how to do this with raw SQL as I expect it would be much quicker than if done in a migration. I'd like to know how (if possible) in SQL, do I deal with the mapping from table_name to App\ClassName. There are about 1.5m records in the sales table and looping over them all will not take an insignificant amount of time.



via Chebli Mohamed

vendredi 3 août 2018

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'products.all_category' doesn't exist (SQL: select * from `all_countries`)

 'mysql' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => env('DB_DATABASE', 'forge'),
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],
    'mysql2' => [
        'driver' => 'mysql',
        'host' => env('DB_HOST_2', '127.0.0.1'),
        'port' => env('DB_PORT_2', '3306'),
        'database' => env('DB_DATABASE_2', 'forge'),
        'username' => env('DB_USERNAME_2', 'forge'),
        'password' => env('DB_PASSWORD_2', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'strict' => true,
        'engine' => null,
    ],

products is the default database and all_category is the table name in second database called category it is not even identifying the second database category ,it is only taking the first default one while configuring ,here is the query inside controller:-

$users = DB::connection('mysql2')->table("all_category")->get(); 

Any help would be much appreciate, Thanks in advance



via Chebli Mohamed

mercredi 1 août 2018

Laravel (1/1) NotFoundHttpException in RouteCollection.php line 179

I've encountered a problem submitting my form in laravel. My form structure looks this.

<form  class="form-group" action="" 
method="post" name="publish" enctype="multipart/form-data"  
onsubmit="return validateForm();">
   <input type="text" name="user">
   <textarea name="poem"></textarea>
   <input type="submit" value="save">
</form>

My web.php file has this route.

Route::post('/writepoem', ['uses'=>'PoemController@postCreatePoem','as'=>'writepoem']);

My PoemController.

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Poem;

class PoemController extends Controller
{ 
    public function postCreatePoem(Request $request)
    {   
        //validation
        $poem=new Poem();
        $poem->poem=$request['poem'];
        $poem->poem=$request['user'];



        //save poem
        $request->user()->poems()->save($poem);
        return redirect()->route('feed');
    }
}

On submitting my form I get this Exception.

(1/1) NotFoundHttpException

in RouteCollection.php line 179.

What could be the issue with routing?



via Chebli Mohamed