Our Black Friday sale is now live! All individual subscriptions are 50% OFF. This week only!

Vusumzi

Vusumzi

Member Since 2 Months Ago

Experience Points
2,610
Total
Experience

2,390 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
3
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
2,610 XP
Nov
17
1 week ago
Activity icon

Started a new Conversation How To Match These Two Outputs And Checked Matching Subject/vakke

Hi everyone, I want to match these two outputs and if they match they should be checked...

public function edit($id)
    {

        $classes = Grade::with('subjects')->get();
       
        $vakkes = Vakke::where('user_id', $id)->get();

        return view('profiles.edit', compact('classes', 'vakkes')); 
           
    }

edit.blade.php

@foreach($class->subjects as $key => $subject)
                                @foreach($vakkes as $key => $vakke)
                                    <div class="custom-control custom-checkbox mt-2">
                                        <input type="checkbox" class="custom-control-input @error('subjects') is-invalid @enderror" id="{{$class->id}}{{$subject->id}}" 
                                        name="vakkes[{{$class->id}}][]" value="{{ $subject->id }}" 
                                        {{ ($user->grades->pluck('id')->contains($class->id) && $vakke->pluck('name')[$key]->contains($subject->name)) ? 'checked': ''}}/>
                                        <label class="custom-control-label" for="{{$class->id}}{{$subject->id}}">{{$subject->name}}</label>
                                    </div>   
                                @endforeach
                            @endforeach

dd($classes)

array:3 [▼
  0 => array:7 [▼
    "id" => 1
    "name" => "Grade 8"
    "slug" => "grade-8"
    "description" => "This is the Supervisor of this site"
    "created_at" => "2020-11-17T07:25:36.000000Z"
    "updated_at" => "2020-11-17T07:25:36.000000Z"
    "subjects" => array:1 [▼
      0 => array:7 [▼
        "id" => 1
        "name" => "Mathematics"
        "slug" => "mathematics"
        "description" => "This is the Supervisor of this site"
        "created_at" => "2020-11-17T07:24:36.000000Z"
        "updated_at" => "2020-11-17T07:24:36.000000Z"
        "pivot" => array:4 [▶]
      ]
    ]
  ]
  1 => array:7 [▼
    "id" => 2
    "name" => "Grade 9"
    "slug" => "grade-9"
    "description" => "This is the Supervisor of this site"
    "created_at" => "2020-11-17T07:25:50.000000Z"
    "updated_at" => "2020-11-17T07:25:50.000000Z"
    "subjects" => array:2 [▼
      0 => array:7 [▼
        "id" => 1
        "name" => "Mathematics"
        "slug" => "mathematics"
        "description" => "This is the Supervisor of this site"
        "created_at" => "2020-11-17T07:24:36.000000Z"
        "updated_at" => "2020-11-17T07:24:36.000000Z"
        "pivot" => array:4 [▶]
      ]
      1 => array:7 [▼
        "id" => 2
        "name" => "Physical Science"
        "slug" => "physical-science"
        "description" => "This is the Supervisor of this site"
        "created_at" => "2020-11-17T07:24:43.000000Z"
        "updated_at" => "2020-11-17T07:24:43.000000Z"
        "pivot" => array:4 [▶]
      ]
    ]
  ]
  2 => array:7 [▼
    "id" => 3
    "name" => "Grade 10"
    "slug" => "grade-10"
    "description" => "This is the Supervisor of this site"
    "created_at" => "2020-11-17T07:26:10.000000Z"
    "updated_at" => "2020-11-17T07:26:10.000000Z"
    "subjects" => array:3 [▼
      0 => array:7 [▶]
      1 => array:7 [▶]
      2 => array:7 [▶]
    ]
  ]
]

dd($vakkes)

array:2 [▼
  0 => array:6 [▼
    "id" => 1
    "user_id" => 6
    "grade_id" => 2
    "name" => array:1 [▼
      0 => "Physical Science"
    ]
    "created_at" => "2020-11-17T07:35:44.000000Z"
    "updated_at" => "2020-11-17T07:35:44.000000Z"
  ]
  1 => array:6 [▼
    "id" => 2
    "user_id" => 6
    "grade_id" => 3
    "name" => array:2 [▼
      0 => "Mathematics"
      1 => "Physical Science"
    ]
    "created_at" => "2020-11-17T07:35:44.000000Z"
    "updated_at" => "2020-11-17T07:35:44.000000Z"
  ]
]
Nov
13
1 week ago
Activity icon

Started a new Conversation How Do You Insert A Multi Dimensional Array Into A Database

Hi all, I have this array called vakke I want to insert into the database

array:15 [▼
  "_token" => "VPC5jBKnYapzKv39aUsEFmDHJFwqKh1bqQ0lPp3M"
  "role" => "teacher"
  "last_name" => "Maya"
  "name" => "Vusumzi Maya"
  "email" => "[email protected]"
  "phone" => "0719087970"
  "gender" => "Female"
  "identity" => "1233454567677"
  "street" => "30 Sabalele street,"
  "suburb" => "Port Elizabeth"
  "city" => "Port Elizabeth"
  "province" => "Eastern Cape"
  "code" => "6211"
  "klasses" => array:3 [▼
    0 => "1"
    1 => "2"
    2 => "3"
  ]
  "vakkes" => array:3 [▼
    1 => array:1 [▼
      0 => "Mathematics"
    ]
    2 => array:1 [▼
      0 => "Physical Science"
    ]
    3 => array:2 [▼
      0 => "Mathematics"
      1 => "Physical Science"
    ]
  ]
]

Here is my Controller

public function store(Request $request)
    {
     	.
	.
	.
	.
	.
       

        $user = new User();
        $user->last_name = $request->last_name;
        $user->name = $request->name;
        $user->email = $request->email;
        $user->phone = $request->phone;
        $user->password = Hash::make($password);
        $user->gender = $request->gender;
        $user->identity = $request->identity;
        $user->street = $request->street;
        $user->suburb = $request->suburb;
        $user->city = $request->city;
        $user->province = $request->province;
        $user->code = $request->code;
        $user->school = $request->school;
        $user->save();
        
        $user->attachRole($request->role);
  

        $user->klasses()->sync($request->klasses); 

            if($request->vakkes){

               // What to write here?
                
            }

        return redirect()->back()->with('message', $user->name . ' has been created successfully');
    }

