Charrua

Charrua

Owner at Charrua

Member Since 2 Years Ago

A Coruña

Experience Points
10,290
Total
Experience

4,710 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
54
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 3
10,290 XP
Jun
01
1 month ago
Activity icon

Replied to How Do You Manipulate Data Before Sending It To The View?

Hello @michaloravec, thank you for your answer, but I'm looking for something more of a structure I think. The select method you expose, allows to select only some relation fields?

I'm injecting the model instance on the controller method and manipulating data, let's see an example.

public function createLabels(Testing $testing)
{
        $testing->load('members.new_rank', 'members.program', 'other_relation');
	
	// manipulate some props
	$testing->new_prop = 1;
	$testing->other_prop = 1 + 2;

        return view('labels.create', compact('$testing'));
}

Remember that my goal is to select what data or props are passed to the view and not all the model fields and relation fields as they are unnecessary. Thank you.

Activity icon

Started a new Conversation How Do You Manipulate Data Before Sending It To The View?

Hello, I'm dealing with passing data into the view, but no all model data and relations fields.

In my controller I have my model querying also for relationships, there is also some easy calculation going on some fields.

My problem is that I don't want to send all model data into the view as this has no sense... Let's say, for the 50 fields I have access, I only need 10 in the view.

So what are the options? Maybe create an array and pass only the data needed? Is there another way?

I was thinking on using View Models as described here. But the same question is happening, what do I pass to the view? The model itself with the 50 fields, an array created with only the data needed, or maybe an object, some sort of structure? Any ideas?

May
22
1 month ago
Activity icon

Replied to Making My Controllers Easy To Understand, Where To Put Code?

@snapey I need to tell you have blown my mind! That was the type of information I was looking for. Seems too complex for me at this moment but I will study the patterns and apply some to clear my controllers and models.

Thank you!

p.d. I'm on crisis haha ;)

Activity icon

Replied to Making My Controllers Easy To Understand, Where To Put Code?

@snapey thank you again for your help on this one!

Can you share with me where I can find solid foundation on what belongs to a model or to a controller? So maybe I can better understand where to put my code.

It's clear to me now that a job can make this task and delegate the task of sending ´Exam´ to students.

May
21
1 month ago
Activity icon

Replied to Making My Controllers Easy To Understand, Where To Put Code?

Hey @snapey thank you for the answer.

When I wrote this I've made a mistake, that's why I insist on using a method like $exam->sendInvitations

Let me try to explain it, after a teacher creates the Exam and attach some students to it, he/she will fire manually those notifications, that's a manual action.

Then, when the Exam date approaches, there is one more notification. This is an automatic notification. This notification will be sent by a scheduled task, that then sends a job as you said. We both agree on the automatic action of sending those notifications.

But my problem is still on the manual action of sending the first Exam notifications.

Activity icon

Replied to Making My Controllers Easy To Understand, Where To Put Code?

@michaloravec @snapey @dondasasmita thank you all for answering.

So I see some different options here,

  1. extract the task to an event and listen for that event
  2. set a job to process the task and
  3. create a method on the Exam model
  4. create a model ExamInvitation and a method on that model

Maybe in this particular case I found correct to extract the task to an event or maybe a job. But I have found me on this situation all the time, where do I place the code for this or that task, is the model a good place?

I wanted to make it simple for reading and I would love something like $exam->sendInvitations() maybe placing that method on the Exam model and make the method fire an event or job.

This last seems right to you? And an approach to use in general?

Thank you so much again for your help and time!

May
20
1 month ago
Activity icon

Started a new Conversation Making My Controllers Easy To Understand, Where To Put Code?

Hello, I'm trying to make my controllers have less code and be really easy to read. Let's go to a real example, I have an a school system that has members and those members have exams.

Sometimes the members pre-paid exams, so when the exam date is coming I need to send an email for those members who have not paid exam with payment options and another email with the invitation for those who have already payed.

My controller on my ExamController looks like this:

public function sendNotifications(Request $request)
    {

        $exam = Exam::with('members')->findOrFail($request->id);
        $members = $exam->members;

        $members_to_charge = $members->filter(function ($member) {
            return $member->needsToPayExam();
        });
        Notification::send($members_to_charge, new SendPaymentInvitation($exam));

        $members_to_invite = $members->diff($members_to_charge);
        Notification::send($members_to_invite, new SendInvitation($exam));

        $exam->invitations_sent = 1;
        $exam->save();
        
        return response('Sent', 200);
    }

I would love to reduce this to something like:

