Cisaia

Cisaia

Member Since 3 Years Ago

Experience Points
5,360
Total
Experience

4,640 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
52
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 2
5,360 XP
Sep
02
1 month ago
Activity icon

Replied to HasManyThrough Relationship With Data From Intermediate Table

Thank you @marianomoreyra ! That was exactly right. It worked like a charm. Im not quite sure why exactly I assumed I needed a hasManyThrough, but I changed it up to a manyToMany (belongsToMany), and everything is working like it should!

Activity icon

Started a new Conversation HasManyThrough Relationship With Data From Intermediate Table

Hello! I am having a problem a with hasManyThrough relationship. I got it to work relatively easily, but now I would like the final product, the collection of items assigned to a product, to include the id and order fields of my intermediate table.

I have three tables

  • products

    • id
  • product_item_rel

    • id
    • product_id
    • item_id
    • order
  • product_items

    • id
    • a bunch of other information

I have created a hasManyThrough relationship on my products model

public function items()
{
    return $this->hasManyThrough(
        'App\Models\ProductItemModels\product_items',               // name of the final model
        'App\Models\ProductItemModels\product_item_rel',            // name of the intermediate model
        'product_id',                                                  // foreign key on the intermediate mode
        'id',                                                       // foreign key on the final model
        'id',                                                       // local key
        'item_id'                                                // local key of the intermediate model
    );
}

This all works great, and using $product->items, I can get all assigned items to my product

The problem I am running into, is that I want two fields from my intermediate table (id, order) to be included in the final collection. Is there a way to rewrite the hasManyThrough relationship to include these two fields from the INTERMEDIATE table?

The reason I need this is

  1. I would like to be able to delete the connection between product and product_item by referencing the connecting product_item_rel id
  2. I would like to be able to order the items based off the "order" field on the product_item_rel
Aug
24
2 months ago
Activity icon

Replied to Simple Conditional Relationship In Model

My bad, that is confusing. Those are the comm_main types. Different altogether than the comm_people types. What i need to do is in one swoop, get all the comm_mains, with their related comm_people, with their related profiles.

Activity icon

Replied to Simple Conditional Relationship In Model

@ravish I wanted to see if maybe $this->type wasnt what I was expecting it was ( a simple 0 or 1) so I made the method simply return $this->type, and that gave me an error "Call to a member function addEagerConstraints() on null"

Activity icon

Replied to Simple Conditional Relationship In Model

@ravish Same thing with your switch. It will return any comm_people that have a type of 0 with their userProfile, but null for anyone with 1. I checked again to make sure that the contactProfile relationship works, and it does. For some reason it is only reading 0, and ignoring or not understanding anything else

Activity icon

Replied to Simple Conditional Relationship In Model

@ravish No, it doesnt. I tried it with a comm_people record with a type of 1, and it returned a null userProfile. It should have returned a complete contactProfile

Activity icon

Replied to Simple Conditional Relationship In Model

Thank you for your response. Yes, I reviewed that laracast when i first had this problem. I tried to implement their solution, but got the following error "Call to undefined method Illuminate\Database\Eloquent\Builder::addEagerConstraints()". Tried to rework the scopeProfile, and continued to get that error. Thats when I tried to do the switch or if statement. I just used your coe, and again gat the same error, "Call to undefined method Illuminate\Database\Eloquent\Builder::addEagerConstraints()"

Activity icon

Started a new Conversation Simple Conditional Relationship In Model

I have 4 models

comm_main

  • id

comm_people

  • id
  • comm_id (connects up to 50 comm_people to comm_main);
  • type (0 = user, 1 = contact);
  • people_id (the id of either the user or contact);

contacts

  • id

users

  • id

I am trying to pull the user or contact record based on the type and people_id in comm_people

comm_main model

public function people()
{
    return $this->hasMany( 'App\Models\CommModels\comm_people', 'comm_id', 'id');
}

comm_people model

public function userProfile()
{
    return $this->hasOne('App\User', 'id', 'people_id');
}    

public function contactProfile()
{
    return $this->hasOne('App\Models\ContactModels\contact_main', 'id', 'people_id');
}

public function peepProfile()
{
    return $this->type == 0 ? $this->userProfile() : $this->contactProfile();
}

in controller

public function getOppCommunicationsByOppId($id)
{

    $types = [3,4];
    $comms = comm_main::whereIn('type', $types)
                        ->where('parent_id', '=', $id)
                        ->with(['people', 'people.peepProfile'])
                        ->get();
 
    $comm = $comms;

    return response()->json([ 'success' => true, 'url' => $url, 'src' => $src, 'data' => $comm ]); 
}

However, this always only gives me the record if type is 0. Meaning if the comm_people is a user (0), I get their user record. If the comm_people is contact (1), I get null. I understand that this might be a use-case for polymorphic, but I really dont want to, as Im not supposed to make any changes to the preexisting tables. I dont understand why this wont work as it seems so simple. Even if you cant help with fixing it, could someone please explain why it doesnt work?

I have also tried

public function peepProfile()
{
    $a = $this->type;

    if($a == 1) {
        return $this->contactProfile();
    }
    return $this->userProfile();
}

and

public function peepProfile()
{
   $a = $this->type;
    switch($a) {
        case 0:
            $b = $this->userProfile();
            break;
        case 1:

            $b = $this->contactProfile();
            break;
    }
    return $b;
}

but they all have the same result. Works correctly if comm_people->type is 0, doesnt work if comm_people->type is 1. I have checked and contactProfile works fine by itself. Please help