I have a One to Many relationship with Klass and Vakke Models

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Klass extends Model
{
    public function vakkes()
    {
        return $this->hasMany('App\Vakke');
    }
    
   public function users()
    {
        return $this->belongsToMany('App\User')->withTimeStamps();
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Vakke extends Model
{
    public function klasses()
    {
        return $this->belongsTo('App\Klass');
    }
    public function users()
    {
        return $this->belongsToMany('App\User')->withTimeStamps();
    }
}

here is my Blade file

@foreach($grades as $grade)
                    <div class="card shadow mx-2 my-4">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold">
                                <div class="custom-control custom-checkbox">
                                    <input class="custom-control-input @error('grade') is-invalid @enderror" type="checkbox"  id="{{$grade->id}}" name="klasses[]" value="{{ $grade->id }}"/>
                                    <label class="custom-control-label pt-1" for="{{$grade->id}}">{{$grade->name}}</label>
                                </div>
                                @error('grade')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </h6>
                        </div>
                        <div class="card-body">
                           
                                @foreach($grade->subjects as $subject)
                                <div class="custom-control custom-checkbox mb-2">
                                    <input class="custom-control-input" type="checkbox" id="{{$grade->id}}{{$subject->id}}" name="vakkes[{{$grade->id}}][]" value="{{ $subject->name }}"/>
                                    <label class="custom-control-label" for="{{$grade->id}}{{$subject->id}}">
                                        {{$subject->name}}
                                    </label>
                                </div>
                                @endforeach
                        </div>
                    </div>
                    @endforeach
Nov
11
1 week ago
Activity icon

Started a new Conversation How Do You Use The Sync Function In A Multi Dimensional Array In Laravel

Can u use the sync method in laravel to sync this type of a multi dimensional array?

array:19 [▼
  "_token" => "r87fqYAVFkBIIYth9dvc6i9zxI73PkBztu3KdJ6f"
  "_method" => "PATCH"
  "role" => "teacher"
  "last_name" => "Maya"
  "name" => "Vusumzi"
  "email" => "[email protected]"
  "phone" => "0719087970"
  "gender" => "Male"
  "identity" => "8548976907564"
  "old_password" => null
  "new_password" => null
  "new_password_confirmation" => null
  "street" => "30 Sabalele street,"
  "suburb" => "Motherwell NU4B"
  "city" => "Port Elizabeth"
  "province" => "Eastern Cape"
  "code" => "6211"
  "grade" => array:5 [▼
    0 => "1"
    1 => "2"
    2 => "3"
    3 => "4"
    4 => "5"
  ]
  "subjects" => array:5 [▼
    1 => array:2 [▼
      0 => "1"
      1 => "2"
    ]
    2 => array:2 [▼
      0 => "1"
      1 => "5"
    ]
    3 => array:3 [▼
      0 => "1"
      1 => "5"
      2 => "6"
    ]
    4 => array:2 [▼
      0 => "1"
      1 => "3"
    ]
    5 => array:4 [▼
      0 => "1"
      1 => "4"
      2 => "5"
      3 => "6"
    ]
  ]
]

I want to sync the subjects' array in this controller

public function update(Request $request, $id)
    {  
       
        $user = User::where('id',$id)->first();
    .
    .
    .
    .
    .
        
        $user->grades()->sync($request->grade);
        
        if($request->subjects){ 
            $user->subjects()->sync($request->subjects);
        }

        return redirect()->back()->with('message', $user->name . ' is updated Successfully');

    }

The grade is working fine but the issue is the subjects array, its giving me this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update grade_subject set 0 = 1 where grade_id = 1 and subject_id in (1))

Activity icon

Replied to How To Fix This Problem Of Sync Method In Laravel?

how do I sync these subjects to their grades? because its giving me this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update grade_subject set 0 = 1 where grade_id = 1 and subject_id in (1))

this is the request

array:19 [▼
  "_token" => "r87fqYAVFkBIIYth9dvc6i9zxI73PkBztu3KdJ6f"
  "_method" => "PATCH"
  "role" => "teacher"
  "last_name" => "Maya"
  "name" => "Vusumzi"
  "email" => "[email protected]"
  "phone" => "0719087970"
  "gender" => "Male"
  "identity" => "8548976907564"
  "old_password" => null
  "new_password" => null
  "new_password_confirmation" => null
  "street" => "30 Sabalele street,"
  "suburb" => "Motherwell NU4B"
  "city" => "Port Elizabeth"
  "province" => "Eastern Cape"
  "code" => "6211"
  "grade" => array:4 [▼
    0 => "1"
    1 => "2"
    2 => "3"
    3 => "4"
  ]
  "subjects" => array:4 [▼
    1 => array:1 [▼
      0 => "1"
    ]
    2 => array:1 [▼
      0 => "1"
    ]
    3 => array:1 [▼
      0 => "1"
    ]
    4 => array:1 [▼
      0 => "1"
    ]
  ]
]
Activity icon

Started a new Conversation How To Fix This Problem Of Sync Method In Laravel?

I have many to many relationships between User, Grade and Subject, the problem here is when I submit the data from the form to the update method in the controller, the sync method $user->subjects()->sync($request->subjects); is adding every subject I selected to every grade I chose... what I want is when I chose a grade and a subject, that subject should be added under the grade which is selected for, not to every grade selected...I think the problem is in the $user->subjects()->sync($request->subjects);, I'm not sure

For instance, in the picture below: I selected Grade 8 with Mathematics in the first block, but when I select Grade 9 with English in the second block and submit, the results came back like this... the Red encircled subjects shouldn't be checked. What am I doing wrong?

https://i.stack.imgur.com/qp0Po.jpg

@foreach($classes as $key => $class)
                    <div class="card shadow mx-2 my-4">
                        <div class="card-header bg-light py-3">
                            <h6 class="m-0 font-weight-bold">
                                <div class="custom-control custom-checkbox">
                                    <input class="custom-control-input" type="checkbox"  id="{{$class->id}}" name="grade[]" value="{{ $class->id }}" 
                                    {{ ($user->grades->pluck('id')->contains($class->id)) ? 'checked' : '' }} />
                                    <label class="custom-control-label pt-1" for="{{$class->id}}">{{$class->name}}</label>
                                </div>
                            </h6>
                        </div>
                        <div class="card-body">                      
                            @foreach($class->subjects as $key => $subject)
                            <div class="custom-control custom-checkbox mt-2">
                                <input type="checkbox" class="custom-control-input @error('subjects') is-invalid @enderror" id="{{$class->id}}{{$subject->id}}" name="subjects[]" value="{{ $subject->id }}" 
                                {{ ($user->grades->pluck('id')->contains($class->id) && $user->subjects->pluck('id')->contains($subject->id)) ? 'checked' : '' }}/>
                                <label class="custom-control-label" for="{{$class->id}}{{$subject->id}}">{{$subject->name}}</label>
                            </div>   
                            @endforeach
                        </div>
                    </div>
                    @endforeach
public function update(Request $request, $id)
    {  
       
        $user = User::where('id',$id)->first();
    .
    .
    .
    .
    .
           
        $user->save();
        
        $user->grades()->sync($request->grade);
        
        if($request->subjects){ 
            $user->subjects()->sync($request->subjects);
        }

        return redirect()->back()->with('message', $user->name . ' is updated Successfully');

    }
Activity icon

Replied to How To Get Data From The Database And Display Then In The Vue Component

@automica The thing I want is the the user information to be displayed

I have also passed the user information in the component

 <grade-component-edit :grades='@json($classes)' :user='@json($user)' :errors='{{$errors}}'></grade-component-edit>
export default{
    props: ['grades', 'user', 'errors'],
 data() {
    return {
        grading: this.grades,
        user: this.user,
        error: this.errors,
        selected_grade: 0,
        subjects: null,
    }
  },
  methods: {
        onChange(event) {
          this.grading.forEach((obj, index) => {
            if (obj.id == event.target.value){
                this.subjects = obj.subjects;
                this.error = []
            }
          })
        }
        
    }
}
</script>

