chrmpt

chrmpt

Member Since 1 Week Ago

Experience Points
1,130
Total
Experience

3,870 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
9
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
1,130 XP
Sep
16
1 week ago
Activity icon

Replied to OnClick Execute Function

Thank you all, just found the right solution! Special Thanks to @ismaile : It was indeed a problem of the ids:

I now pass the ids and search inside the controller for the corresponding models. When I then attach the labels to the member, it does it perfectly.

Thanks again. What an awesome community!

Activity icon

Replied to OnClick Execute Function

I now tried it by passing the ids and then getting the Label and the Member by id before i perform the attach method. It still does nothing.

Activity icon

Replied to OnClick Execute Function

member and label are both models that have a many-to-many relationship. Therefore, both models have the id property accessible via $member->id and $label->id.

I pass the whole 'Model' within the function, because it seems to be best practice to set up a many-to-many relationship by entering

$member->labels()->attach($label);

where 'labels()' is defined as

public function labels() {
        return $this->belongsToMany(Label::class);
    }

In the background, Laravel automatically assumes, that label has a property called 'label_id' and member a property called 'member_id' which is exactly the case.

If I take a look back at my function in the controller, specifically these lines...

$id = $validatedData['member']->id;

return redirect('/members/' . $id)->with('success', 'Label added/removed successfully');

... I think this proves that the id is passed correctly since it redirects correctly. Furthermore, When I access the Model Function directly, like I used to, then it alternates between adding all available Labels and removing all the set Labels. Which is not useful but it shows, the function does what it's supposed to.

Or did I miss something here? Please forgive me for asking stupid questions, still a Laravel newbie..

Activity icon

Replied to OnClick Execute Function

Okay this is driving me nuts. There must be some mistake I just can't see. Thank you so much @ismaile . You have been a great help so far.

Now, what i did:

  • I added the following function to my MemberController:
// MemberController.php
public function attachDetachLabel(Request $request) {
    $validatedData = $request->validate([
        'label' => 'required',
        'member' => 'required'
    ]);

    Member::attachDetachLabel($validatedData['member'], $validatedData['label']);
    $id = $validatedData['member']->id;

    return redirect('/members/' . $id)->with('success', 'Label added successfully');
}
  • I defined the attachDetachLabel function in the Member Model as follows:
// Member.php
public function attachDetachLabel($member, $label) {
    $alreadyExists = $this->labels()->where('label_id', $label->id)->exists();

    if ($alreadyExists) {
        $member->labels()->detach($label);
    } else {
        $member->labels()->attach($label);
    }
}
  • I defined the post route in my web.php:
// web.php
...
Route::post('members', '[email protected]')->name('members.attachDetachLabel');
  • I adjusted the view as follows (surprisingly without any CSS issues):
// view.blade.php
<div class="dropdown-menu">
    @foreach($labels as $label)
        <form action="{{ action('[email protected]') }}" method="POST">
            @csrf
            <input type="hidden" name="label_id" value="{{ $label }}">
            <input type="hidden" name="member_id" value="{{ $member }}">
            <button type="submit" class="dropdown-item">{{ $label->name }}</button>
        </form>
    @endforeach
</div>

When I now select a Label, It (correctly) reloads the view, but it has not performed the attachment / detachment. Do you know why that is?

Sep
15
1 week ago
Activity icon

Replied to OnClick Execute Function

Nope. Just using Laravel 7 - no VueJs or Livewire

Activity icon

Started a new Conversation OnClick Execute Function

Hi Guys, hope you can help me.

I have a tool where i can manage Member Data. Each Member can have some Tags (I call them Labels).

Now I have a dropdown with all the available labels. If I click on one Label, I want to add this Label to the Member. I already have a function in my MemberModel to do that.

Here is my Approach:

// view.blade.php
...
<div class="dropdown-menu">
    @foreach($labels as $label)
         <a class="dropdown-item" href="" onclick="{{ ($member->attachLabel($member, $label)) }}">{{ $label->name }}</a>
    @endforeach
</div>

However, with this approach, the function gets executed for all the dropdown items - and i don't even have to click at them. Whenever I reload the page, the current member gets assigned all the available Labels.

How can I change it to only execute the Model Function when I click on the button?

