#StackBounty: #javascript #jquery #laravel #turbolinks #laravel-mix Laravel Mix and Turbolinks

Bounty: 50

I am having some problems with turbolinks setup. Here is setup: Laravel Mix – with defaults: bootstrap, jquery,..). And just after bootstrap.js file I have included turbolinks. Everything works until page reload – where I always get error. What am I doing wrong?

ERROR:

app.js:1282 Uncaught TypeError: $ is not a function
    at HTMLDocument.<anonymous> (app.js:1282)
    at Object.push../node_modules/turbolinks/dist/turbolinks.js.e.dispatch (vendor.js:105933)
    at r.notifyApplicationAfterPageLoad (vendor.js:105934)
    at r.visitCompleted (vendor.js:105934)
    at r.complete (vendor.js:105933)
    at r.<anonymous> (vendor.js:105933)
    at vendor.js:105933

LINE with ERROR:

document.addEventListener('turbolinks:load',function() {
    new SimpleBar(document.getElementsByClassName("js-simplebar")[0]);
    $(".sidebar-toggle").on("click", function() {
        //...
    });
});

EDIT
Here are js includes (compiled – Laravel Mix)

<head>
/js/manifest.js
<scrip`enter code here`t defer src="/js/vendor.js" data-turbolinks-track="true"></script>
/js/app.js
</head>

This is my app.js:

require('./bootstrap');
let Turbolinks = require('turbolinks');
Turbolinks.start();

require("./dashboard");
require('./custom/INotifier').run();

require("./theme/bootstrap");
require("./theme/theme");

And bootstrap.js (includes jQuery, bootstrap js, axios,…)

try {

window.Popper = require('popper.js').default;
window.$ = window.jQuery = require('jquery');
require('bootstrap');

} catch (e) {}

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';


Get this bounty!!!

#StackBounty: #php #laravel #redis #throttling #task-queue Laravel Redis Queue Ignoring Throttling

Bounty: 100

I am using Laravel Horizon and Redis and I am trying to throttle it. I am using an external API that has a rate limit of 100 requests per minute. I need to make about 700 requests. I have it setup so that every job I add to the queue only performs one API call in the job itself. So if I throttle the queue I should be able to stay within the limits. For some reason no throttling is happening and it instead blows through the queue (thus triggering many API errors of course). However the throttle works locally just not on my server.

I was originally trying to throttle per Laravel’s queue documentation but could only get it to work locally so I swapped to trying out the laravel-queue-rate-limit package on Github. As per the README I added the following to my queue.php config file:

'rateLimits' => [
        'default' => [ // queue name
            'allows' => 75, // 75 job
            'every' => 60 // per 60 seconds
        ]
    ],     

For some reason the throttling works properly when I run it on my local Ubuntu environment, but it does not work on my server (also Ubuntu). On the server it just blows through the queue as if there is no throttle in place.

Is there something I am doing wrong or maybe a better way to handle a rate limited external API?

Edit 1:

config/horizon.php

    'environments' => [
        'production' => [
            'supervisor-1' => [
                'connection' => 'redis',
                'queue' => ['default'],
                'balance' => 'simple',
                'processes' => 3,
                'tries' => 100,
            ],
        ],

One of the handle’s that starts most jobs:

    public function handle()
    {
        $updatedPlayerIds = [];
        foreach ($this->players as $player) {
            $playerUpdate = Player::updateOrCreate(
                [
                    'id' => $player['id'],
                ],
                [
                    'faction_id' => $player['faction_id'],
                    'name' => $player['name'],
                ]
            );

            // Storing id's of records updated
            $updatedPlayerIds[] = $playerUpdate->id;

            // If it's a new player or the player was last updated awhile ago, then get new data!
            if ($playerUpdate->wasRecentlyCreated ||
                $playerUpdate->last_complete_update_at == null ||
                Carbon::parse($playerUpdate->last_complete_update_at)->diffInHours(Carbon::now()) >= 6) {
                Log::info("Updating '{$playerUpdate->name}' with new data", ['playerUpdate' => $playerUpdate]);
                UpdatePlayer::dispatch($playerUpdate);
            } else {
//                Log::debug("Player data fresh, no update performed", ['playerUpdate' => $playerUpdate]);
            }
        }

        //Delete any ID's that were not updated via the API
        Player::where('faction_id', $this->faction->id)->whereNotIn('id', $updatedPlayerIds)->delete();
    }

Also, here is a rough diagram I made trying to illustrate how I have multiple job PHP files that end up spawned in a short amount of time, especially ones like the updatePlayer() which are often spawned 700 times.

diagram


Get this bounty!!!

#StackBounty: #php #arrays #json #laravel Reading big arrays from big json file in php

Bounty: 500

I know my question has a lot of answers on the internet but it’s seems i can’t find a good answer for it, so i will try to explain what i have and hope for the best,

so what i’m trying to do is reading a big json file that might be has more complex structure "nested objects with big arrays" than this but for simple example:

{
  "data": {
    "time": [
      1,
      2,
      3,
      4,
      5,
       ...
    ],
    "values": [
      1,
      2,
      3,
      4,
      6,
       ...
    ]
  }
}

this file might be 200M or more, and i’m using file_get_contents() and json_decode() to read the data from the file,

then i put the result in variable and loop over the time and take the time value with the current index to get the corresponding value by index form the values array, then save the time and the value in the database but this taking so much CPU and Memory, is their a better way to do this

a better functions to use, a better json structure to use, or maybe a better data format than json to do this

my code:

$data = json_decode(file_get_contents(storage_path("test/ts/ts_big_data.json")), true);
        
foreach(data["time"] as $timeIndex => timeValue) {
    saveInDataBase(timeValue, data["values"][timeIndex])
}

thanks in advance for any help

Update 06/29/2020:

i have another more complex json structure example

{
      "data": {
        "set_1": {
          "sub_set_1": {
            "info_1": {
              "details_1": {
                "data_1": [1,2,3,4,5,...],
                "data_2": [1,2,3,4,5,...],
                "data_3": [1,2,3,4,5,...],
                "data_4": [1,2,3,4,5,...],
                "data_5": 10254552
              },
              "details_2": [
                [1,2,3,4,5,...],
                [1,2,3,4,5,...],
                [1,2,3,4,5,...],
              ]
            },
            "info_2": {
              "details_1": {
                "data_1": {
                  "arr_1": [1,2,3,4,5,...],
                  "arr_2": [1,2,3,4,5,...]
                },
                "data_2": {
                 "arr_1": [1,2,3,4,5,...],
                  "arr_2": [1,2,3,4,5,...]
                },
                "data_5": {
                  "text": "some text"
                }
              },
              "details_2": [1,2,3,4,5,...]
            }
          }, ...
        }, ...
      }
    } 

the file size might be around 500MB or More and the arrays inside this json file might have around 100MB of data or more.

and my question how can i get any peace and navigate between nodes of this data with the most efficient way that will not take much RAM and CPU, i can’t read the file line by line because i need to get any peace of data when i have to,

is python for example more suitable for handling this big data with more efficient than php ?

please if you can provide a detailed answer i think it will be much help for every one that looking to do this big data stuff with php.


Get this bounty!!!

#StackBounty: #php #html #laravel #laravel-blade #utf8mb4 HTML Special Characters and Emojis Showing up Incorrectly on Laravel Webpage

Bounty: 100

I am likely doing something silly and thought this would be pretty straightforward. I am pulling data from an external API and storing it right into the database via Eloquent model’s I am creating/saving.

The data saved into the database field looks like this:
I Can't Believe It's A – The field is using utf8mb4_unicode_ci collation.

My webpage has the following meta data:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

And I am displaying it via my Laravel blade template like so:

<td>{{ $company->type->name }}</td>

I am a bit confused what I am doing wrong here? From the documentation and other stackoverflow questions I appear to be doing it correctly. My config/database.php has the following:

        'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            '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' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => 'InnoDB',
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

I believe this is all correct. Is there something I am missing here? Switching to use the unescaped {!! !!} seems to result in the content still being displayed the same. I am using Laravel v7.13.0

Also Google Chrome reports it is UTF-8 rendered:

> document.characterSet
"UTF-8"

Edit:

Here is an example of getting the response and what the returned JSON name looks like:

>>> $response = Http::withOptions(
            [
                'verify' => false,
                'base_uri' => config('custom.torn_api_base'),
                'timeout' => 5.0
            ]
        )->get(
            "company/79831",
            [
                'selections' => 'profile',
                'key' => config('custom.torn_api_key')
            ]
        );
=> IlluminateHttpClientResponse {#3393
     +"cookies": GuzzleHttpCookieCookieJar {#3379},
     +"transferStats": GuzzleHttpTransferStats {#3418},
   }

>>> $response->json()['company']['name'];
=> "👾 Button Mashers™"

^ You can see above their API is giving me the same string with the UTF-8 encoding that would be used on their website.

Here is me creating and saving the model to the database:

            $company = Company::updateOrCreate(
                [
                    'id' => $tornPlayerData['job']['company_id']
                ],
                [
                    'name' => $tornPlayerData['job']['company_name'],
                    'player_id' => $this->player->id,
                    'isOwner' => true
                ]
            );

I also am logging the response and here is what one of the lines with an Emoji looks like:

Log::info("Updating company '{$company->name}'' now", ['company' => $company]);

laravel.log│[2020-06-22 00:43:52] staging.INFO: Updating company '👾 Button Mashers™'' now {"company":{"App\Company":{"id":79831,"name":"👾 Button Mashers™","player_id":2141091,"updated_at":"2020-06-22T04:43:52.000000Z","created_at":"2020-06-22T04:43:52.000000Z"}}}

Edit 2:
I just manually copied and pasted the Tinker output of 👾 Button Mashers™ to name in the associated database row. Now the website displays that manually adjusted one properly. So it seems Laravel is doing something weird to the data from the API when it is storing it and I am unsure why that would be.

Edit 3:

Using the HEX query as the user asked in the answer.

SELECT id,name,HEX(name) FROM `companies` WHERE id=60335

(60335, 'Pokey's Play House!', '506F6B657926233033393B7320506C617920486F75736521');

(60335, ''', '26233033393B');

The second result is with me modifying the Pokey's Play House! to just ' so you can see the result of just the apostrophe.

Edit 4:

The blade template:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>Company Directory</title>

        <!-- Fonts -->
{{--        <link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">--}}

        <!-- Styles -->
          ** Snipped some minor CSS away from here **
        </style>
        <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css">

        <!-- Scripts -->

        https://code.jquery.com/jquery-3.5.1.slim.min.js
        https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js
        <script type="text/javascript" charset="utf8">
            **removed some datatables code from here for this paste**
        </script>

    </head>
    <body>
        <table id='directory-table' class='display'>
            <thead>
                <tr>
                    <th>Player Name</th>
                    <th>Company Name</th>
                    <th>Type</th>
                    <th>Rank</th>
                    <th>Positions</th>
                </tr>
            </thead>
            <tbody>
                @forelse ($companies as $company)
                    <tr>
                        <td>
                            <a href='https://www.example.
net/profiles.php?XID={{ $company->player->id }}'>
                                {{ $company->player->name }}
                            </a>
                        </td>
                        <td>
                            <a href='https://www.example.net/joblist.php#/p=corpinfo&userID={{ $company->id }}'>
                                {{ $company->name }}
                            </a>
                        </td>
                        <td>{{ $company->type->name }}</td> //THIS IS THE PROBLEM STRING HERE BEING OUTPUTTED
                        <td> {{ $company->rank }}</td>
                        <td> {{ $company->hired_employees }}/{{ $company->max_employees }}</td>
                    </tr>
                @empty
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                @endforelse
            </tbody>
        </table>
    </body>
</html>


Get this bounty!!!

#StackBounty: #php #mysql #laravel #eloquent #laravel-6 Laravel relationship conflicts in union

Bounty: 100

I have following model:
1- User model

 /**
 * Define user and functional area relationship
 */
public function functionalAreas()
{
    return $this->belongsToMany('AppFunctionalArea', 'user_functional_areas', 'user_id', 'functional_area_id')->withPivot('id', 'is_primary')->withTimestamps();
}

and Business model:

 /**
 * Define business and user functional area relationship
 */
public function functionalAreas()
{
    return $this->belongsToMany('AppFunctionalArea', 'business_functional_areas', 'business_id', 'functional_area_id')->withTimestamps();
}

Now I should take all businesses and users and show them in a single list, for this I’m using from union, following is my query:

public function usersAndOrganizations()
{
    $users = $this->users();

    $organizations = $this->organizations();

    $invitees = $users->union($organizations)->paginate(10);

    return response()->json($invitees);
}

private function users()
{
    $users = User::byState($approved = true, 'is_approved')
        ->search()->select([
            'id',
            DB::raw("CONCAT(first_name, ' ', last_name) AS name"),
            'about',
            'address',
            'slug',
            'average_reviews',
            DB::raw("'freelancer' AS type")
        ]);

  $users = $users->with([
        "functionalAreas" => function ($q) {
            $q->select([
                'functional_areas.id',
                DB::raw("functional_areas.name_en AS name"),
            ]);
        }
    ]);
    return $users;
}


private function organizations()
{
    $businesses = Business::where('owner_id', '!=', auth()->user()->id)->verified()
        ->active()->search()
        ->select([
            'id',
            'name',
            'about',
            'address',
            'slug',
            'average_reviews',
            DB::raw("'business' AS type")
        ]); 
        $businesses = $businesses
            ->with([
            "functionalAreas" => function ($q) {
                $q->select([
                    'functional_areas.id',
                    DB::raw("functional_areas.name_en AS name"),
                ]);
            }
        ]);
        return $businesses;
} 

But above query not return the business functional area, its output query use from user relationship instead of business, that with section generate twice the following query:

select `functional_areas`.`id`, functional_areas.name_en AS name, `user_functional_areas`.`user_id` as `pivot_user_id`, `user_functional_areas`.`functional_area_id` as `pivot_functional_area_id`, `user_functional_areas`.`id` as `pivot_id`, `user_functional_areas`.`is_primary` as `pivot_is_primary`, `user_functional_areas`.`created_at` as `pivot_created_at`, `user_functional_areas`.`updated_at` as `pivot_updated_at` from `functional_areas` inner join `user_functional_areas` on `functional_areas`.`id` = `user_functional_areas`.`functional_area_id` where `user_functional_areas`.`user_id` in (2, 6, 7)

But in fact 6, and 7 is business id not user only 2 is user id, one of this queries should use business_functional_areas instead of user_functional_areas.
One more thing found is, all items are inside AppUser model in result, its like businesses are also as user object.


Get this bounty!!!

#StackBounty: #mysql #laravel #eloquent #mariadb Laravel Eloquent order by field as 1,2,3,4,1,2,3,4

Bounty: 250

I have goods table

good_link, parent_link, name
sdf-sdfg   ffff         rock    
utyruuur   ffff         qwe     
gfhdfggg   dddd         paper   
sdfghvcx   eeee         water   
ncvbcxvb   dddd         tree    
dsgfdsg    zzzz         sdff
sdfsdff    zzzz         fdgdf
sdfgdgg    zzzz         sdfsdf
dsvfdgg    zzzz         ssdfgr
brtyfgh    zzzz         fgdfgdf

How can I order data by parent_link to have data like

ffff
dddd
eeee
zzzz
ffff
dddd
zzzz
zzzz
zzzz
zzzz

so all goods goes one after another but always with different parent_link(previous row parent_link != next row parent_link, and ordering A-Z has no difference)?


Get this bounty!!!

#StackBounty: #php #laravel laravel invalid route action with subdomain

Bounty: 50

What is the proper way to add a subdomain into your routes? I have a laravel/homestead project working on my local computer but when I move it to HostGator, a shared host server, it no longer works. Well the home page works but the links to the sub-pages don’t.

For example, the following route works fine on my local version:

Route::get('/appointments', 'AppointmentController@index');

But if I use that route on the remote server, it takes it to tekknow.net/appointments instead of tekknow.net/medaverter/appointments.

So I followed instructions from here:
https://laravel.com/docs/5.6/routing#route-group-sub-domain-routing
and added a prefix like this:

Route::prefix('MedAverter')->group(function() {
  Route::get('/appointments', 'AppointmentController@index');
});

But that doesn’t work either. It also goes to /tekknow.net/appointments

I also tried changing the route to this:

Route::get('/MedAverter/appointments', 'AppointmentController@index');

But that also went to tekknow.net/appointments

Anybody know what I’m doing wrong?

EDIT: I went onto my HostGator cPanel and looked at all my subdomains and saw that my subdomain root was medaverter.tekknow.net which is linked to Document root of medaverter.tekknow.net/MedAverter which gets redirected to http://www.tekknow.net/MedAverter. So I renamed my folder from medaverter to MedAverter to match the subdomain redirection.


Get this bounty!!!

#StackBounty: #php #laravel #sqlite Laravel 7, SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed when tryi…

Bounty: 50

I have three tables, User, Company and Department, with their respective models and factories.

I created a test where I’m adding the relationship:

// MyTest.php
$user = factory(User::class)->create();

$company = factory(Company::class)->make();
$company->user()->associate($user);
$company->create(); // it fails here because of NOT NULL constraint, companies.user_id

$department = factory(Department::class)->make();
$department->company()->associate($company);
$department->create();

I get the following error: Integrity constraint violation: 19 NOT NULL constraint failed: companies.user_id (SQL: insert into "companies" ("updated_at", "created_at") values (2020-03-10 07:27:51, 2020-03-10 07:27:51))

My table schema is defined like this:

// users
Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('name');
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('phone');
    $table->integer('user_type');
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

// companies
Schema::create('companies', function (Blueprint $table) {
    $table->id();
    $table->foreignId('user_id')->constrained()->onDelete('cascade');
    $table->string('name');
    $table->string('contact_email');
    $table->string('contact_phone');
    $table->timestamps();
});

// departments
Schema::create('departments', function (Blueprint $table) {
    $table->id();
    $table->foreignId('company_id')->constrained()->onDelete('cascade');
    $table->string('name');
    $table->string('contact_email');
    $table->string('contact_phone');
    $table->timestamps();
});

It is my understanding that there should be no NULL-values in SQL-tables, which is why I am deliberately trying to avoid ->nullable() in my migrations. Especially for foreign keys like these.

EDIT:

I tried doing it this way, I also made a pivot table for users_companies. Now I can attach a company, but I’m still getting an SQL-error when doing the test this way:

$user = factory(User::class)->create();
$company = factory(Company::class)->create();

$user->companies()->attach($company);
$company->departments()->create([
    'name' => 'Department 1',
    'contact_email' => 'department1@example.test',
    'contact_phone' => '95281000',
]);

This also fails with the error stated below:

$company = factory(Company::class)->create();
$company->departments()->save(factory(Department::class)->make());

The error is this: Integrity constraint violation: 19 NOT NULL constraint failed: departments.company_id (SQL: insert into "departments" ("name", "contact_email", "contact_phone", "company_id", "updated_at", "created_at") values (Department 1, department1@example.test, '123456789', ?, 2020-03-11 07:59:31, 2020-03-11 07:59:31)).

CompanyFactory.php

<?php

/** @var IlluminateDatabaseEloquentFactory $factory */

use AppCompany;
use FakerGenerator as Faker;

$factory->define(Company::class, function (Faker $faker) {
    return [
        'name' => 'Company 1',
        'contact_email' => 'company@example.test',
        'contact_phone' => '123456789',
    ];
});

Factories

DepartmentFactory.php

<?php

/** @var IlluminateDatabaseEloquentFactory $factory */

use AppDepartment;
use FakerGenerator as Faker;

$factory->define(Department::class, function (Faker $faker) {
    return [
        'name' => 'Department 1',
        'contact_email' => 'department1@example.test',
        'contact_phone' => '123456789',
    ];
});


Get this bounty!!!

#StackBounty: #php #laravel Laravel logging.channels.single.path with queue is cached?

Bounty: 50

Im using Queue::before in AppServiceProvider.php and set logging.channels.single.path value every time when job started:

config(['logging.channels.single.path' => storage_path('logs/accounts/'.$command->acc->login.'.log')]);

When I running 1 job all ok – logs in the right place.
When running 2 or more it writing logs to different files – one account can write to another accounts logfile. Why is it happening? It looks like it is caching the config variable.

Queue on horizon redis. One job after done dispatching another same job with the same $acc instance.


Get this bounty!!!

#StackBounty: #php #laravel #websocket #pusher #pusher-js Laravel Web-sockets and Chrome `SameSite` attribute

Bounty: 500

So there is a lot of issues about this sameSite buisness, but I cannot find any answers when it comes to Laravel Websockets. There is nothing in their documentation about this.

So I thought I would ask here and see if you guys have any ideas.

Consider the following configuration:

import Echo from 'laravel-echo'

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    wsHost: window.location.hostname,
    wsPort: 6001,
    wssPort: 6001,
    disabledStats: true,
    encrypted: false,
    enabledTransports: ['ws', 'wss'],
    namespace: 'App',
    auth: {
      headers: {
        'X-CSRF-TOKEN': token.content
      }
    }
});

According to their docs, they use the pusher library – but there is no indication of it actually hitting pusher website. And I dont need any pusher credentials as those are all faked.

The issue is simple:

A cookie associated with a cross-site resource at http://support.pusher.com/ was set without the SameSite attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with SameSite=None and Secure. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.

Which is all over stack overflow.

I disableSats and while yes the websockets do still run and work fine, eventually chrome will be like “nope”. So I have no idea if this is a pusher issuer, the laravel websockets issue or what.

Does any one have any ideas on what I could do or attempt to do? I have tried setting forceTLS: true in the above config and that seems to work – but then web sockets won’t connect.

I am worried that one day my app will just cease to work as we depend on websockets for a lot of functionality and this is one of the easiest and best libraries out there that do not require days of research and set up.


Get this bounty!!!