Now in the dropdown list I want the users grade to be selected and in the checkboxes his subjects to be checked...

I want to do something like this in vue

 <select id="province" name="province" class="form-control @error('province') is-invalid @enderror" >
        <option value="0">Choose...</option>
       <option value="Western Cape" {{ old('province', $user->province) == "Western Cape" ? 'selected' : ''}}>Western Cape</option>
. . .
Activity icon

Replied to How To Get Data From The Database And Display Then In The Vue Component

@automica Yes I am getting some data like this:

https://ibb.co/PDzxB1J

How do I display it in the component?

Nov
10
2 weeks ago
Activity icon

Replied to How To Get Data From The Database And Display Then In The Vue Component

@automica This is what is being passed

public function edit($id)
    {
        $roles = Role::all();

        $user = User::with(['roles', 'grades', 'subjects'])->findOrFail($id);

        $classes = Grade::with('subjects')->get();
 

        return view('profiles.edit', compact('roles', 'user', 'classes')); 
           
    }
Activity icon

Replied to How To Get Data From The Database And Display Then In The Vue Component

@automica Its working fine when I'm adding a user to the database, but I want to get values from the database and populate the input fields, so I can update the user

Activity icon

Started a new Conversation How To Get Data From The Database And Display Then In The Vue Component

I want to populate this component with the data from the database so the user can make chages

GradeComponentEdit.vue

<template>

    <div class="row my-2">
    <div class="col-md-2 font-weight-bold pt-2">Grade:</div>
        <div class="col-md-10">
            <select name="grade" class="form-control" v-model="selected_grade" @change="onChange($event)">
                <option value="0" disabled>Choose a grade</option>
                <option v-for="grade in grading" :key="grade.id" :value="grade.id">{{grade.name}}</option>
            </select>
            <span v-if='error.grade' class="errors text-danger">{{ error.grade[0] }}</span>
            
            <div class="row my-3" v-if="subjects !== null">
                <div class="col-md-5">
                    <div class="card shadow">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold">Select Subjects:</h6>
                        </div>
                        <div class="card-body">
                            <div class="custom-control custom-checkbox mb-2" v-for="subject in subjects" :key="subject.id">
                                <input type="checkbox" class="custom-control-input" name="subjects[]" :id="subject.id" :value="subject.id"/> 
                                <label class="custom-control-label" :for="subject.id">{{subject.name}}</label>
                            </div>
                            <span v-if='error.subjects' class="errors text-danger">{{ error.subjects[0] }}</span>
                        </div>  
                    </div>
                </div>
            </div>
        </div>
    </div>

</template>
<script>

export default{
    props: ['grades', 'errors'],
 data() {
    return {
        grading: this.grades,
        error: this.errors,
        selected_grade: 0,
        subjects: null,
    }
  },
  methods: {
        onChange(event) {
          this.grading.forEach((obj, index) => {
            if (obj.id == event.target.value){
                this.subjects = obj.subjects;
                this.error = []
            }
          })
        }
        
    }
}
</script>

edit.blade.php

 @if($user->hasRole(['superadministrator', 'learner']))
                    <div class="col-md-12" >
                        <div class="row my-3">
                            <div class="col-md-2 font-weight-bold pt-2">School:</div>
                            <div class="col-md-10">
                                <input  type="text" id="school" name="school" class="form-control @error('school') is-invalid @enderror" value="{{ old('school', $user->school) }}">
                                @error('school')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>
                        <grade-component-edit :grades='@json($classes)' :errors='{{$errors}}'></grade-component-edit>

                    </div>
                    @endif
Activity icon

Replied to Check Subjects Which Match

One more thing...

when I Update the user I use the update resource method and when I sync the subjects from the request it does the same thing again... am I doing this wrong?

public function update(Request $request, $id)
    {  
       
        $user = User::where('id',$id)->first();
        .
	.
	.
	.
	.
           
        $user->save();
        
        $user->grades()->sync($request->grade);
        
        if($request->subjects){ 
            $user->subjects()->sync($request->subjects);
        }

        return redirect()->back()->with('message', $user->name . ' is updated Successfully');

    }
Activity icon

Replied to Check Subjects Which Match

@automica Perfect... Thank you Very Much Man!

Activity icon

Replied to Check Subjects Which Match

There is many to many relationship between Grades and Subjects

Activity icon

Replied to Check Subjects Which Match

dd($user->grades)

array:2 [▼
  0 => array:7 [▼
    "id" => 4
    "name" => "Grade 11"
    "slug" => "grade-11"
    "description" => "This is Grade Eleven"
    "created_at" => "2020-11-07T13:21:49.000000Z"
    "updated_at" => "2020-11-07T13:21:49.000000Z"
    "pivot" => array:2 [▼
      "user_id" => 6
      "grade_id" => 4
    ]
  ]
  1 => array:7 [▼
    "id" => 5
    "name" => "Grade 12"
    "slug" => "grade-12"
    "description" => "This is Grade Twelve"
    "created_at" => "2020-11-07T13:22:15.000000Z"
    "updated_at" => "2020-11-07T13:22:15.000000Z"
    "pivot" => array:2 [▼
      "user_id" => 6
      "grade_id" => 5
    ]
  ]
]
Activity icon

Replied to Check Subjects Which Match

dd($user)

array:22 [▼
  "id" => 6
  "name" => "Vusumzi"
  "last_name" => "Maya"
  "email" => "[email protected]"
  "email_verified_at" => null
  "phone" => "0719087970"
  "avatar" => "avatar-1604755667.png"
  "gender" => "Male"
  "identity" => "8548976907564"
  "street" => "30 Sabalele street,"
  "suburb" => "Motherwell NU4B"
  "city" => "Port Elizabeth"
  "province" => "Eastern Cape"
  "code" => 6211
  "school" => null
  "randomstr" => "8pHbnEmjh1"
  "related" => null
  "created_at" => "2020-11-07T13:24:45.000000Z"
  "updated_at" => "2020-11-07T13:27:48.000000Z"
  "roles" => array:1 [▶]
  "grades" => array:2 [▶]
  "subjects" => array:2 [▼
    0 => array:7 [▼
      "id" => 1
      "name" => "Mathematics"
      "slug" => "mathematics"
      "description" => "This is Mathematics"
      "created_at" => "2020-11-07T13:15:50.000000Z"
      "updated_at" => "2020-11-07T13:15:50.000000Z"
      "pivot" => array:2 [▶]
    ]
    1 => array:7 [▶]
  ]
]

dd($classes)

