pilat

Experience

9,480

0 Best Reply Awards

  • Member Since 1 Year Ago
  • 83 Lessons Completed
  • 0 Favorites

12th December, 2017

pilat started a new conversation Is There A Nice Way To Handle Empty Dates In Templates? • 2 minutes ago

Hi,

Let say, I have a nullable Date or DateTime field in my DB. Eloquent exports it to templates as Carbon object, so, I can freely setup formatting:

<td>{{ $record->some_date_field->format('d.m.Y') }}</td>

This is very convenient, and I'd prefer to have this option available.

The problem is when 'm trying to display dates from those records, that have it empty (or null; or zero, if I store timestamps). Here's a piece of real code:

    public function getCompleteTillCarbonAttribute()
    {
        return \Carbon\Carbon::createFromTimestamp($this->complete_till);
    }
<td>{{ $task->complete_till_carbon->format('Y-m-d') }}</td>

When original complete_till is 0, I'll have 1970-01-01 date on the output. To avoid this, I should do one of the following:

  1. Use conditions in blade:
<td>{{ 0 === $task->complete_till ? 'n/a' : $task->complete_till_carbon->format('Y-m-d') }}</td>
  1. Make a function that returns date as string:
    public function getCompleteTillStringAttribute()
    {
    return 0 === $this->complete_till
        ? 'n/a'
        : date('d.m.Y', $this->complete_till); // don't see any use for Carbon in this case
    }

Neither way is very elegant, I wold say. I'd prefer to control the template to be in charge for appearance, but not to have that ugly ternary condition operator…

Something like this: {{ $record->a_date->format('d.m.Y')->ifEmpty('n/a') }}, or this: {{ $record->a_date->format('d.m.Y', 'n/a') }};

Do anyone have good solution to the issue?

27th November, 2017

pilat left a reply on Eager Load Relations Stored In Json Field • 2 weeks ago

I'm on the same issue too. Here's my example:

# Eloquent model:
class Activity extends Model
{
    // ...
    public function notifications()
    {
        return $this->hasMany(\App\NotifyLog::class, 'report->activity->element_id', 'element_id');
    }
    // ...
}


# Client code:
# 1. No eager loading:
App\Entities\Activity::has('notifications')->take(3)->get()->map(function ($a) { return $a->notifications; });
# ^^^ works like a charm.

# 2. With eager loading:
App\Entities\Activity::with->('notifications')->has('notifications')->take(3)->get()->map(function ($a) { return $a->notifications; });
# ^^^ nope… Here's the exact results:
# "select * from `activities` where exists (select * from `notify_logs` where `notify_logs`.`report`->'$."activity"."element_id"' = `activities`.`element_id`) limit 3"
# []
# 17504.04
# "select * from `notify_logs` where `notify_logs`.`report`->'$."activity"."element_id"' in (?, ?)"
# array:2 [
#   0 => 15062832
#   1 => 13737651
# ]
# 1.27
# => Illuminate\Support\Collection {#1114
#      all: [
#        Illuminate\Database\Eloquent\Collection {#1085
#          all: [],
#        },
#        Illuminate\Database\Eloquent\Collection {#1087
#          all: [],
#        },
#        Illuminate\Database\Eloquent\Collection {#1061
#          all: [],
#        },
#      ],
#    }

pilat left a reply on With() Does Not Work If I Use Where() In Relations • 2 weeks ago

Ok. What I understand at this moment is that there has to be an option to use ::withColumn() in the eager-loading query builder.

Something like this:

    public function custom_fields()
    {
        return $this->hasMany(\App\CustomField::class, 'element_id', 'element_id')
            ->whereColumn('custom_fields.subdomain', 'activities.subdomain');
            // or simpler: ->whereColumn('subdomain', 'subdomain');
    }

    public function lead()
    {
        return $this->hasOne(\App\AmoLeadsCache::class, 'id', 'element_id')
             ->whereColumn('leads_cache.subdomain', 'activities.subdomain');
    }
    // this doesn't work, off cause…

At the moment, I'll stick to the custom made local scopes, like this one:

    public function scopeWithLead($query, $subdomain)
    {
        return $query
            ->with([ 'lead' => function ($query) use ($subdomain) {
                $query
                    ->where('subdomain', $subdomain);
            }]);
    }

, although I don't find it very DRY having to repeat that $subdomain constraint on both the Activitiy and its relations:

// finally, the client code:
\App\Entities\Activity::subdomain('subdomain1')->withLead('subdomain1')->take(3)->get();
// don't like that repetition of subdomain constraint however… :/

22nd November, 2017

pilat left a reply on With() Does Not Work If I Use Where() In Relations • 2 weeks ago

@BryceSharp thank you. It looks similar to my current workaround.