public function sendNotifications(Request $request)
    {

        $exam = Exam::with('members')->findOrFail($request->id);
        $exam->sendInvitations($exam->members);

        $exam->invitations_sent = 1;
        $exam->save();
        
        return response('Sent', 200);
    }

My question is where do I need to place the code and method sendInvitations() the Exam model will be a correct place to do it? Let's imagine I have 15 more methods like this one, is the Exam model still the good place to do it?

Thank you

May
19
1 month ago
Activity icon

Started a new Conversation Testing Basics, Unit Testing Method For Boolean

Hello, I'm starting to write a unit test for a method.

My method returns a boolean value, I can config world data and pass it to the method and expect an assertTrue. But is a good practice to also config some data for the method to return the opposite and assertFalse? So I'll be checking both cases.

For example my method on the Member model checks if a member has a program name of Basic program associated.

public function hasBasicProgram()
{
    $has_program = ($this->program_name == 'Basic program') ? true : false;

    return $has_program;
}

And my test method creates a member with a program_name of Basic program, so the test passes. But how I can test the inverse? I need to create a new method? I wanted to test if the hasBasicProgram() method returns false if the member that I check has no program_name of Basic program.

This is my test method

public function test_member_has_basic_program()
{    
    $member = factory(Member::class)->create(['program_name' => 'Basic program']);

    $this->assertTrue($member-> hasBasicProgram());
}
Activity icon

Replied to New To PhpUnit Tests, Need Help With Repeating Factories

Great answer! You helped me so much!

I have still a question, on the last part, when using factory-callbacks:

A Member can exist on the system without a Transaction but the inverse can't.

So my logic is telling me that I need to create first the member and then the transaction, where should I place the factory-callback? On the member factory?

$factory->afterCreating(App\Member::class, function ($user, $faker) {
    $user->transactions()->save(factory(App\Transaction::class)->make());
});

This sounds good for you?

Activity icon

Started a new Conversation New To PhpUnit Tests, Need Help With Repeating Factories

I'm new to writing tests and I have started with an application that's already running and I have a few questions:

I have my model Member with member data stored, that member has many Transaction and also many Testing (those are exams).

When writing unit tests for the member, on all tests I have to create the member, I see me repeating all the time some code.. Is this approach correct? Because when testing other parts maybe I have to create a member also... Is there a place to put code and reuse it?

Maybe instead of creating and repeating the member I can use a method inside testing methods that return always the same member. Is this a good idea or practice?

Do you use comments inside your unit testing methods or on the method header?

My unit test for a specific feature is looking like this but I see that's no so reading friendly. I wanted to simplify this.

/**
     * Member has pre paid testing corresponing with testing new rank?
     *
     * @return void
     */
    public function test_has_pre_paid_testing_corresponding_with_new_testing_rank()
    {
        $member = factory(Member::class)->create(['id' => '1']);
        $transaction = factory(Transaction::class)->create(['reason_id' => 10, 'member_id' => $member->id, 'school_id' => $member->school_id]); 
        $testing = factory(Testing::class)->create(['id' => '1', 'school_id' => $member->school_id]);        
        $testing->members()->attach($member->id);

        $this->assertEquals(1, $member->id);
        $this->assertEquals(10, $member->transactions()->first()->reason_id);
        $this->assertEquals($member->school_id, $testing->school_id);
        $this->assertEquals($member->id, $testing->members()->first()->id);
    }
May
18
1 month ago
Activity icon

Replied to Database Table Design, Add New Column For A New Feature?

Hello @michaloravec, I have thought on polymorphic relationships but this won't solve the data I need to store related exclusively to the event or the concert transaction fields.

The polymorphic approach would solve relating a Transaction with an Event but not on storing transaction related data with the event, being different on data saved for a concert.

All transactions will share some fields as total, date... But there is some data related exclusively to an event or a concert and is different between entities. (event and concert are examples)

Activity icon

Started a new Conversation Database Table Design, Add New Column For A New Feature?

Hello, I'm struggling with a problem on where to add some data on a database and how to relate it.

I have a Members and Transactions tables for money transactions associated with members. Each transaction has one transaction_type, easy for now.

The problem is that I need to relate other entities to those transactions, for example an Event could be related to a transaction or maybe a Concert.

My approach will be to add an extra column to the Transactions table with event_id and concert_id. But this design doesn't seem to be good enough, because in the future if new entities will be created, I can't add columns and columns to the table. Another issue is that I need to add some extra columns related with the event or the concert, maybe a concert will have a number of tickets bought and an event may have any other related info.