array:5 [▼
  0 => array:7 [▼
    "id" => 1
    "name" => "Grade 8"
    "slug" => "grade-8"
    "description" => "This is Grade Eight"
    "created_at" => "2020-11-07T13:19:55.000000Z"
    "updated_at" => "2020-11-07T13:19:55.000000Z"
    "subjects" => array:3 [▼
      0 => array:7 [▼
        "id" => 1
        "name" => "Mathematics"
        "slug" => "mathematics"
        "description" => "This is Mathematics"
        "created_at" => "2020-11-07T13:15:50.000000Z"
        "updated_at" => "2020-11-07T13:15:50.000000Z"
        "pivot" => array:2 [▶]
      ]
      1 => array:7 [▶]
      2 => array:7 [▶]
    ]
  ]
  1 => array:7 [▼
    "id" => 2
    "name" => "Grade 9"
    "slug" => "grade-9"
    "description" => "This is Grade Nine"
    "created_at" => "2020-11-07T13:20:27.000000Z"
    "updated_at" => "2020-11-07T13:20:27.000000Z"
    "subjects" => array:4 [▼
      0 => array:7 [▼
        "id" => 1
        "name" => "Mathematics"
        "slug" => "mathematics"
        "description" => "This is Mathematics"
        "created_at" => "2020-11-07T13:15:50.000000Z"
        "updated_at" => "2020-11-07T13:15:50.000000Z"
        "pivot" => array:2 [▶]
      ]
      1 => array:7 [▶]
      2 => array:7 [▶]
      3 => array:7 [▶]
    ]
  ]
  2 => array:7 [▼
    "id" => 3
    "name" => "Grade 10"
    "slug" => "grade-10"
    "description" => "This is Grade Ten"
    "created_at" => "2020-11-07T13:20:56.000000Z"
    "updated_at" => "2020-11-07T13:20:56.000000Z"
    "subjects" => array:4 [▼
      0 => array:7 [▼
        "id" => 1
        "name" => "Mathematics"
        "slug" => "mathematics"
        "description" => "This is Mathematics"
        "created_at" => "2020-11-07T13:15:50.000000Z"
        "updated_at" => "2020-11-07T13:15:50.000000Z"
        "pivot" => array:2 [▶]
      ]
      1 => array:7 [▶]
      2 => array:7 [▶]
      3 => array:7 [▶]
    ]
  ]
  3 => array:7 [▶]
  4 => array:7 [▶]
]

check this pic: https://i.stack.imgur.com/mDB2J.jpg

Those uncycled red should not be checked, they are the extra ones... Grade 11 and Grade 12 blocks are correct, but grade 8 - 10 subjects shouldn't be checked...

Activity icon

Replied to Check Subjects Which Match

Yes, but no difference

@foreach($classes as $key => $class)
                    <div class="card shadow mx-2 my-4">
                        <div class="card-header bg-light py-3">
                            <h6 class="m-0 font-weight-bold">
                                <div class="custom-control custom-checkbox">
                                    <input class="custom-control-input" type="checkbox"  id="{{$class->id}}" name="grade[]" value="{{ $class->id }}" 
                                    {{ ($user->grades->pluck('id')->contains($class->id)) ? 'checked' : '' }} />
                                    <label class="custom-control-label pt-1" for="{{$class->id}}">{{$class->name}}</label>
                                </div>
                            </h6>
                        </div>
                        <div class="card-body">                      
                            @foreach($class->subjects as $key => $subject)
                            <div class="custom-control custom-checkbox mt-2">
                                <input type="checkbox" class="custom-control-input @error('subjects') is-invalid @enderror" id="{{$class->id}}{{$subject->id}}" name="subjects[]" value="{{ $subject->id }}" 
                                {{ ($user->subjects->pluck('id')->contains($subject->id)) ? 'checked' : '' }}/>
                                <label class="custom-control-label" for="{{$class->id}}{{$subject->id}}">{{$subject->name}}</label>
                            </div>   
                            @endforeach
                        </div>
                    </div>
                    @endforeach
Activity icon

Replied to Check Subjects Which Match

This is what I have in my controller

public function edit($id)
    {
        $user = User::with(['roles', 'grades', 'subjects'])->findOrFail($id);
        
        $classes = Grade::with('subjects')->get();

        return view('profiles.edit', compact( 'user', 'classes')); 
           
    }

I also tried with the Id, but still have the same problem...

Activity icon

Started a new Conversation Check Subjects Which Match

Hi.. Im having problem with the subject's nested @foreach($class->subjects as $subject)... some of the subjects which do not belong to the grade/class are checked... I think the problem is all the subjects which have the same name are being checked...

@foreach($classes as $class)
                    <div class="card shadow mx-2 my-4">
                        <div class="card-header bg-light py-3">
                            <h6 class="m-0 font-weight-bold">
                                <div class="custom-control custom-checkbox">
                                    <input class="custom-control-input" type="checkbox"  id="{{$class->id}}" name="class[]" value="{{ $class->name }}" 
                                    {{ ($class->name == ($user->grades->pluck('name')->contains($class->name))) ? 'checked' : '' }} />
                                    <label class="custom-control-label pt-1" for="{{$class->id}}">{{$class->name}}</label>
                                </div>
                            </h6>
                        </div>
                        <div class="card-body">                      
                            @foreach($class->subjects as $subject)
                            <div class="custom-control custom-checkbox mt-2">
                                <input type="checkbox" class="custom-control-input @error('subjects') is-invalid @enderror" id="{{$class->id}}{{$subject->id}}" name="subjects[]" value="{{ $subject->name }}" 
                                {{ ($subject->name == ($user->subjects->pluck('name')->contains($subject->name))) ? 'checked' : '' }} />
                                <label class="custom-control-label" for="{{$class->id}}{{$subject->id}}">{{$subject->name}}</label>
                            </div>   
                            @endforeach
                        </div>
                    </div>
                    @endforeach

How do I uniquely I identify each subject which is loaded in the database and compared with the user's subjects?

Activity icon

Replied to How To Get The Values Checked If They Match

Yes it did help a little... and I have made some modifications

@foreach($classes as $class)
                    <div class="card shadow mx-2 my-4">
                        <div class="card-header bg-light py-3">
                            <h6 class="m-0 font-weight-bold">
                                <div class="custom-control custom-checkbox">
                                    <input class="custom-control-input" type="checkbox"  id="{{$class->id}}" name="class[]" value="{{ $class->name }}" 
                                    {{ ($class->name == ($user->grades->pluck('name')->contains($class->name))) ? 'checked' : '' }} />
                                    <label class="custom-control-label pt-1" for="{{$class->id}}">{{$class->name}}</label>
                                </div>
                            </h6>
                        </div>
                        <div class="card-body">                      
                            @foreach($class->subjects as $subject)
                            <div class="custom-control custom-checkbox mt-2">
                                <input type="checkbox" class="custom-control-input @error('subjects') is-invalid @enderror" id="{{$class->id}}{{$subject->id}}" name="subjects[]" value="{{ $subject->name }}" 
                                {{ ($subject->name == ($user->subjects->pluck('name')->contains($subject->name))) ? 'checked' : '' }} />
                                <label class="custom-control-label" for="{{$class->id}}{{$subject->id}}">{{$subject->name}}</label>
                            </div>   
                            @endforeach
                        </div>
                    </div>
                    @endforeach

but the problem now is in the subject's nestedforeach loop, some of the subjects which have the same name are also being checked...