However, I loose eager loading this way. So, if I have, say, 500 activities in the list, I'll execute 500 extra SQL queries to fill them all.

You are assuming that the instance of Activity calling fillData() has the scope of the eager loaded lead.

Yes, this is what bothering me. Why is it exactyl there is no that scope when I use ->where() in the relation functions, but the scope is present when I don't use ->where()? Is there a way I could preserve both the extra constraint in relations AND the eager loading?

21st November, 2017

pilat left a reply on With() Does Not Work If I Use Where() In Relations • 3 weeks ago

Listening to SQL queries:

>>> DB::listen(function ($query) { dump($query->sql); dump($query->bindings); dump($query->time); });
=> null
>>> App\Entities\Activity::with('lead')->latest()->take(3)->get()->map(function ($a) { return $a->fillData('{name}: {url}'); });
"select * from `activities` order by `created_at` desc limit 3"
[]
74.0
"select * from `leads_cache` where `leads_cache`.`subdomain` is null and `leads_cache`.`id` in (?, ?, ?)"
array:3 [
  0 => 4879927
  1 => 5219288
  2 => 5673393
]
13.18
=> Illuminate\Support\Collection {#1034
     all: [
       ": ",
       ": ",
       ": ",
     ],
   }

So, it looks for null subdomain for some reason…

But if there's no eager loading, it makes proper queries (just too many of them):

>>> App\Entities\Activity::latest()->take(3)->get()->map(function ($a) { return $a->fillData('{name}: {url}'); });
"select * from `activities` order by `created_at` desc limit 3"
[]
80.55
"select * from `leads_cache` where `leads_cache`.`id` = ? and `leads_cache`.`id` is not null and `leads_cache`.`subdomain` = ? limit 1"
array:2 [
  0 => 4879927
  1 => "mysubdomain"
]
8.77
"select * from `leads_cache` where `leads_cache`.`id` = ? and `leads_cache`.`id` is not null and `leads_cache`.`subdomain` = ? limit 1"
array:2 [
  0 => 5219288
  1 => "mysubdomain"
]
1.31
"select * from `leads_cache` where `leads_cache`.`id` = ? and `leads_cache`.`id` is not null and `leads_cache`.`subdomain` = ? limit 1"
array:2 [
  0 => 5673393
  1 => "mysubdomain"
]
0.75
=> Illuminate\Support\Collection {#1028
     all: [
       "Name 1: https://url1",
       "Name 2: https://url2",
       "Name 3: https://url3",
     ],
   }

pilat left a reply on With() Does Not Work If I Use Where() In Relations • 3 weeks ago

Ok, I made that client code up, in fact.

Here's the part of the same Activity class that does not work:

class Activity extends Model
{
    // ...

    public function fillData($template)
    {
        if (strpos($template, '{price}') !== false) {
            $replaceData['price'] = empty($this->lead->price) ? '0' : $this->lead->price;
        }
        if (strpos($template, '{url}') !== false) {
            $replaceData['url'] = empty($this->lead->url) ? '[no url]' : $this->lead->url;
        }
        if (strpos($template, '{name}') !== false) {
            $replaceData['name'] = empty($this->lead->name) ? '[no name]' : $this->lead->name;
    }

    // ...
}

Now goes the client code:

1. No eager loading:

>>> App\Entities\Activity::latest()->take(3)->get()->map(function ($a) { return $a->fillData('{name}: {url}'); });

=> Illuminate\Support\Collection {#1026
     all: [
       "Name 1: https://url1",
       "Name 2: https://url2",
       "Name 3: https://url3",
     ],
   }

2. With eager loading:

>>> App\Entities\Activity::with('lead')->latest()->take(3)->get()->map(function ($a) { return $a->fillData('{name}: {url}'); });

=> Illuminate\Support\Collection {#1028
     all: [
       ": ",
       ": ",
       ": ",
     ],
   }

3. Still with eager loading, but without "::where()" in relation functions (my very first example):

>>> App\Entities\Activity::with('lead')->latest()->take(3)->get()->map(function ($a) { return $a->fillData('{name}: {url}'); });

=> Illuminate\Support\Collection {#1026
     all: [
       "Name 1: https://url1",
       "Name 2: https://url2",
       "Name 3: https://url3",
     ],
   }

20th November, 2017

pilat started a new conversation With() Does Not Work If I Use Where() In Relations • 3 weeks ago

Hi, I've stumbled over this issue.

Laravel 5.3

Here's couple of examples:

class Activity extends Model
{
    public function custom_fields()
    {
        return $this->hasMany(\App\CustomField::class, 'element_id', 'element_id');
    }

