mercredi 6 mars 2019

How can i install composer.json and all the dependencies without creating a new project on shared hosting

I already have composer.phar installed at /home3/username/laravel folder but it is still requesting for composer.json and composer.lock which i do not have. How can i get these without running composer create-project laravel/laravel {directory} "5.0.*" --prefer-dist I already have an existing laravel project i just need the composer.json and all the dependencies



via Chebli Mohamed

Loop Multidimensional array in Controller

I need to loop in a mulitidemnsional array being consumed in an outbound API. After consuming the API in a controller, I need to insert the looped records in a Model.

So, the response recieved from the external API is the following:

enter image description here

So, what Im doing in my controller function is the following:

public function index()
{

    $client = new \GuzzleHttp\Client();
    $response = $client->request('GET', 'http://api', [
        'headers' => [
             'x-authtoken' => '0275d',
             'cache-control' => 'no-cache'],
             'decode_content' => false
    ]);

     //get body content
    $body = $response->getBody()->getContents();
    $data = json_decode($body, true);

    foreach ( $data['content']['Propiedades'] as $propiedades )
        {
            $id = Arr::get($propiedades, 'Id');
            $Moneda = Arr::get($propiedades, 'Precio.Moneda');
            $Precio = Arr::get($propiedades, 'Precio.Valor')
        }

}

The problem is that im looping just one instance of "propiedades" array.

enter image description here

1. How can I loop all "propiedades" array and retrieve key values from it?

2. How can access each "propiedades" array to the next nested array and bring back those nested values realted to the first array level? For example, my result must be for each property record:

Propiedades.Id

Propiedades.Precio.Moneda

Propiedades.Precio.Valor

3. Ones I get all the "propiedades" with their values, do I need to create an array to insert those records in a Model? How do I pass the data to the model? My Models will have the structure as the array recevied from the API with the corresponding child entities for "propiedades"


Thanks in advance! Regards



via Chebli Mohamed

Laravel 5 session disappearing after redirection

In laravel 5.7, session is getting disappeared after redirecting to another page. I am working on an application in which I am pushing users to the payment gateway page before which I am storing the data in a session as per laravel documentation. After coming back from the payment gateway when I try to retrieve that session it returns empty. Can anyone please tell me how can I resolve this issue.

My code is something like this

public function processPayment(Request $request)
 {
    //...........
    session()->put('order_checkout_data', [
        'gateway' => 'paypal',
        'data' => $paypalData
    ]);

    //$request->session()->save();  <!-- This i tried after reading some solution but didnt help

    //print_r(session('order_checkout_data')) <!-- I can see the session here

    $paypal = new PayPal();
    $response = $paypal->purchase($paypalData);

    if ($response->isRedirect()) {
        $response->redirect(); //This is where redireting to paypal
            }
}

public function handleGatewayResponse(Request $request){
    print_r(session('order_checkout_data')); //No data
}

I tried with session global function and facade as well , like these

Session::put('order_checkout_data', [
            'gateway' => 'paypal',
            'data' => $paypalData
        ])

and also

session(['order_checkout_data'=>[
            'gateway' => 'paypal',
            'data' => $paypalData
        ]])

But no value. My env settings likes this

SESSION_DRIVER=file
SESSION_LIFETIME=360

I tried to go through some of the links with a similar problem but that didn't help. Here are the links that I have followed :



via Chebli Mohamed

mardi 5 mars 2019

Laravel 5 : Ajax response download not working

Tried below code:

$headers = [
        'Cache-Control'       => 'must-revalidate, post-check=0, pre-check=0',
        'Content-type'        => 'text/csv',
        'Content-Disposition' => 'attachment; filename=list.csv',
        'Expires'             => '0',
        'Pragma'              => 'public'   
      ];

    $list = List::all()->toArray();

    # add headers for each column in the CSV download
    array_unshift($list, array_keys($list[0]));
    $FH = fopen(storage_path('list.csv'),'w');
    foreach ($list as $row) { 
        fputcsv($FH, $row);
    }
    fclose($FH);

    $path = storage_path('list.csv');

    return response()->download($path, 'list.csv', $headers);

Below will out put in inspect element network call

enter image description here

I have got the same issue in Stack overflow but not working for me. Exporting CSV response laravel 5.5 and download as csv file

Can have any idea regarding this what I have missing in here?



via Chebli Mohamed

lundi 4 mars 2019

Passing data to the intermediate table using sync() in Laravel 5.1

I've got the following, which works as expected:

$aircraft->logbook_entries()->sync($request['tasks']);

I want to add "work_order_id" to my pivot table, so I've done the following:

  • Added ->withPivot('work_order_id') to the Aircraft and Task models in the appropriate belongsToMany relationships
  • Updated my query to read $aircraft->logbook_entries()->sync([1 => ['work_order_id' => $workorder->id], $request['tasks']]);

This does not add the work_order_id to the pivot table as desired. Instead I get the error:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (pams_amo.airframe_logbook_entries, CONSTRAINT airframe_logbook_entries_work_order_id_foreign FOREIGN KEY (work_order_id) REFERENCES work_orders (id)) (SQL: insert into airframe_logbook_entries (aircraft_id, created_at, task_id, updated_at) values (1, 2019-03-04 22:11:48, 12, 2019-03-04 22:11:48))

I followed the Laravel 5.1 Documents: Many To Many Relationships - syncing for convenience that says:

Syncing For Convenience

You may also use the sync method to construct many-to-many associations. The sync method accepts an array of IDs to place on the intermediate table. Any IDs that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the IDs in the array will exist in the intermediate table:

$user->roles()->sync([1, 2, 3]);

You may also pass additional intermediate table values with the IDs:

$user->roles()->sync([1 => ['expires' => true], 2, 3]);



via Chebli Mohamed

What is the best way to secure Laravel/MySQL-API server from hackers?

Everyone we are working in a application. Where backed is laravel 5.1 and database is mysql. Both are under csf firewall and protected from the public. Below is the technology stack :-

Technology :

1) Laravel 5.1 backend Hosting : AWS/ EC2

2) Databse mysql 5.5 Hosting : RDS

3) Fronend Wordpress around 40 sites consuming api from backend.

Currently implemented Security :-

1) CSF firewall on backend.

2) Ip restrictions on RDS.

is there anything else we can do make it secure. Becuase we are saving customer personal and identification details.

Any suggestion will be appreciated.



via Chebli Mohamed