Nov
09
2 weeks ago
Activity icon

Started a new Conversation Property Or Method "subjectselected" Is Not Defined On The Instance But Referenced During Render.

How do I fix this error

[Vue warn]: Property or method "subjectselected" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

<div class="row">
        <div class="col-md-9">
            <div class="card shadow mb-4 px-4 pb-4">
                <div class="card-header py-3 px-0">
                    <h6 class="m-0 font-weight-bold text-primary">Classes</h6>
                    <input type="hidden" :value="gradeSelected" name="grades"/>
                    <input type="hidden" :value="subjectselected" name="subjects"/>
                </div>
                <div class="row">
                    @foreach($classes as $class)
                    <div class="card shadow mx-2 my-4">
                        <div class="card-header bg-light py-3">
                            <h6 class="m-0 font-weight-bold">
                                <div class="custom-control custom-checkbox">
                                    <input class="custom-control-input" type="checkbox"  id="{{$class->id}}" name="class[]" :value="{{ $class->id }}" v-model="gradeSelected" 
                                     />
                                    <label class="custom-control-label pt-1" for="{{$class->id}}">{{$class->name}}</label>
                                </div>
                            </h6>
                        </div>
                        <div class="card-body">                      
                            @foreach($class->subjects as $subject)
                            <div class="custom-control custom-checkbox mt-2">
                                <input type="checkbox" class="custom-control-input @error('subjects') is-invalid @enderror" id="{{$class->id}}{{$subject->id}}" name="subjects[]" :value="{{ $subject->id }}" v-model="subjectSelected" 
                                 />
                                <label class="custom-control-label" for="{{$class->id}}{{$subject->id}}">{{$subject->name}}</label>
                            </div>   
                            @endforeach
                        </div>
                    </div>
                    @endforeach
                </div>
            </div>
        </div>
    </div>
    @endif
    <div class="row">
        <div class="col-md-12 mb-4">
            <button type="submit" class="btn btn-success btn-lg mr-4"><i class="fas fa-user-edit fa-fw mr-2"></i>Update User</button> 
            <a href="/dashboard" class="btn btn-secondary btn-lg"><i class="fas fa-user-edit fa-fw mr-2"></i>Cancel</a>                
        </div>
    </div>
</form>

@section('js_index_page')
<script>
    window.addEventListener('load',function(){
        var app = new Vue({
            el: '#app',
            data:{
                gradeSelected: {!! $user->grades->pluck('id') !!},
                subjectSelected: {!! $user->subjects->pluck('id') !!}
            }
        });
    });
</script>
@endsection
Activity icon

Started a new Conversation How To Get The Values Checked If They Match

Hi all... I have eager-loaded all the grades with subjects in the database, I want all the grades and subjects associated with the user to be checked when I edit the user, but its not checked... what am I doing wrong?

public function edit($id)
    {
       
        $classes = Grade::with('subjects')->get();
       
        return view('profiles.edit', compact( 'classes'));  
    }

edit.blade.php

<div class="row">
                    @foreach($classes as $class)
                    <div class="card shadow mx-2 my-4">
                        <div class="card-header bg-light py-3">
                            <h6 class="m-0 font-weight-bold">
                                <div class="custom-control custom-checkbox">
                                    <input class="custom-control-input" type="checkbox"  id="{{$class->id}}" name="class[]" value="{{ $class->name }}" 
                                    {{ (is_array(old('class')) && in_array( $class->name, old('class', $user->class))) ? 'checked' : '' }} />
                                    <label class="custom-control-label pt-1" for="{{$class->id}}">{{$class->name}}</label>
                                </div>
                            </h6>
                        </div>
                        <div class="card-body">                      
                            @foreach($class->subjects as $subject)
                            <div class="custom-control custom-checkbox mt-2">
                                <input type="checkbox" class="custom-control-input @error('subjects') is-invalid @enderror" id="{{$class->id}}{{$subject->id}}" name="subjects[]" value="{{ $subject->name }}" 
                                {{ (is_array(old('subjects')) && in_array( $subject->name, old('subjects', $user->subjects))) ? 'checked' : '' }} />
                                <label class="custom-control-label" for="{{$class->id}}{{$subject->id}}">{{$subject->name}}</label>
                            </div>   
                            @endforeach
                        </div>
                    </div>
                    @endforeach
                </div>
Nov
08
2 weeks ago
Activity icon

Replied to Get All Users With Specified Grade And Subject

Yes I see... so how must I send the request to the controller?... because each subject will have its own submit button

 @foreach(Auth::user()->subjects as $subject)
          <div class="m-2">
               <button type="button" class="btn btn-outline-primary">{{ $subject->name }}</button>
          </div>
  @endforeach

how to send this with a form?

Activity icon

Started a new Conversation Get All Users With Specified Grade And Subject

I have this blade file which brings the Authenticated User (Teacher), Grade and subjects he is teaching. what I want is when he clicks in the subject's button, all the users (learners) in the database which are doing that grade and subject -which is clicked- should be displayed

@foreach(Auth::user()->grades as $grade)
        <div class="col-md-2">
            <div class="card shadow mb-4">
                <div class="card-body">
                    <h5 class="font-weight-bold">{{$grade->name}}</h5>
                    <hr>
                    <h6 class="font-weight-bold">Subjects:</h6>
                    @foreach(Auth::user()->subjects as $subject)
                        <div class="m-2">
                            <button type="button" class="btn btn-outline-primary">{{ $subject->name }}</button>
                        </div>
                    @endforeach
                </div>
            </div>
        </div>
        @endforeach

How do I send the request to the controller's method (showLearners) and the controller should query the database and return all the required learners?

public function showLearners(...)
{ 

    // 

}

The User Model has many to many relationship with Grade and Subject Models

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laratrust\Traits\LaratrustUserTrait;

use App\Events\UserRegistered;

class User extends Authenticatable
{
    use LaratrustUserTrait;
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    
     /**
     * The event map for the model.
     *
     * @var array
     */
    
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];    

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'related' => 'array',
    ];

   

    public function grades()
    {
        return $this->belongsToMany('App\Grade');
    }

    public function subjects()
    {
        return $this->belongsToMany('App\Subject');
    }
}
Activity icon

Started a new Conversation Display All The Users With Grade And Subject Equals To A Teachers Subject And Grade He Teaches

Hi guys... I want to display all the users with roles = learners, grade equals to specific Grade and subjects equals to a specific subject... I have hardcoded it and works OK but How do I do it dynamically?

ClassController

public function index()
    {
        return view('classes.index');
    }

index.blade.php

@if(count(Auth::user()->grades) > 0)
        @foreach(Auth::user()->grades as $grade)
        <div class="col-md-2">
            <div class="card shadow mb-4">
                <div class="card-body">
                    <form method="POST" action="{{ route('class.show') }}">
                        @csrf
                        <h5 class="font-weight-bold">{{$grade->name}}</h5>
                        <input type="hidden" name="grade" value="{{$grade->name}}"/>
                        <hr>
                        <h6 class="font-weight-bold">Subjects:</h6>
                        <ul>
                            @foreach(Auth::user()->subjects as $subject)
                                <li>{{ $subject->name }}</li>
                                <input type="hidden" name="subjects[]" value="{{$subject->name}}"/>
                            @endforeach
                        </ul>
                        <hr>
                        <div class="row">
                            <div class="col-md-12">
                                <button type="submit" class="btn btn-primary"><i class="fas fa-graduation-cap fa-fw mr-2"></i>View Learners</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        @endforeach
    @else
        <h3 class="col-md-12 offset-md-5">No Grades to display</h3>   
    @endif

