JarJarSloth

JarJarSloth

Member Since 1 Month Ago

Experience Points 350
Experience
Level
Lessons Completed 0
Lessons
Completed
Best Reply Awards 0
Best Answer
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

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

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

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

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    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 Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

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

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

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

21 Nov
3 weeks ago

JarJarSloth started a new conversation Register User With Relation Models

Im trying to figure out a better way to handle my registration of new users. I have multiple other tables in my DB that needs to be populated by the new user registration as default values. The way i am doing it now is after all validation i run this


        Money::create([
            'user_id' => $user->id,
        ]);
        Attribute::create([
            'user_id' => $user->id,
        ]);
        Resource::create([
            'user_id' => $user->id,
        ]);

        return $user;
    

This will lead to more and more duplications.

11 Nov
1 month ago

JarJarSloth left a reply on Laravel Eloquent, How Do I Group By For 2 Columns In Relation

@AHMEDDABAK - Alright, but how should the relation from user to conversations be and the conversation table, when you say only need a unique id can you explain how this would make sense as a relation to find correct messages ? Thanks

JarJarSloth left a reply on Laravel Eloquent, How Do I Group By For 2 Columns In Relation

@AHMEDDABAK - Alright that seems easier to handle and scale with. But how would my Conversation table looks like do you mean? And do you mean that this would help me get all my conversations easy via $user->conversations ? Thanks

JarJarSloth started a new conversation Laravel Eloquent, How Do I Group By For 2 Columns In Relation

I have a messages table that looks like this:

 - id
 - user_id
 - target_user_id
 - message

What i would like to do is group up these by conversation.

So some typical records can look like this:


    #1
    user_id = 5
    target_user_id = 10
    message = hello
    #2
    user_id = 10
    target_user_id = 5
    message = hello
    #3
    user_id = 15
    target_user_id = 20
    message = hello
    #2
    user_id = 15
    target_user_id = 20
    message = hello

What i would like to achieve is how i can group all my records together so that all messages between user 5 and 10 for example is in a group and 15 and 20 is in a group and sorted by latest.

This is my current attempt:

    $msgs = Message::with('user')
         ->where(function($q) {
         $q->where('user_id', Auth::user()->id)
            ->orWhere('target_user_id', Auth::user()->id);
         })
         ->groupBy('user_id', 'target_user_id')
                ->get();
09 Nov
1 month ago

JarJarSloth left a reply on Fetch Most Recent Message Sent To Me Per User

@SERGIU17 - Alright, my relationship between App\User and App\Message is this hasMany:

    public function messages()
    {
        return $this->hasMany(Message::class);
    }

in the Message table i have a column for user_id and a column called target_user_id So the one sending the message has it's id in user_id and the one receiving the message has their id in target_user_id. Thanks.

JarJarSloth started a new conversation Fetch Most Recent Message Sent To Me Per User

I am trying to sort my messages from users in the same way twitter handles it. So each user who has sent a message to me should be listed and with their latest message to me below.

My query to try and get this seems like it should work to me but it doesn't. It returns the first message the user sent to me so the opposite.

This is my query:

$messages = Message::with('user')->where('target_user_id', Auth::user()->id)->latest()->groupBy('user_id')->get();
04 Nov
1 month ago

JarJarSloth started a new conversation Get Data With Polymorphic Table Not Updating After Delete.

I am trying to remove a record in my relation table and then fetch all records and return them with JSON to display the updated list. The problem is that when i am trying to get the data after delete it returns the data as if the record is not deleted but when i check the record is deleted.

This is how i'm deleting the record.

public function sellCat(Request $request)
{
  $cat = Cat::find($request->pivot['id']);
  $cat->delete();
  $user = Auth::user();

  return $user->cats;
}

User-Model:
public function cats()
    {
        return $this->morphedByMany(Cat::class, 'animals')->withPivot('id');
    }

Cat-Model:
    public function cats()
    {
        return $this->morphToMany('App\User', 'animals');
    }

Animals model is empty but it's a relation table that takes user_id, animal_id animal_type.

02 Nov
1 month ago

JarJarSloth left a reply on Prevent Users From Accessing Search Api.

@Sergiu17

I'm already doing this but users can simply write /api/search?q=a and it will return a lot of users.. What i would like is for no users to via only the url get access to user information. I want it to only via the axios request return anything.

JarJarSloth started a new conversation Prevent Users From Accessing Search Api.

I am building a search function on my site and users can search for others users. Im making an axios.get request against /api/search?q= with their query.

The problem i'm having is that users can simply visit /api/search?q= and it will return information about a lot of users i don't want them to have access to.

Could i somehow handle the access via CSRF token or something else to fix this issue?

31 Oct
1 month ago

JarJarSloth left a reply on Using Relationtable For Users

@lostdreamer_nl

I tried your suggestion and according to the documentation the polymorphic many to many relationship seems right. But i am unable to get it working, when trying to get equipment

$user->equipment  

it returns the db relation row and

$user->weapons

returns NULL.

Inside my user_equipment table i have

user_id = 21 ( my current user )
equipment_id = 2 ( a specific weapon id from weapons table)
equipment_type = weapon

JarJarSloth started a new conversation Using Relationtable For Users

have tried to find the best solution for my problem without getting exactly what i want.

I have 4 tables now, the structure looks like this.

:: Users ::
id
name
email
------

:: user_equipment ::
id
user_id
equipment_id
equipment_type

:: weapons ::
id
name
price

:: armor ::
id
name
price

$user->equipment $user->equipment->weapons $user->equipment->armor

Here is what i have right now:

User Model:

class User extends Model
{    
  public function equipment()
  {
    return $this->hasOne(UserEquipment::class);
  }
}

UserEquipment Model:

class UserEquipment extends Model
{
   public function weapons()
   {
     return $this->hasMany(Weapon::class);
   }
}