Rooduef

Rooduef

Member Since 1 Month Ago

Experience Points
240
Total
Experience

4,760 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed
0
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-in-session Created with Sketch.

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • subscriber Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist Created with Sketch.

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • chatty-cathy Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

Level 1
240 XP
Mar
24
2 weeks ago
Activity icon

Replied to Only One 'active' Model

Thanks, it's pretty fine approach.

But it seems that with this approach I can't get 100% guarantee of uniqueness of active record.

For example:

  1. One of Banners has been successfully updated and marked as active.
  2. Observer starts logic for make all other Banners inactive.
  3. DB connection has been lost.
  4. There is two active Banners in the DB now.

Am I correct?

Activity icon

Started a new Conversation Only One 'active' Model

I have, for example, entity named Banner:

            Table "public.banners"
   Column   |              Type              | 
------------+--------------------------------+
 id         | bigint                         |
 title      | character varying(255)         |
 image      | character varying(255)         |

How can I mark only one of them as active and get only one active record at a point in time with Eloquent?

I think that I can add boolean field named active to the banners table. But the main problem is I don't know how to guarantee that active records will be not more than one at a point in time.

Mar
19
3 weeks ago
Activity icon

Awarded Best Reply on Get Model By Min() One Of Fields Grouped By Another Field

I solved this problem simpler:

$schedules = Schedule::selectRaw('DISTINCT ON (event_id) *')
    ->orderBy('event_id')
    ->orderBy('date')
    ->get();

But, as far as I know, this query will work only with PostgreSQL.

Activity icon

Replied to Get Model By Min() One Of Fields Grouped By Another Field

I solved this problem simpler:

$schedules = Schedule::selectRaw('DISTINCT ON (event_id) *')
    ->orderBy('event_id')
    ->orderBy('date')
    ->get();

But, as far as I know, this query will work only with PostgreSQL.

Mar
14
3 weeks ago
Activity icon

Replied to Get Model By Min() One Of Fields Grouped By Another Field

I tried to do this:

$s1 = \DB::table('schedules')
    ->selectRaw('event_id, min(datetime) as min_date')
    ->groupBy('event_id');

$s = \DB::table('schedules as s')
    ->joinSub($s1, 's1', function (Builder $join) {
        $join->on('s.event_id', '=', 's1.event_id');
    })
    ->where('s.datetime', '=', 's1.min_date')
    ->get('s.*');

But got the error:

Illuminate\Database\QueryException : SQLSTATE[22007]: Invalid datetime format: 7 ERROR: invalid input syntax for type timestamp: "s1.min_date"

Activity icon

Replied to Get Model By Min() One Of Fields Grouped By Another Field

Thank you, but I see that you join another table while I need to join a result of subquery on the same table. Sorry, but I can't imagine how I can apply your approach to my problem.

Can you give a deeper explanation?

Activity icon

Started a new Conversation Get Model By Min() One Of Fields Grouped By Another Field

Hi everyone.

Please help me to solve the next problem. I have two tables in the database - events and schedules.

Their schemes:

              Table "public.events"
       Column       |              Type              |                  
--------------------+--------------------------------+
 id                 | bigint                         |
 name               | character varying(255)         |
 description        | text                           |
 duration           | character varying(255)         |
 created_at         | timestamp(0) without time zone |
 updated_at         | timestamp(0) without time zone |



              Table "public.schedules"
      Column      |              Type              | 
------------------+--------------------------------+
 id               | bigint                         |
 event_id         | integer                        |
 date             | timestamp(0) without time zone |
 price            | character varying(255)         |
 created_at       | timestamp(0) without time zone |
 updated_at       | timestamp(0) without time zone |

schedules.event_id is the foreign key references on events.id

I need to fetch the nearest schedule for each event. With raw SQL I can do it with this query:

SELECT s.*
FROM schedules rs
INNER JOIN
  (SELECT event_id,
          min(date) min_date
   FROM schedules
   GROUP BY event_id
   ) s1 ON s1.event_id = s.event_id
WHERE s1.min_date = s.date

But how can I do this with Eloquent?

Thanks for help.

Feb
15
1 month ago
Activity icon

Replied to Laravel 5.8 Interface Binding While Running Tests

It should be Tests\TestCase.

Hm... The reason is that I extended my test class with PHPUnit\Framework\TestCase instead of Tests\TestCase. Now I extend my class from Tests\TestCase and it got the desired result.