Here is where I have hard coded the values (Grade 12, Mathematics) and it works, but I want to do it dynamically... ClassController

public function show(Request $request)
    { 
       $learners = User::whereHas('roles', function($query){
             $query->where('name', 'learner');
        })->whereHas('grades', function($query){
            $query->where('name', 'Grade 12');
        })->whereHas('subjects', function($query){
            $query->where('name', 'Mathematics');
        })->get();
        }
        return view('classes.show', compact('learners'));  
    }
Nov
06
2 weeks ago
Activity icon

Replied to How To Directly Echo Blade Error Messages Into Vue Component

Thank you...

But How do you handle them inside the vue component? I know you can't handle them as in Blade like:

 @error('last_name')
          <span class="invalid-feedback" role="alert">
                  <strong>{{ $message }}</strong>
         </span>
  @enderror

what is the correct way of displaying them?

Activity icon

Started a new Conversation How To Directly Echo Blade Error Messages Into Vue Component

Hello... I have this vue component inside a blade form and I want to handle laravel validation errors in this vue component... How do I directly pass/echo and catch the errors from the controller to the vue and display them underneath the input fields

<template>

    <div class="row my-2">
    <div class="col-md-2 font-weight-bold pt-2">Grade:</div>
        <div class="col-md-10">
            <select name="grade" class="form-control" v-model="selected_grade" @change="onChange($event)">
                <option value="0" disabled>Choose a grade</option>
                <option v-for="grade in grading" :key="grade.id" :value="grade.id">{{grade.name}}</option>
            </select>

            <div class="row my-3" v-if="subjects !== null">
                <div class="col-md-5">
                    <div class="card shadow">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold">Subjects:</h6>
                        </div>
                        <div class="card-body">
                            <div class="custom-control custom-checkbox mb-2" v-for="subject in subjects" :key="subject.id">
                                <input type="checkbox" class="custom-control-input @error('subjects') is-invalid @enderror" name="subjects[]" :id="subject.id" :value="subject.id"/> 
                                <label class="custom-control-label" :for="subject.id">{{subject.name}}</label>
                            </div>
                        </div>  
                    </div>
                </div>
            </div>
        </div>
    </div>

</template>
<script>

export default{
      props: ['grades'],
 data: function() {
    return {
      grading: this.grades,
      selected_grade: 0,
      subjects: null,
    }
  },
  methods: {
        onChange(event) {
          this.grading.forEach((obj, index) => {
            if (obj.id == event.target.value){
                this.subjects = obj.subjects;
            }
          })
        }
    }
}
</script>

create.blade.php: inside this form there is a vue component that I want to validate its input elements like the form elements

<form method="POST" action="{{ route('admin.store') }}">
        @csrf
<div class="row my-2">
      <div class="col-md-2 font-weight-bold pt-2">Surname:</div>
      <div class="col-md-10">
            <input  type="text" id="last_name" name="last_name" class="form-control @error('last_name') is-invalid @enderror" value="{{ old('last_name') }}">
               @error('last_name')
                    <span class="invalid-feedback" role="alert">
                  	  <strong>{{ $message }}</strong>
                      </span>
                  @enderror
              </div>
      </div
.
.
.
.


<grade-component :grades='@json($grades)' ></grade-component>
.
.
.
.

</form>
Nov
05
2 weeks ago
Activity icon

Awarded Best Reply on Laravel Vue Cannot Get Old Value Because Of V-model

<script>
    var app = new Vue({
        el: '#app',
        data: {
            roleType: "{!! old('role') !!}", 
        },
    });
</script>
Activity icon

Replied to Laravel Vue Cannot Get Old Value Because Of V-model

<script>
    var app = new Vue({
        el: '#app',
        data: {
            roleType: "{!! old('role') !!}", 
        },
    });
</script>
Activity icon

Started a new Conversation How To Set The Default Value In A Select Tag In Vue

Hi all... I want to set the default value in a select tag in vue. Im looping the options using v-for, but I can't get the first option <option value="0">Choose a grade...</option> to be shown

<select name="grade" class="form-control @error('grade') is-invalid @enderror" v-model="selected_grade" @change="onChange($event)">
       <option value="0">Choose a grade...</option>
       <option v-for="grade in grading" :key="grade.id" :value="grade.name">{{grade.name}}</option>

</select>


export default{
      props: ['grades'],
 data: function() {
    return {
      grading: this.grades,
      selected_grade: null,
      subjects : null,
    }
  },
  methods: {
        onChange(event) {
          this.grading.forEach((obj, index) => {
            if (obj.name === event.target.value){
                this.subjects = obj.subjects;
            }
          })
        }
    }
}
</script>
Activity icon

Replied to How To Handle Laravel Error Messages In Vue

How to display the errors inside the vue file after passing the errors in to the component?

I don't think this is the right way

@error('grade')
      <span class="invalid-feedback" role="alert">
          <strong>{{ $message }}</strong>
      </span>
@enderror

I get this error in the console

Property or method "$message" is not defined on the instance but referenced during render.

Activity icon

Replied to How To Handle Laravel Error Messages In Vue

This is how I passed the data into the component

<grade-component :grades='@json($grades)'></grade-component>

But its not working...

Activity icon

Replied to How To Handle Laravel Error Messages In Vue

No.. its in vue component file (GradeComponent.vue)

Activity icon

Started a new Conversation How To Handle Laravel Error Messages In Vue

Hi all...I know I did something wrong here, but how do you handle error messages of Laravel in vue template?...

<template>

    <div class="row my-2">
    <div class="col-md-2 font-weight-bold pt-2">Grade:</div>
        <div class="col-md-10">
            <select name="grade" class="form-control @error('grade') is-invalid @enderror" v-model="selected_grade" @change="onChange($event)">
                
                <option v-for="grade in grading" :key="grade.id" :value="grade.name">{{grade.name}}</option>

            </select>
             @error('grade')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror

            <div class="row my-3" v-if="subjects !== null">
                <div class="col-md-3">
                    <div class="card shadow">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold">Subjects:</h6>
                        </div>
                        <div class="card-body">
                            <div class="custom-control custom-checkbox" v-for="subject in subjects" :key="subject.id">
                                <input type="checkbox" class="custom-control-input @error('subjects') is-invalid @enderror" name="subjects[]" :id="subject.name" :value="subject.name"/> 
                                <label class="custom-control-label" :for="subject.name">{{subject.name}}</label>
                            </div>
                            @error('subjects')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>  
                    </div>
                </div>
            </div>
        </div>
    </div>

</template>

<script>