    public function lead()
    {
        return $this->hasOne(\App\AmoLeadsCache::class, 'id', 'element_id');
    }
}

class Activity extends Model
{
    public function custom_fields()
    {
        return $this->hasMany(\App\CustomField::class, 'element_id', 'element_id')
            ->where('custom_fields.subdomain', $this->subdomain);
    }

    public function lead()
    {
        return $this->hasOne(\App\AmoLeadsCache::class, 'id', 'element_id')
            ->where('leads_cache.subdomain', $this->subdomain);
    }
}

Now, in client's code I do the following:

Activity::with('custom_fields', 'lead')->where(/*some other cond*/)->get();

It works as expected if I don't have ->where() in the "relations" methods. If i have, however, all relations are empty (lead=>null, custom_fields=>[]).

Anyone knows how to overcome this? I really need to preserve those ->where() clauses, but I also need eager loading.

18th October, 2017

pilat started a new conversation Arrow Keys Don't Work In Tinker • 1 month ago

Help, please,

There's a problem which causes me lots of pain when I'm trying to use tinker on production server.

In the local development I can use Up and Down keys to browse commands history. Also, I often use Ctrl+{A,E} to move to the line start/end. None of this works when I'm on remote server via ssh. Even Left and Right keys are not working :-(

Here's, what I get when pressing this or that key:

>>> Up arrow: ^[[A ; Down arrow: ^[[B ; Left: ^[[D ; Right: ^[[C ; Ctrl+A: ^A ; Ctrl+E: ^E

Terminal: iTerm2

Remote server OS: some sort of linux... uname -a only says it's Linux and then displays the host name.

Note: I can use arrows in shell (/bin/bash), when connected to that server. The issue is only in tinker.

13th September, 2017

pilat left a reply on Performant Way To Mass "update Or Create" • 2 months ago

@kfirba Thank you, this is something I've thought of, actually, just wanted to avoid slipping off the ORM-way. Also, there is a strange error when I'm trying to add "unique index" to this table (looks that I have some records violating this).

But I should definitely give it a try and measure how fast if would become.

Btw, I import 500 records at a time, are there any limitation on the SQL query length?

P.S.: there was a point in past, when I simply used REPLACE INTO …but it kept increasing autoincremental ` and that was also "no an ORM" :-)

pilat started a new conversation Performant Way To Mass "update Or Create" • 2 months ago

Hi, I'm looking for a way to do something like updateOrCreateMany (imaginary method).Currently, the code looks like this:

class ActivityRepositoryEloquent extends BaseRepository implements ActivityRepository
{
    // ... before this method, that was something like:
    //  $this->model
           ->whereIn('element_id', array_column($records, 'element_id')
           ->update([ 'delete' => true ]);

    public function insertMany($records)
    {
        $searchFields = [
            'subdomain',
            'element_type',
            'element_id',
            'slug',
        ];

        foreach ($records as $record) {
            $record['deleted'] = false;

            $this->model->updateOrCreate(
                array_only($record, $searchFields),
                array_except($record, $searchFields)
            );
        }
    }
}

What it does, in fact, is the following: it makes SELECT * …nd then `UPDATE ?or each of the records!

Is there a way to minimize the number of SQL queries?

A comment about that 'deleted' field: I'm syncing data with a 3rd-part service. For the original data, let's call is "leads", there is id field defined by 3rd-party system. So, I simply store it in my database with that ID and have no issues. There is related data, however, like "custom fields", that I (re)generate in my site, basing on the leads data. Here I have auto-incremental id and I've noticed that my genius schema (deleting all related and re-generating it after leads sync) causes ids to grow higher and higher after each sync.

So, the revised schema is the following:

  1. Set deleted = false for all the custom fields, with element_id IN ([ array of lead ids ])

  2. updateOrCreate custom fields, basing on leads data + hardcoded deleted = 0

  3. Delete all the records that have deleted = 1 remaining;

22nd August, 2017

pilat left a reply on How To Retrieve Raw Url Query String • 3 months ago

http_build_query($request->query()); // for GET params only
// or
http_build_query($request->input()); // for all data

Not only this would give you "query string, formatted for direct using in links", it would also prepare it in accordance to all related RFCs: eliminate duplicates, encode non-latin characters and so on.

16th August, 2017

pilat left a reply on Design Question: Controllers, Request Processing • 3 months ago

The very first dependency is $subdomain. It's just a string, reflecting currently selected "account". This is an account in a 3rd-party CRM system, in fact, and every user of my Laravel-based site is expected to belong to one or another (or even to several) such accounts. And, even if a user belongs to several accounts, each request to one of "account-aware pages" is bound to one account only (and there must be one).

2nd dependency is, let's call it $api. It is used to communicate with that CRM via API. provides methods like buildApiUrl(), apiCall(), getUsers(), getGroups(), getCustomFields() and many others. I need to know $subdomain when initializing this $api dependency.

3rd one is $conf. It's not an instance of certain class, but rather an stdClass object. I build this $conf once with a command like $this->conf = (object)App::make(\App\Config::class, [ $this->subdomain ])->getAllAsArray(); and then access config variables in $this->conf->var_name fashion. Also, there's separate configuration for each "subdomain".

On the second though, I should probably do the following this time:

  1. Not sure that I even need that parent ServiceAwareController (at least now), considering that I needed it for a common constructor from the very beginning, and then it turned out that controller's constructor was not a very good place to touch Request.

  2. Obtain dependencies from the service container in each action method, just like in "something I'd like to avoid" examples.

I often confuse action methods with other kinds of methods, forgetting that there is only one action method called within the request. So, all that "loading from service container", however "repeating" it looks in the code, will actually be called once per each request, which is good.

  1. Make controllers slimmer. Much slimmer than I have them now. This way I may actually need less dependencies in my action methods.

  2. Then, (after #2 + #3 are done) analyze what is repeating too much and do something with it, if it still itches.

pilat left a reply on Design Question: Controllers, Request Processing • 3 months ago

@martinbean I'm trying to become a bit more DRY about dependencies hooking. Currently, my controller actions look like it's shown in the "would like to try to avoid" examples (both kinds, in fact), but I've realized that the set of dependencies I need inside action methods is almost the same each time ('subdomain', 'conf', 'api_connector', something like this), so, maybe I could move them out of single actions somehow?

pilat started a new conversation Design Question: Controllers, Request Processing • 3 months ago

Hi, I have a number of controllers (but not all), where I need access to specific set of dependencies. Ideally, I'd like to access these dependencies as the class's properties, so, they're like "cached", and they're accessible from any method, closure etc. within that controller.

Here's the meta-code:

class ServiceAwareController extends Controller
{
  protected $subdomain, $dep1, $dep2, $dep3;

  public function __construct()
  {
    $this->subdomain = $request->subdomain;
    $this->dep1 = App::make('dep1', [ $this->subdomain ]);
    $this->dep2 = App::make('dep2', [ $this->subdomain ]);
    $this->dep3 = App::make('dep3', [ $this->dep3 ]);
    // ...
  }
}

class ChildController1 extends ServiceAwareController
{
  public function anActionMethod()
  {
    return $this->dep2->produceSomething();
  }
}

class ChildController2 extends ServiceAwareController
{
  public function anActionMethod()
  {
    return $this->dep1->produceSomething();
  }
}```


The issue: my services depend on request and controller's constructor is not a place to work with request. The request is not fully ready, at this point. This I've read on Github when I've got stuck with "why there is no effect from my middlewares": I've placed some code into the controller's constructor right after `$this->middleware()` calls, and that code presumed all middlewares have already run, which was why it failed.

Is there an elegant solution on how to provide easy access to dependencies to all my "child controllers"? Without overloading action methods' signatures, or putting numerous "App::make" in the beginning of each of them?

I.e., this is something I'd like to avoid, if possible:
```php
class ChildController1 extends ServiceAwareController
{
  public function anActionMethod(Dep1 $dep1, Dep2 $dep2)
  {
    // 1. signature is overloaded
    // 2. there might be some extra logic required to instantiate dependency properly (well, at least I can put that logic into service provider)
  }

  public function anotherActionMethod(Dep1 $dep1, Dep2 $dep2)
  {
    $this->subdomain = $request->subdomain;
    $this->dep1 = App::make('dep1', [ $this->subdomain ]);
    $this->dep2 = App::make('dep2', [ $this->subdomain ]);
    $this->dep3 = App::make('dep3', [ $this->dep3 ]);
    // it won't be very DRY if I put this stuff at the top of each action method...
  }```

15th August, 2017

pilat left a reply on Auth()->check() Inside Error Template • 3 months ago

Why would you want to run abort in a service provider and then still have your application work?

I don't want it to work. I want to display an error page, but to offer some adequate (to the current auth status) links for the user to "get out from this error page".

pilat started a new conversation Auth()->check() Inside Error Template • 3 months ago

Good Morning,

I have an issue with auth() checking inside the error template. Here's what I'm trying to do:

@if (auth()->check())
    <a href="{{ url('home') }}" class="btn btn-default">Home</a>
@else
    <a href="{{ url('register') }}" class="btn btn-default">Register</a>
    <a href="{{ url('login') }}" class="btn btn-default">Login</a>
@endif

Now, if I call abort() inside a controller — the `auth()-check works as expected. If I call `abort from inside a service provider (deferred), however, `auth()->checkalways returns false.

Is there a way to work around this issue?

14th August, 2017

pilat started a new conversation Cannot Test HTTP Responses When Using Abort() Inside A Service Provider • 3 months ago

Hi, an't figure out one thing:

I have the following code in my Service Provider (it's deferred, by the way):

if ($someCond) {
  abort(404, 'blah-blah-blah');
}

… and here's the test:

/** @test */
public function test_regression_404_error_did_not_consider_auth_status()
{
    $this
        ->actingAs(User::find(1))
        ->get('section/somethingimaginary')
        ->seeStatusCode(404)
        ->see('something that only logged user should see on the error page');
}

When I try to run this test, I see the following error in PHPUnit's output:

1) MiddlewaresTest::test_regression_404_error_did_not_consider_auth_status
Symfony\Component\HttpKernel\Exception\NotFoundHttpException: blah-blah-blah

How can I make this test work?

Notes:

  • the service provider is deferred;

  • when trying this same URL in the browser, I can actually see the error page I expect and the browser knows it has 404 response code;

  • there is something conditional (dependent on auth()->check()) on the error page, and it doesn't work, in fact. The error screen is alway as there were no logged in user. When I did abort(403) from a controller, this issue was not here.

11th August, 2017

pilat left a reply on Asserting Array/json Structure Without Doing Any Request • 4 months ago

@JhumanJ this is not what I look for. I need an ability to test my array structure, using the same "template convention" as in seeJsonStructure().

pilat started a new conversation Asserting Array/json Structure Without Doing Any Request • 4 months ago

Hi, I'd like to test array structure, returned by some methods. I like the way how seeJsonSructure() works, but I can't apply it to a "raw data".

Here's example of what I'm trying to do:

        $this->seeJsonStructure(
            [
                '*' => [
                    'id', 'name',
                ],
            ],
            $object->getAllThings()
        );

I understand that seeJsonStructure() should be called on a response, but I don't have separate endpoint for getting just this list. Any workarounds?

2nd July, 2017

pilat left a reply on Should I Use Laravel Notifications For This? • 5 months ago

Ok, it seems I'd better create another kind of App\User, who has extra attributes: emails and phones and who will return these arrays in the routeNotificationFor($driver) function.

Activity class, on the other side, will have functions to return collections of these users. So, sending notifications, related to an activity, would look like this:

Notification::send($activity->getWorkerUsers(), new NotificationForAWorker());

Notification::send($activity->getCusomerUsers(), new NotificationForACustomer());

30th June, 2017

pilat left a reply on Should I Use Laravel Notifications For This? • 5 months ago

#2 seems to be possible to control by via() method:

public function via($notifiable)
    {
        if (!empty($this->opts['debug'])) {
            return [];
        }

        if (!empty($this->opts['channels'])) {
            return $this->opts['channels'];
        }

        return [ 'mail', MySmsChannel::class ];
    }

#4 is also not actual anymore (there's $notifiable as argument to many methods).

However, I need a way to find how to control recipients emails/phones. I need different sets of addresses, depending on either this is notification to Customers or to Workers… Should I make different channels? Or can the recipients list (returned by "Activity:: routeNotificationFor ") be altered somehow from inside the Notification class?

pilat started a new conversation Should I Use Laravel Notifications For This? • 5 months ago

I have a custom entity, let say: "Activity", that bears some data inside and is related to some other data. So, let say, to get notifications recipients, there are methods like: getWorkersEmails(), getCustomersEmails(), getWorkersPhones(), etc.

Now, I need to send various kinds on notifications on some events, related to this activity. For example: ActivityAssigned. To name a few:

  • Activity Assigned notification, sent to Workers by Email;
  • Activity Assigned notification, sent to Customers by Email;
  • Activity Assigned notification, sent to Workers by SMS;
  • Activity Assigned notification, sent to Customers by SMS.
  • ...

Can I use Notification here? Or, is it too complex and I'd rather make special model for this stuff?

Some ideas, in relation to notifications:

  1. They should be fired by $activity instance, because those Workers/Customers are not present in the system as user.

So, the Activity model will be something like this:

class Activity extends Model
{
    use Notifiable;

    // ....

    public function routeNotificationFor($driver)
    {
        if (method_exists($this, $method = 'routeNotificationFor'.Str::studly($driver))) {
            return $this->{$method}();
        }

        switch ($driver) {
            case 'mail_worker':
                return $this->getWorkersEmails();
            case 'mail_customer':
                return $this->getAllContactsEmails();
            case 'sms_worker':
                return $this->getAllWorkersPhones();
            case 'sms_customer':
                return $this->getAllContactsPhones();
        }
    }
}
  1. I think cold group notifications by 'to whom', but implement a series of toChannel() methods inside each of those classes.

  2. I need a way to prevent sending of notifications to selected channels, basing on the $options argument, injected into notification instance. Is is possible at all (besides using exceptions)? Which method should return null or false to have notification to certain channel cancelled?

  3. From inside Notification class, do I have access to the the object, on which ->notify() was called? I.e., instead of: $activity->notify(new ActivityAssignedWorkers($activity, $options)), I'd prefer to have $activity->notify(new ActivityAssignedWorkers($justOptions)).

  4. Is there a way to return something to the place, where $object->notify was called? E.g.: $report = $activity->notify(ActivityAssignedWorkers($options));

pilat left a reply on Using MailMessage In A Mailable Class • 5 months ago

There's a workaround, please check it out: https://medium.com/@woganmay/convert-mailmessage-to-mailable-in-laravel-5-3-d26e74b55c51

I'd like to know if there's a better way, though.

P.S.: here's my version:

// inside mailable's "handle()":

        $message = (new MailMessage)
            ->greeting('Здравствуйте,')
            ->subject($this->subject)
            ->line($this->body);

        return $this->view('vendor.notifications.email', $message->data());

28th June, 2017

pilat left a reply on Tinker Reload? • 5 months ago

In iTerm2, you can open Preferences, then go to Keys, then add a new Key Mapping:

  1. Key Combination: I leave this to your own liking;
  2. Action: 'Send text with "vim" special characters"
  3. The text: exit\nphp artisan tinker\n.

30th May, 2017

pilat left a reply on How Do You Set Up PhpStorm With Existing Laravel + Homestead Project? • 6 months ago

Sorry, I'm not sure that I follow this. I have Valet installed on my machine. Project root is where app, routes, resources, etc. folders are, but web-server root is {projectRoot}/public. I can't find anything appropriate for this scenario in the "Project from existing files" scenario…

5th May, 2017

pilat left a reply on There Is An Issue, Which Is Only In Production Build • 7 months ago

turn out to be in the widget itself.

This code works in both "build modes":

<template>

  <div class="outer-block">

    <div v-if="cnd" class="inner-block-1"></div>

    <div class="inner-block-2"></div>

  </div>

</template>

This one only works in "dev" builds, but not in "--production":

<template>

  <div v-if="cnd" class="outer-block">
    <div class="inner-block-1"></div>
    <div class="inner-block-2"></div>
  </div>
  <div v-else class="outer-block">
    <div class="inner-block-2"></div>
  </div>

</template>

Still not sure why… :/

4th May, 2017

pilat started a new conversation There Is An Issue, Which Is Only In Production Build • 7 months ago

Hi, I have Laravel 5.3 installed, and I have very strange issue appearing only in gulp --production mode. It's not reproduced when building with just gulp.

In particular, one Vue component is simply not drawn for some reason.

Here's the template:

<template>

    <tbody class="dashboard__members">
        <tr class="data-row" v-for="member in membersData">
            <td
                v-for="value,idx in member.data"
                :data-order="getSorting(value)"
                :class="'dashboard__data-cell col-'+getColSpec(idx).code"
            >
                <component
                    :is="getColSpec(idx).widget"
                    :cell-value="value"
                    :cell-spec="getColSpec(idx)"
                ></component>
            </td>
        </tr>
    </tbody>

</template>

<script>

module.exports = {

    props: ['membersData', 'colSpec', 'isDeps'],

    methods: {
        getColSpec(idx) {
            return this.colSpec[idx];
        },
        getSorting(value) {
            let res;
            if (typeof(value.name) !== 'undefined')
                res = value.name;
            else if (typeof(value._value) !== 'undefined')
                res = Number(value._value);
            else
                res = Number(value);

            return res;
        }
    }
};

</script>

The result: one of widgets does not load, despite it's definitely there. Just to make it clear: this whole component (tbody) is called twice. Second time there is the same colSpec provided and in the second tbody all columns are rendered. But in the first tbody, 2nd column is skipped. In both 'tbodies', the column has the same component.is value.

I don't see any errors in JS console. The ony one strange thing is te order of components in he Vue dev-tools. Those Person widgets are gropped somewhere near the bottom, instead of being sparse: once in a row..

What would be the next steps to troubleshoot?

19th April, 2017

pilat started a new conversation How To Make Sure/check If A Listener Is Not Queued? • 7 months ago

I need certain listeners to run in "Sync" mode. So that all the data they produce become available to the "client code" immediately after the event were dispatched.

Client code example:

event(new LeadsSynced($payload));

// here I start using something one of listeners produced:
$activities = Activities::where(/* ... */)->all(); 

Listener code:

namespace App\Listeners;

use App\Events\LeadsSynced;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;

class SyncActivities // notice that I don't have "implements ShouldQueue" here
{
//

It looks, however, that the data, which is supposed to be generated by SyncActivities is not available to my client code in the place where I suppose it should be.

Do I miss some other places where it could "go parallel"?

17th April, 2017

pilat left a reply on Vue.js Acces Parent Methods • 7 months ago

The case, like this, when you have a parent, full with useful methods, whom you want to use in various children… I believe it's exactly the time to add a global store, like `vu, into your app.

13th March, 2017

pilat left a reply on Controller Code Cached?? • 8 months ago

@Snapey you are probably right. I'll try to add something with a cache-busting character to the query string.

10th March, 2017

pilat left a reply on Controller Code Cached?? • 9 months ago

pilat left a reply on Controller Code Cached?? • 9 months ago

Don't know... Is there a way to cancel all possible caches for specific controller? (something like 'no-cache middleware')

pilat started a new conversation Controller Code Cached?? • 9 months ago

Hi, I'm trying to debug one controller action on a production web-ste.

I don't return view in this action. I make some heavy lifting (external Api-to-DB sync), dump few things directly into the browser and then make redirect(). When I call the action again — I get my previous output instantly (the sync does not work so fast) and redirected as before.

I add Log::info(...) to the method body -- nothing's added to the log, the behavior is the same. I add dd(...) to the action method body (at the very top!) -- the same!

What happens and how do I prevent this?

    public function updateLeads($subdomain)
    {
        dd(__METHOD__); // EVEN THIS DOES NOT CHANGE A BIT!
        Log::debug(__METHOD__ . " @ $subdomain");
        $this->leadsUpdater->updateCacheFull();
        sleep(2);

        return redirect()->route('sync_index', [$subdomain])
            ->with('top_message', [
                'type' => 'success',
                'text' => 'Кэш сделок обновлен',
            ]);
    }

7th March, 2017

pilat left a reply on Database Queue On Shared Hosting • 9 months ago

A comment on my previous note: the solution has proven not to work if CRON Job is run by sh. With bash it should work.

Another approach, which has finally worked for me:

flock -n /tmp/latavel_queues.lockfile /usr/bin/php /path/to/laravel/artisan queue:listen

Hint: first check if flock is available on your server. I was lucky :-)

20th February, 2017

pilat left a reply on Elixir In Production / Development • 9 months ago

@ejdelmonico actually, I'm doing this for a reason. elixir() would only map asset's filename to the "cache-busted" one, in accordance to manifest file. It won't prefix "web-dir" to the final asset URL, however — this is what `asset does.

So, let say I have a shared hosting with non-changeable server setup that serves HTTP from the $HOME/public_html directory. It can't be re-routed to WHATEVER/public. In this case, I simply put my entire Laravel content into $HOME/public_html and access the site via /public/ URI (instead of just /).

If I use elixir() alone, I'll get assets URIs like /js/myscript-qwe43re.js, which would not be found by browser. asset(elixir()) would do the trick and make the proper URI of /public/js/myscript-qwe43re.js.

Moreover: if I have another copy of site (let say, local copy, served with Valet or Homestead), accessible via web-root / — I don't need to change my code. I just make sure that all local links are made with `url and all assets are referenced with `asset (with or without elixir()) and it handles my current web-dir properly.

13th February, 2017

pilat left a reply on Database Queue On Shared Hosting • 9 months ago

Here's a one-liner to put into your crontab (let it run, let say, every 5 minutes):

cd /path/to/your/project && jobs -l | grep `cat queue.pid` || { nohup /usr/bin/php artisan queue:listen & echo $! > queue.pid; }

two variables here:

  1. /path/to/your/project — your Laravel project root. Effectively, the folder, where php artisan would work;
  2. /usr/bin/php — path to PHP executable on the server (can be found `which p)

The script would check if there is a job with PID, stored in queue.pid file. If No, it would start a new job and write its PID into the file.

31st January, 2017

pilat started a new conversation Join On The First Row Only With Query Builder • 10 months ago

I'd like to "left join" on the first(it's important) row in my project.

With a raw SQL, it would look like:

select * from users join widgets on widgets.id = (
    select id from widgets
    where widgets.user_id = users.id
    order by created_at desc
    limit 1
)

(c) https://www.periscopedata.com/blog/4-ways-to-join-only-the-first-row-in-sql.html

Is it possible to do something like this with Query Builder?

17th January, 2017

pilat left a reply on Elixir In Production / Development • 10 months ago

Not sure if that addresses the issue directly, but I have the following in my view files: {{ asset(elixir('my/resource.ext')) }}

elixir() on a "stripped" file path would find versioned filename, while asset() would add the necessary "/web/folder/" to it.

16th December, 2016

pilat started a new conversation Service Provider Works Off App(), But Not Off A Type-hinting • 11 months ago

Hi, I have a custom-made service provider.

Then, I have a Controller method like this:

public function loadFeature(Request $request, $subdomain)
{
    dd(app(\App\Feature::class));
}

This way it works.

If I change it to this, however:

public function loadMap(Request $request, \App\Feature $feature)
{
    dd($feature);
}

I get ReflectionException in Container.php line 809: Class App\Feature does not exist error.

Why may it happen?

5th December, 2016

pilat left a reply on Can't Figure Out How To "cook Vue The Laravel's Way" In Multi-page App • 1 year ago

@pmall It's already there, in the stock resources/assets/js/bootstrap.js

// resources/assets/js/bootstrap.js

/**
 * Vue is a modern JavaScript library for building interactive web interfaces
 * using reactive data binding and reusable components. Vue's API is clean
 * and simple, leaving you to focus on building your next great project.
 */

window.Vue = require('vue');
require('vue-resource');

But then, in app.js I have the following initialization code:

resources/assets/js/app.js

/**
 * Next, we will create a fresh Vue application instance and attach it to
 * the page. Then, you may begin adding components to this application
 * or customize the JavaScript scaffolding to fit your unique needs.
 */

Vue.config.debug = true;

Vue.component('example', require('./components/Example.vue'));

const app = new Vue({
    el: '#app'
});

^^^ this code is out-of-the-box as well.

And THEN, in the page-specific script I have the following:

// resources/assets/js/feature1.js

Vue.component('feature1', {
  template: '<span>This is a Feature #1</span>'
});

Now, if I leave this as it is, I gives mw the following error: VM2994:2611 [Vue warn]: Unknown custom element: <feature1> - did you register the component correctly? For recursive components, make sure to provide the "name" option. (found in root instance)

I understand there is one of the following issues:

  1. My component is declared after the const app = new Vue() --or--
  2. It has something to do with variables scopes when using modules and Webpack. --or-- .. I dunno

I just thought there must be some "best practices" to do what I want :-)

pilat left a reply on Can't Figure Out How To "cook Vue The Laravel's Way" In Multi-page App • 1 year ago

Well, you still have to import all your components from within that "root" app.js:

Vue.component('Graph', require('./components/Graph.vue'));

This is what I wanted to avoid.

By the way, I've came to understanding that I probably do not need to instantiate that const app = new Vue({...}); thing inside app.php (while I still load jquesry and other staff there). So, I've only instantiate Vue instance on the page where I need Vue:

// feature1.js

Vue.component('filter-bar', {
  template: '<span>This is a filter-bar</span>'
});

const tabel = new Vue({
    el: '#tabel'
});

Still interesting in the proper ways to solve this issue, however.

pilat started a new conversation Can't Figure Out How To "cook Vue The Laravel's Way" In Multi-page App • 1 year ago

Hi, I'd really like to use Laravel's "boilerplate" for the Vue-ish things, but I can't figure out how to organize everything… :-(

Imagine a multi-page application. All of the pages load js/app.js (which is left as it is default in Laravel 5.3) , as there is some commonly needed stuff. Fine with that.

Then I have another page, like js/feature1.js. The page has some JS app, that has to be loaded on this page only. I therefore add another line to the gulpfile.js:

.webpack('app.js')
.webpack('test.js') // the new line

, and then hook the compiled version of this script into appropriate for that page view via <script> tag (I have 'user-js' section just for that).

So, the HTML code looks like this after all:

<script src="http://project.dev/js/app.js"></script>    
<!-- Scripts ('user-js') -->
<script src="http://project.dev/js/feature.js"></script>

Now, in the 2nd file I want to import or define the feature1-specific component(s), like this:

// Define a new component called filter-bar
Vue.component('feature1', {
  template: '<span>This is a feature #1</span>'
});

, and attach these components to the Vue object, created in the app.js --> how would I do that?

Note: I do not fancy creating another Vue instance specifically for this page, nor making conditional loading of the "root" app.js (and then having to repeat all the "bootstrap" in featureXX.js as well).

29th November, 2016

pilat started a new conversation Which Is The Proper Way To Load A Remote Module? • 1 year ago

Hi, I have a need in using a module, which is not in the npm repository. Currently, I load it the ol'good way, via tag:

<script src="https://api-maps.yandex.ru/2.1/?lang=ru_RU" type="text/javascript"></script>

I would like, however, to load it within my, let say, app.js file. Something like:

import ymaps from 'https://api-maps.yandex.ru/2.1/?lang=ru_RU'

Can I do this with elixir/webpack/whatever? What is the proper way?

pilat left a reply on JavaScript Conditional Load? • 1 year ago

I would add @parent call into the 'scripts.footer' section, in order to preserve all other views' "injections":

@section('scripts.footer')

    @parent

    //-- load page specific script/styles for this page.
    <script type="text/javascript" src="......"></script>

@stop```
Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.