thanks already for your help. I have been searching the internet already for that, couldn't find anything that helped.

Activity icon

Replied to Accessing Multiple DB Tables From One View Indeplendently

Never mind i got it working. had to define the many-to-many relations inside the model, not the controller. Thanks again for your help. Appreciate your feedback

Activity icon

Replied to Accessing Multiple DB Tables From One View Indeplendently

Thanks a lot @jlrdw . Exactly what I needed.

In fact, Member and Label do have a many-to-many relationship in my example. Already had set up almost everything correctly.

If I may ask a stupid question?

I now have in my MemberController the following function:

// MemberController.php

public function labels() {
    return $this->belongsToMany(Label::class);
}

How can I use this in my Case? I have trouble in two cases:

  1. show all the assigned Labels of the current Member
//  view.blade.php
...
@forelse($member->labels as $label)
	<a href="#" class="tag tag-rounded tag-green">{{ $label->name }}</a>
@empty
	-
@endforelse
  1. Add the functionality to add a Label wit hthe Click of the button (dropdown-button in this case
// view.blade.php
...
<div class="dropdown-menu">
    @foreach($labels as $label)
        <a class="dropdown-item" href="{{ $member->labels()->attach($label) }}">{{ $label->name }}</a>
    @endforeach
</div>

Both does not work. Why do i get the error, that labels is unknown?

Activity icon

Started a new Conversation Accessing Multiple DB Tables From One View Indeplendently

Hey Guys

I have a question about the correct procedure in Laravel 7 to address the following Problem:

I have a view which shows data from different Tables in my DB:

  • User Data from Table Members
  • Tags associated to the specific Member from Table MemberLabels

It was easy to show both of that in my View. I had a model for Member and for MemberLabel. In my Membercontroller I forward both, Member data and associated Labels:

public function show($id)
    {
       ...
        
        return view('detail', compact(['member', 'labels']));
    }

This works fine for me. But is it also the recommended way to do it?

And now my question: Showing data from several DB-tables now works. But how can I manipulate different DB-tables from one view?

E.G. I have the edit Screen where I can edit the member details. That part of course gets handled by the update method inside the Membercontroller. But on that same view I have the option to add a label. This would I like to handle with the LabelController. But is this the right way? And if so, how?

Concrete:

  • How should I access different (update) Functions from different Controllers independently just from one view?
Sep
14
1 week ago
Activity icon

Replied to How To Merge Input Fields Inside The Controller

Wow, thanks a lot for your fast answer. Truly appreciate it!

Activity icon

Started a new Conversation How To Merge Input Fields Inside The Controller

Hey Guys

Glad to join you. Even though I am a complete novice to Laravel, I already am a huge fan.

I have a question about the update function in my Members Controller (using Laravel 7):

This function worked fine for me. But now, I have three separated drop down menus per Date Input (one for day, one for month, one for year). How can I combine these three values (yyyy-mm-dd) to pass the value to the db?

My update function


public function update(Request $request, $id)
    {
        $validatedData = $request->validate([
            'member_number' => 'numeric|nullable',
            'first_name' => 'required|max:255',
            'last_name' => 'required|max:255',
            'street' => 'required|max:255',
            'zip' => 'required|numeric',
            'place' => 'required|max:255',
            'birthdate' => 'required',
            'phone_home' => 'numeric|nullable',
            'phone_mobile' => 'numeric|nullable',
            'email' => 'max:255',
            'membership_fee' => 'nullable',
            'liable_to_fee' => '',
            'annual_mailing' => '',
            'newsletter' => '',
            'magazine' => '',
        ]);


        Member::whereId($id)->update($validatedData);

        return redirect('/members/' . $id)->with('success', 'Member edited successfully');

}

"birthdate" and "membership_fee" are both dates in my mysql database. When I tested my Application, I first had just two input fields named just like that. However, the dropdowns are much more convenient. So in my blade, there is no "birthdate" and "membership_fee" field anymore, but

  • birthdate_day

  • birthdate_month

  • birthdate_year

  • membership_fee_day

  • membership_fee_month

  • membership_fee_year

How can I pass these three values and combine together to pass them as one single birthday and one single membership_fee value to the db?

Thank you already for your support. I have searched the internet like crazy to find a solution.