export default{
      props: ['grades'],
 data: function() {
    return {
      grading: this.grades,
      selected_grade: null,
      subjects : null
    }
  },
  methods: {
        onChange(event) {
          this.grading.forEach((obj, index) => {
            if (obj.name === event.target.value){
                this.subjects = obj.subjects;
            }
          })
        }
    }
}
</script>
Nov
04
2 weeks ago
Activity icon

Started a new Conversation Display A Selected Grade With Its Subject

Hi... I want to when a user select a dropdown from the list, a group of subjects available for that grade must be displayed with checkboxes next to them

My controller

public function create()
    {
       
        $grades = Grade::with('subjects')->orderBy('slug', 'asc')->get();
        
        return view('admin.users.create', compact( 'grades'));
    }

Blade file

<div class="form-group">
         <select id="grade" name="grade" class="form-control @error('grade') is-invalid @enderror" v-model="selectedSubjects">
                 <option value="">Choose a Grade...</option>
                            @foreach($grades as $grade)
                  <option value="{{ $grade->id }}" {{ old('grade', $grade) == $grade->name ? 'selected' : "" }}>{{ $grade->name }}</option>                                            
                              @endforeach
               </select>
</div>


<div class="custom-control custom-checkbox mt-2">
        @foreach($grade->subjects as $subject)
                 <input type="checkbox" class="custom-control-input" id="{{$subject->slug}}" name="subjects[]" :value="selectedSubjects" /> 
                   <label class="custom-control-label" for="{{$subject->slug}}">{{$subject->name}}</label>
           @endforeach
  </div>
                              
<script>
    window.addEventListener('load',function(){
        var app = new Vue({
            el: '#app',
            data:{
                selectedSubjects: {!! $grade->subjects->pluck('name') !!},
            }
        });
    });
</script>

THIS CANNOT BE DONE... IT'S IMPOSSIBLE!!! I GIVE UP

Nov
03
3 weeks ago
Activity icon

Started a new Conversation Does Check Boxes Has To Have A Unique Id?

Hi all.. When I try to click a single checkbox, others also get selected automatically.

public function edit($id)
    {
        $classes = Grade::with('subjects')->get();
        return view('profiles.edit', compact('classes'));  
    }

This is the collection I get when I dd($classes)

array:3 [▼
  0 => array:7 [▼
    "id" => 1
    "name" => "Grade 8"
    "slug" => "grade-8"
    "description" => "This is Grade Eight"
    "created_at" => "2020-10-31T17:35:41.000000Z"
    "updated_at" => "2020-10-31T17:35:41.000000Z"
    "subjects" => array:1 [▼
      0 => array:7 [▼
        "id" => 1
        "name" => "Mathematics"
        "slug" => "matematics"
        "description" => "This is Mathematics"
        "created_at" => "2020-10-31T17:34:29.000000Z"
        "updated_at" => "2020-11-02T20:58:31.000000Z"
        "pivot" => array:2 [▶]
      ]
    ]
  ]
  1 => array:7 [▶]
  2 => array:7 [▶]
]

How do I give each checkbox a unique id?...

 @foreach($classes as $class)
                    <div class="card shadow mx-2 my-4">
                        <div class="card-header py-3">
                            <h6 class="m-0 font-weight-bold text-primary">
                                <div class="form-check">
                                    <input class="form-check-input" type="checkbox"  id="{{$class->id}}" name="classes[]" value="" />
                                    <label class="form-check-label" for="{{$class->id}}">
                                        {{$class->name}}
                                    </label>
                                </div>
                            </h6>
                        </div>
                        <div class="card-body">
                        @foreach($class->subjects as $subject)
                            <div class="form-check">
                                <input class="form-check-input" type="checkbox"  id="{{$subject->id}}" name="subjects[]" value="" />
                                <label class="form-check-label" for="{{$subject->id}}">
                                    {{$subject->name}}
                                </label>
                            </div>
                            @endforeach
                        </div>
                    </div>
                    @endforeach

Nov
02
3 weeks ago
Activity icon

Started a new Conversation Display A Grade With Their Subjects

Hi... I have eager loaded grades with their subjects from the database

public function edit($id)
    {
        $classes = Grade::with('subjects')->get();
       
        return view('profiles.edit', compact('classes'));  
    }

I want to display then in such a way that, when a user select a grade from the multiple select menu, all subjects associated with that grade must be display below in the

    tags with vue js

    <div class="form-group">
           <select id="grade" name="grade" class="form-control" multiple v-model="subjects">
                    @foreach($classes as $class)
                       <option value="{{ $class->name }}" {{ old('class', $user->class) == $class->name ? 'selected' : "" }}>{{ $class->name }}</option>
                      @endforeach
           </select>              
    </div>
    
    <ul>
     	@foreach($class->subjects as $subject)
                  <li>@{{ subjects }}</li>
            @endforeach
    </ul> 
    
    

    My vue.js

    @section('js_index_page')
    <script>
        window.addEventListener('load',function(){
            var app = new Vue({
                el: '#app',
                data:{
                    subjects: ''
                }
            });
        });
    </script>
    @endsection
    
Activity icon

Replied to Why Am I Getting An Integer Not An Array?

related is a string with a value of null

Activity icon

Replied to Why Am I Getting An Integer Not An Array?

Yes.. but I want to add the new user_id into an existing array which has values already... how to push the user_id into that array?

Activity icon

Started a new Conversation Why Am I Getting An Integer Not An Array?

I am trying to push $user_id into an array and store it in a database

but when I dd($children) I get an integer value, not an array... what am I doing wrong?

if($user->hasRole(['guardian','learner'])){
               
                $user_id = $user->id;
                $db =  Auth::user()->related;
                $children = array_push($db, $user_id);

                dd($children);
.
.
.
.


                return redirect()->route('parent-child.index')->with('message', $user->name . ' has been added Successfully');
            }
Nov
01
3 weeks ago
Activity icon

Replied to How To Display Grades In A Dropdown And Related Subjects In Checkboxes?

I'm using Javascript (JQuery to be specific)

Activity icon

Started a new Conversation How To Display Grades In A Dropdown And Related Subjects In Checkboxes?

Hi all... I have eager loaded all the grades with the subjects from the database. I want to display it in a cascading select box with checkboxes below...

I want it when a user selects a Grade from the dropdown menu, a group of subjects related to that Grade should be displayed below dynamically...

My Controller Method

public function create()
    {        
        $grades = Grade::with('subjects')->orderBy('slug', 'asc')->get();
        return view('admin.users.create', compact( 'grades'));
    }

create.blade.php

<select id="grade" name="grade" class="form-control @error('grade') is-invalid @enderror" value="{{ old('grade') }}">
                <option value="">Choose a Grade...</option>
                @foreach($grades as $grade)
                    <option value="{{ $grade->name }}" {{ old('grade', $grade) == $grade->name ? 'selected' : "" }}>{{ 								$grade->name }}</option>                                            
                @endforeach                
            </select>

