Charrua

Charrua

Owner at Charrua

Member Since 2 Years Ago

A Coruña

Experience Points
10,530
Total
Experience

4,470 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,530 XP
Sep
08
1 month ago
Activity icon

Replied to TDD How Do You Test Backend And Frontend

@martinbean you are right, my differentiation on using the words “backend” and “frontend” has nothing to do here, it was a mental map I have from the app. I was thinking on user roles when referring to those.

Thank you @martinbean and @tray2 for the answers and help on this issue!

Sep
07
1 month ago
Activity icon

Replied to TDD How Do You Test Backend And Frontend

Great information, and do you create separate tests for user roles?

For example if you have 3 different users with different permissions using policies, do you write 3 tests for the same feature?

Thank you.

Sep
06
1 month ago
Activity icon

Started a new Conversation TDD How Do You Test Backend And Frontend

Hi, I'm new to TDD. As I'm developing my app at the moment I'm only using phpunit with assertions and HTTP tests.

At this moment I'm testing features, functionalities and some information that is rendering on blade templates with HTTP tests.

Do you use this tests for what's rendering on the browser, like HTTP tests or do you write separate tests in another package like Laravel Dusk?

I wanted to find some guide on what to test and how to test it. For example, use phpunit to test features and use dusk to test frontend and browser...

Sep
04
1 month ago
Activity icon

Replied to Restrict Models And Relations Based On Prop 'published_at'

Thank you for your answer, all I need was some orientation :)

As always, thank you again for your contribution to the Laravel community.

I have wrote for guests users, when showing courses the policy method isPublished:

public function isPublished(?User $user, Course $course)
{
    return $course->published_at
            ? Response::allow()
            : Response::deny(‘I’m sorry, you can’t browse unpublished courses.');
}

And used in my controller as:

public function showPresentation(Course $course)
{
    $this->authorize('isPublished', $course);

    return view('front.courses.show-presentation', compact('course'));
}

public function showContent(Course $course)
{
    $this->authorize('isPublished', $course);
        
    return view('front.courses.show-content', compact('course'));
}
Sep
03
1 month ago
Activity icon

Started a new Conversation Restrict Models And Relations Based On Prop 'published_at'

Hello, I have a simple app of courses. Those courses have units and those units have lessons.

The relations are: Course → has many units → has many lessons

The Course model has a published_at prop. The whole purpose is that student users can’t access to the show view of any course, unit or lesson if the course is not published. Only admin users can.

Thinking on what would be the right place to place this restriction/check I came with some (bad) ideas:

I was thinking on writing a policy method for the three models checking first if he course is published but it does not seem the right place for me... What do you think on this one?

Other idea was to check in each controller on the show method and if the course is not published, redirect to some other route... But seems a lot of code.

What would be the right place to place the restriction/check?

Aug
20
2 months ago
Activity icon

Replied to Testing Image Upload And Validation Failure

I have taken this code from the docs, please see https://laravel.com/docs/7.x/http-tests#testing-file-uploads

Maybe I’m understanding it wrong?

Aug
17
2 months ago
Activity icon

Started a new Conversation Testing Image Upload And Validation Failure

Hello, I’m trying to test file uploads inside a controller and validate it.

The problem is that validation is failing for image type and seems that the only data is getting onto the controller is the image name...

From the docs I can see

use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class ExampleTest extends TestCase
{
    public function testAvatarUpload()
    {
        Storage::fake('courses');

        $file = UploadedFile::fake()->image('avatar.jpg');

        $response = $this->json('POST', '/courses', [
            'image' => $file,
        ]);

        // Assert the file was stored...
        Storage::disk('courses')->assertExists($file->hashName());
    }
}

Controller

public function store(Request $request)
{
        $request->validate([
            'image' => 'mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);
   
        $image_path = $request->file('image')->store('courses');

        $course = new Course;
        $course->image_path = $image_path;
              
        return view('courses.show', compact('course'));
}

Any ideas on what I’m doing wrong? It seems that the image object is not reaching the controller. Thank you.

Jul
16
3 months ago
Activity icon

Started a new Conversation HasOneTrough / HasManyTrough Relations

Hello, I’m trying to get a model through other models and tables.

Users → hasMany (one to many) → Enrollments → belongsToMany (many to many) → Courses

I wanted to get the courses that the user has enrollments. Something like $user->courses

I tried both HasOneTrough and HasManyTrough but I can’t figure it out, maybe I don’t need to use this and use another package called has-many-deep I’ve found, what do you think?

Also it would be great to get the inverse, for example to know the users that have enrollments to the course, something like $course->users

Thank you.

Jun
01
4 months 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
5 months 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 ;)

May
21
5 months ago
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.

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
5 months 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
5 months 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
5 months 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?