It seems to me that is wrong to add those columns to the database, because if I have 1000 records and only 5 records are for an event, the 995 will have a column unused... I think my problem is self explanatory.

Any ideas? What would you do to solve this?

Apr
20
2 months ago
Activity icon

Replied to Get Only Related Models

Great answer, the solution as you've said was to use a many to many relationship.

Thank you @oussama.tn

Apr
19
2 months ago
Activity icon

Started a new Conversation Get Only Related Models

Hello I have 3 models SchoolMember, TestingCandidate and Testing.

So each TestingCandidate belongs to a Testing and a SchoolMember.

My problem is when querying the TestingCandidates of a particular Testing, I wanted to get a collection of SchoolMember instead of TestingCandidates for sending them notifications.

I can't see a clean solution, any ideas?

At the moment I'm using

// get testing with candidates
$testing = \App\Testing::with('candidates')->findOrFail($id);

// my candidate list is here
$candidate_list = $testing->candidates;
Apr
07
3 months ago
Activity icon

Started a new Conversation NotificationSent Listener Returning No Response But SMS Is Sent

Hello, I'm sending SMS notifications to around 300 users using Twilio and using laravel-notification-channels/twilio. Each notification is sent through a queued job to defer the task from the app.

For logging purposes I have created a NotificationSent event listener, this event is fired when a notification is sent, and provide some information such as the $event->response from Twilio inside an object.

The main issue is that sometimes, I get the $event->response object NULL, but the SMS is sent on Twilio, it seems that for some reason I can get the response. I don't know if this is some timeout that have the job or what could be happening.

I have found that laravel-notification-channels/twilio uses the twilio/twilio-php package, this last, uses cURL to send the HTTP requests to Twilio, and the default timeout on the CurlClient is 60 seconds. Saying that, I don't know if the job gets this timeout... I'm out of ideas.

Mar
30
3 months ago
Activity icon

Awarded Best Reply on Send Notifications To Primary And Secondary Email

I have resolved this issue using the cc method on Mailable:

public function toMail($notifiable){
    return (new Mailable($this->message))->to($notifiable->email)->cc('[email protected]');
}
Activity icon

Replied to Send Notifications To Primary And Secondary Email

I have resolved this issue using the cc method on Mailable:

public function toMail($notifiable){
    return (new Mailable($this->message))->to($notifiable->email)->cc('[email protected]');
}
Activity icon

Started a new Conversation Send Notifications To Primary And Secondary Email

Hello, I'm trying to send email notifications to primary email and a copy to a secondary email. This notifications fire in batches of 1000 users. They are general notifications.

For sending notifications I'm using the Notification facade:

Notification::send($users, new Announcement($message));

And on the notification I'm using the toMail method:

public function toMail($notifiable){
    return (new Mailable($this->message))->to($notifiable->email);
}

Any ideas on how to solve this issue? Thank you.

Mar
24
3 months ago
Activity icon

Started a new Conversation Laravel Passport: When To Use Personal Access Tokens?

Hello, I wanted to deeper understand when to use Personal Access Tokens on Passport.

From Laravel docs:

Sometimes, your users may want to issue access tokens to themselves without going through the typical authorization code redirect flow. Allowing users to issue tokens to themselves via your application's UI can be useful for allowing users to experiment with your API or may serve as a simpler approach to issuing access tokens in general.

I understand that the first purpose is for testing or experimenting with the API, but the second "simpler approach to issuing access tokens in general" why would I need to issue an access token if not for testing or experimenting?

If I wanted to consume an API trough an SPA, this approach is good enough? What would be the weak points?

Thank you

Mar
09
3 months ago
Activity icon

Started a new Conversation Processing 2500 Images With Jobs

Hello, I need to download and process 2500 images (resize, add background, etc) and save them for late use.

The current scenario is:

1- using guzzle, firing 10-20 concurrent requests and saving images to local disk and saving them to database.

2- If they are saved to database, I add a job to the queue for processing each image.

3- The job process the image using the Intervention Image and save it to another location on disk.

The main problem is that I need to download them first and then process, so double space on disk and time is needed.

I was thinking on another way and was when I get the response of the image (when requesting) send the image raw content directly to the job queue to process the image. The problem here is that the jobs table gets around 600mb (this is obvious as I'm storing the images on a string) for later processing.

Other idea was to put the request of each image inside a job. Make the job do the request and process the image, only storing the processed new image. Also if the image is stored, add the value to the database. This last idea is the only one I haven't tried.

I started this project using no jobs at all, and processing the images when I get the response, but this failed many times, so I started using jobs and no more problems at all.

Any ideas on how to improve this workflow? Thank you.