@foreach($grade->subjects as $subject)
    <div class="custom-control custom-checkbox mt-2">
        <input type="checkbox" class="custom-control-input @error('subjects') is-invalid @enderror" id="{{$subject->slug}}" name="subjects[]" value="{{$subject->name}}" 
        {{ (is_array(old('subjects')) && in_array( $subject->name, old('subjects', $subjects))) ? 'checked' : '' }} />
        <label class="custom-control-label" for="{{$subject->slug}}">{{$subject->name}}</label>
    </div>
    @endforeach
Oct
29
3 weeks ago
Activity icon

Started a new Conversation A Welcome Email With Password Setup

Hi, I have a question...

I want to create a system where the superadmin or admin creates a new user and then the system sends an email to the user with a link to setup a new password so they can login... how will you go about creating that?

Event

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

use App\User;

class UserRegistered
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('channel-name');
    }
}

Listener

<?php

namespace App\Listeners;

use App\Events\UserRegistered;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Mail;
use App\Mail\WelcomeMail;
// use Illuminate\Support\Facades\Mail;

class SendWelcomeEmail 
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  UserRegistered  $event
     * @return void
     */
    public function handle(UserRegistered $event)
    {
        Mail::to($event->user->email)->send(new WelcomeMail($event->user));
    }
}

Markdown Email

@component('mail::message')
# Welcome to Kwakhanya Learning Centre

Hello {{ $user->name }} {{ $user->last_name }} 

@component('mail::panel')
You can use either your email or mobile number as your username<br>
Your Username: {{ $user->email}} or {{ $user->phone }}<br>

Please click the button below to setup your password to login into the system
@endcomponent

@component('mail::button', ['url' => 'http://127.0.0.1:8000/password/set'])
Create Password
@endcomponent

Thanks,<br>
{{ config('app.name') }}
@endcomponent

How to send this email with the reset Password link in the button so a user can set his/her own password

Oct
27
4 weeks ago
Activity icon

Replied to Laravel Vue Cannot Get Old Value Because Of V-model

I want when a user select a certain role from the dropdown list, a div element below must shown or hidden based on the users selection from the list...

<div class="col-md-6" v-if="roleType == 'learner'">
.
.
.
.

</div>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            roleType: '',
        },
    });
</script>
Activity icon

Started a new Conversation Laravel Vue Cannot Get Old Value Because Of V-model

How do I get the old value of the drop down box when validation fails?... The problem is when I include v-model in my select box, but when I remove it, works fine. How to solve this problem?

 <select id="role" name="role" class="form-control @error('role') is-invalid @enderror" value="{{ old('role') }}" v-model="roleType">
 <option value="">Choose user role...</option>

@foreach($roles as $role)
 <option value="{{ $role->name }}" {{ old('role', $role ) == $role->name ? 'selected' : '' }}>{{ $role->display_name }}</option>
                                                
  @endforeach
 </select>

The old function doesn't work when I include the v-model... please help

Oct
26
4 weeks ago
Activity icon

Replied to The Slug Can't Be Displayed In The Text Box

Thanks for the info dude

Activity icon

Started a new Conversation The Slug Can't Be Displayed In The Text Box

Hi all I'm trying to generate a slug in the second textbox but it cant be displayed, I only get an error:

Use of undefined constant slug - assumed 'slug' (this will throw an Error in a future version of PHP) (View: C:\xampp\htdocs\Kwakhanya-Centre\resources\views\admin\subjects\create.blade.php)

here is my code: create.blade.php

@extends('layouts.admin')

@section('main-content')
    <!-- Page Heading -->
    <h1 class="h3 mb-4 text-gray-800">{{ __('Create New Subject') }}</h1>
    <hr/>
    @if (session('message'))
    <div class="alert alert-success border-left-success alert-dismissible fade show" role="alert">
        {{ session('message') }}
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    @endif
    <form method="POST" action="{{ route('subjects.store')}}">                
        @csrf
        <div class="row justify-content-center">
            <div class="col-md-12">
                <div class="card shadow mb-4">
                    <div class="card-body">
                        <h4 class="font-weight-bold">Subject Details:</h4>
                    <hr>
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="name" class="font-weight-bold">Subject Name</label>
                                    <input type="text" name="name" v-model="name" class="form-control @error('name') is-invalid @enderror" id="name" value="{{ old('name') }}">
                                    @error('name')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                                </div>
                                <div class="form-group">
                                    <label for="name" class="font-weight-bold">Slug</label>
                                    <input type="text" name="slug" class="form-control @error('slug') is-invalid @enderror" id="slug" value="{{ slug }}">
                                    @error('slug')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                                </div>

                                <div class="form-group">
                                    <label for="description" class="font-weight-bold">Description</label>
                                    <input type="text" name="description" class="form-control  @error('description') is-invalid @enderror" id="description" value="{{ old('description') }}">
                                    @error('description')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                    @enderror
                                </div>
                             
                            </div>
                        </div>
                    </div>
                </div>
                <!-- Permissions -->
                
                <div class="col-md-12 mb-4">
                    <button type="submit" class="btn btn-primary btn-lg"><i class="fas fa-book fa-fw mr-2"></i>Create Subject</button> 
                    <a href="/grades"  class="btn btn-secondary btn-lg ml-4"><i class="fas fa-ban fa-fw mr-2"></i>Cancel</a>                
                </div>
            </div>
        </div>
    </form>
@endsection

@section('js_index_page')
<script>
    new Vue({
        el: '#app',
        data: {
            name: "",
        },
  computed: {
    slug: function() {
      var slug = this.sanitizeTitle(this.name);
      return slug;
    }
  },
  methods: {
    sanitizeTitle: function(name) {
      var slug = "";
      // Change to lower case
      var nameLower = name.toLowerCase();
      // Letter "e"
      slug = nameLower.replace(/e|é|è|ẽ|ẻ|ẹ|ê|ế|ề|ễ|ể|ệ/gi, 'e');
      // Letter "a"
      slug = slug.replace(/a|á|à|ã|ả|ạ|ă|ắ|ằ|ẵ|ẳ|ặ|â|ấ|ầ|ẫ|ẩ|ậ/gi, 'a');
      // Letter "o"
      slug = slug.replace(/o|ó|ò|õ|ỏ|ọ|ô|ố|ồ|ỗ|ổ|ộ|ơ|ớ|ờ|ỡ|ở|ợ/gi, 'o');
      // Letter "u"
      slug = slug.replace(/u|ú|ù|ũ|ủ|ụ|ư|ứ|ừ|ữ|ử|ự/gi, 'u');
      // Letter "d"
      slug = slug.replace(/đ/gi, 'd');
      // Trim the last whitespace
      slug = slug.replace(/\s*$/g, '');
      // Change whitespace to "-"
      slug = slug.replace(/\s+/g, '-');
      
      return slug;
    }
  }
});
</script>

@endsection

everything else is working fine, but how do I display the text I typed in the first text box to the second textbox as a slug?

Oct
23
1 month ago
Activity icon

Replied to Redirecting User To The Edit Profile Page

@silencebringer I tried your importing

use App\Http\Controllers\Auth\Request;

it didn't work, but then I tried importing

use Illuminate\Http\Request;

It work!!!, I don't know how... but anyway thanks dude