But now I don't understand which and why parent class I should use :) And is out of this topic. I must research it independently. But I would be grateful if you give me some explanation about this.

Anyway, you gave me some useful and non-evident information and save a lot of my time. Thank you.

Activity icon

Replied to Laravel 5.8 Interface Binding While Running Tests

Have you added parent::setUp(); at the top of setUp method?

Yep. That's my setUp() method:

public function setUp()
    {
        parent::setUp();

        $this->setUpHttpMock();

        $this->app-bind(ApiConfigurationInterface::class, FakeApiConfiguration::class);

        $this->apiClient = $this->app->make(ApiClient::class);
    }

Both does the same thing, app() is just a global helper to access application instance.

I think that the clear way is don't use global helper... But I can't call it via $this object.

Activity icon

Replied to Laravel 5.8 Interface Binding While Running Tests

Thank you so much!

I didn't even think that I can perform bindings in setUp() method :)

After I bind FakeApiConfiguration::class to ApiConfigurationInterface at setUp() method, it wors fine.

But instead of $this->app->bind(...); I perform binding like this: app()->bind(...);. Because first approach ends with Error : Call to undefined function Tests\Unit\ApiClient\bind(). Can you also tell why it is and which approach is proper?

Thank you again!

Activity icon

Replied to Laravel 5.8 Interface Binding While Running Tests

Sorry, It just a typo while I was formatting post. Real code does not contain it.

Activity icon

Started a new Conversation Laravel 5.8 Interface Binding While Running Tests

Hi everyone.

I'm stuck with the next problem.

I writing an external API client. It depends on the configuration interface that provides some configuration parameters like endpoints, tokens, etc.

API client class:

class ApiClient
{
    private $apiConfiguration;

    public function __construct(ApiConfigurationInterface $apiConfiguration)
    {
        $this->apiConfiguration = $apiConfiguration;
    }

API configuration interface:

interface ApiConfigurationInterface
{
    public function getToken();
    public function getEndpoint();
    public function getProxy();
}

In the service provider I bind appropriate class to this interface:

    public function register()
    {
        $this->app->bind(ApiConfigurationInterface::class, ApiConfiguration::class);
    }

And it works fine.

Also, I wrote some tests for my API client. In the tests I would like to substitute production configuration class to stub class that also implements configuration interface. This will enable me to provide API client fake parameters and API client will use mock external API providing directly in tests.

For achieve it, I added the condition to service provider that checks environment and binds different configuration interface implementations:

    public function register()
    {
        if ($this->app->environment('testing')) {
            $this->app->bind(ApiConfigurationInterface::class, FakeApiConfiguration::class);
        } else {
            $this->app->bind(ApiConfigurationInterface::class, ApiConfiguration::class);
        }
    }

That's fake configuration class:

class FakeApiConfiguration implements ApiConfigurationInterface
{
    public function getToken()
    {
        return 'token';
    }

    public function getEndpoint()
    {
        return 'http://localhost:9999/endpoint';
    }

    public function getProxy()
    {
        return null;
    }
}

In the test class I use service container to inject API client.

class ApiClientTest extends TestCase
{
    use HttpMockTrait;

    private $apiClient;

    ...

    public function setUp()
    {
        $this->setUpHttpMock();

        app()->make(ApiClient::class);

    }
}

And that's the problem: when I try to perform tests, PHPUnit fails with next messag:

Illuminate\Contracts\Container\BindingResolutionException : Target [App\Services\ApiClients\ApiClient\Configuration\ApiConfigurationInterface] is not instantiable while building [App\Services\ApiClients\ApiClient\ApiClient].

I tried to delete conditions from service provider to check if they are the source of the problem:

    public function register()
    {
        $this->app->bind(ApiConfigurationInterface::class, FakeApiConfiguration::class);
    }

But I have no changes. PHPUnit message is the same.

I tried also dirty trick: create TestClass instance in the test controller and call test from here. And it works without errors.

class TestController extends Controller
{
    public function test()
    {
        $test = new ApiClientTest();
        ApiClientTest::setUpBeforeClass();
        $test->setUp();
        $test->someTest();
    }
}

I cannot understand why service container does not bind and instantiate appropriate class to API configuration interface while running tests. I spent a lot of time to solve that problem. But could not find a resolution.

Please help to solve it.