Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

srikanthgopi's avatar

Category and Profile

I have 3 tables (professionals, professional_categories, professional_profiles). Professionalprofile table has a field professional_id that references id on professionals and a category_id which references with id on professional_categories).

In my professionals controller public function search() { $professionals = Professional::with('category', 'profile')->paginate(10); return view('search.professionals.index', compact('professionals')); }

On the blade views how to fetch professional profile in @foreach loop.

Note: i Have added belongsToMany('ProfessionalProfile::class') in ProfessionalCategory model. And also added the below in ProfessionalProfile

public function professional() { return $this->belongsTo(Professional::class); }

public function category() { return $this->belongsTo(ProfessionalCategory::class); }

I'm still in the learning stage and trying to learn from every mistake. Please do correct me.

1 like
11 replies
tisuchi's avatar

what I can tell you from your code-

  • A Professional belongs to a Category <=> A Category has many Professional
  • A Professional has one Profile <=> A Profile belongs to a Professional

Here is the relationships.

Professional.php

public function category() { 
    return $this->belongsTo('Professionalcategory'); //adjust model name and foreign key
}

public function profile() { 
    return $this->belongsTo('Professionalprofile'); //adjust model name and foreign key
}

Professionalcategory.php

public function professional() { 
    return $this->hasMany('Professionalcategory'); //adjust model name and foreign key
}

Professionalprofile.php

public function profile() { 
    return $this->belongsTo('Professional'); //adjust model name and foreign key
}

Don't forget to adjust your table name and foreign key if it is different than convention.

3 likes
srikanthgopi's avatar

@tisuchi How do i get them on the view? My Controller code is

public function search() { $professionals = Professional::with('category', 'profile')->paginate(10); return view('search.professionals.index', compact('professionals')); }

i tried $professional->profile->qualification and $professional->professional_profile->qualification Neither work.

Couldn't find proper related resource to find help online.

1 like
tisuchi's avatar

@srikanthgopi

This line should return qualification. (Make sure you have qualification column in professional profile table)

$professionals->profile->qualification;

If its not working, just return your function like this way and show the output.

public function search() { 
    $professionals = Professional::with('category', 'profile')->first(); 
    return $professionals;
}
3 likes
srikanthgopi's avatar

@tisuchi Here is what it displays

{"current_page":1,"data":[{"id":1,"name":"Srikanth Gopi","username":"srikanthgopi","phone":"8520900469","email":"shrikanth003@gmail.com","avatar":"srikanthgopi.jpg","slug":"srikanthgopi","gender":1,"created_at":"2017-12-27 21:00:52","updated_at":"2017-12-27 21:00:52","profile":null,"category":null},{"id":2,"name":"Harika Maddali","username":"harikamaddali","phone":"7386740495","email":"[email protected]","avatar":"harikamaddali.jpg","slug":"harikamaddali","gender":0,"created_at":"2017-12-27 23:44:12","updated_at":"2017-12-27 23:44:12","profile":null,"category":null},{"id":3,"name":"Dr Soumya Podduturi","username":"drsoumyapodduturi","phone":"987654321","email":"[email protected]","avatar":"drsoumyapodduturi.jpg","slug":"drsoumyapodduturi","gender":0,"created_at":"2017-12-28 00:37:00","updated_at":"2017-12-28 00:37:00","profile":null,"category":null},{"id":4,"name":"Dr Sruthi Alla","username":"drsruthialla","phone":"1234567890","email":"[email protected]","avatar":"drsruthialla.jpg","slug":"drsruthialla","gender":0,"created_at":"2017-12-28 00:39:25","updated_at":"2017-12-28 00:39:25","profile":null,"category":null},{"id":5,"name":"Dr Sruthi Gondi","username":"drsruthigondi","phone":"456789123","email":"[email protected]","avatar":"drsruthigondi.jpg","slug":"drsruthigondi","gender":0,"created_at":"2017-12-28 00:42:09","updated_at":"2017-12-28 00:42:09","profile":null,"category":null},{"id":6,"name":"Dr. Shuba Dharmana","username":"drshubadharmana","phone":"456456456","email":"[email protected]","avatar":"drshubadharmana.jpg","slug":"drshubadharmana","gender":0,"created_at":"2017-12-28 00:46:20","updated_at":"2017-12-28 00:46:20","profile":null,"category":null},{"id":7,"name":"Dr Rashmi Shetty","username":"drrashmishetty","phone":"987987987","email":"[email protected]","avatar":"drrashmishetty.jpg","slug":"drrashmishetty","gender":0,"created_at":"2017-12-28 00:56:10","updated_at":"2017-12-28 00:56:10","profile":null,"category":null},{"id":8,"name":"Dr. Vidushi Jain","username":"drvidushijain","phone":"123123123","email":"[email protected]","avatar":"drvidushijain.jpg","slug":"drvidushijain","gender":0,"created_at":"2017-12-28 01:03:18","updated_at":"2017-12-28 01:03:18","profile":null,"category":null},{"id":9,"name":"Dr. Smitha Allagadda","username":"drsmithaallagadda","phone":"951426","email":"[email protected]","avatar":"drsmithaallagadda.jpg","slug":"drsmithaallagadda","gender":0,"created_at":"2017-12-28 01:09:08","updated_at":"2017-12-28 01:09:08","profile":null,"category":null},{"id":10,"name":"Dr. Praveen Kumar Raju","username":"drpraveenkumarraju","phone":"7535645","email":"[email protected]","avatar":"drpraveenkumarraju.jpg","slug":"drpraveenkumarraju","gender":1,"created_at":"2017-12-28 01:14:05","updated_at":"2017-12-28 01:14:05","profile":null,"category":null}],"first_page_url":"http://localhost:8000/search/professionals?page=1","from":1,"last_page":2,"last_page_url":"http://localhost:8000/search/professionals?page=2","next_page_url":"http://localhost:8000/search/professionals?page=2","path":"http://localhost:8000/search/professionals","per_page":10,"prev_page_url":null,"to":10,"total":11}

Im using forelse loop like this

@forelse($professionals as $professional) @empty No Results to display @endforelse

1 like
tisuchi's avatar

@srikanthgopi

It's not returning any relationship data. Surely something wrong in your foreign key adjustment.

Professional.php

public function category() { 
    //make sure professionalcategory has a column name called professional_id
    return $this->belongsTo('Professionalcategory'); 
}

public function profile() { 
    //make sure professionalprofile has a column name called professional_id
    return $this->belongsTo('Professionalprofile'); 
}

Make sure foreign key.

Check there- Ref: https://laravel.com/docs/5.5/eloquent-relationships#one-to-many

3 likes
tisuchi's avatar

show your database table structure, too.

2 likes
srikanthgopi's avatar

MySQL Database Tables Professionals - id, name, username, phone, email, password, avatar, slug, gender, remember_token, created_at, updated_at

professional_categories - id, parent_id, name, slug, description, image, created_at, updated_at

professional_profiles - id, professional_id, category_id, subcategory_id, specialization, qualification, ..., created_at, updated_at

I updated the same on the link too

I also have another guard Business which has similar Business, business_categories and business_profiles tables.

1 like
srikanthgopi's avatar

Also to mention, i have used category_id on professionalprofiles table instead of professionals table

1 like
tisuchi's avatar
tisuchi
Best Answer
Level 70

The answer looks like too big. This is what you need to have in your table-

Professionals - 
    id
    name
    username
    phone
    email
    password
    avatar
    slug
    gender
    remember_token
    created_at
    updated_at



professional_categories - 
    id
    parent_id
    professional_id     **this column I have added
    name
    slug
    description
    image
    created_at
    updated_at


professional_profiles - 
    id
    professional_id     **this column I have added
    category_id
    professionalcategory_id     **this column I have added
    subcategory_id
    specialization
    qualification
    created_at
    updated_at

Now check the code carefully.

ProfessionalController.php

class ProfessionalController extends Controller
{
    public function search() {
      $professionals = Professional::with('profile', 'category')->paginate(10);
      return view('search.professionals.index')
                ->with('professionals', $professionals);
    }
}

Professional.php (Model)

class Professional extends Authenticatable
{
    
    public function profile() {
      return $this->hasOne(ProfessionalProfile::class, 'professional_id');
    }

    public function category() {
      return $this->belongsTo(ProfessionalCategory::class, 'professional_id');
    }
}

ProfessionalProfile.php (Model)

class ProfessionalProfile extends Model
{
  
  public function professional() {
    return $this->belongsTo(Professional::class, 'professional_id');
  }
}

ProfessionalCategory.php (Model)

class ProfessionalCategory extends Model
{
  public function professional() {
    return $this->hasMany(Professional::class, 'professional_id');
  }
}

Blade View

<div class="col-md-9">
        @forelse ($professionals as $professional)
          <div class="row pt-md-2 pb-md-2 bg-white border">
            <div class="col-2 col-md-2 border-right">
              <a href="{{ url('search/professionals/'.$professional->slug) }}"><img class="img-fluid " src="{{Storage::url('uploads/avatars/professionals/'.$professional->avatar)}}" height="100%"/></a>
            </div>
            <div class="col-8 col-md-8 border-right">
              <a href="{{ url('search/professionals/'.$professional->slug) }}" class="h5">{{$professional->name}}</a><br />
              <span>Dental Hygenist - Dentist | Periodontologiy and Oral Implantology</span><br />
              <span>{{$professional->profile->qualification}}</span>, <span>6 Years Experience</span>, <span>Registration : 8520125</span><br />
              <small class="text-muted"><i class="fas fa-map-marker-alt text-blue"></i> <a href="">Floss and Gloss Advance Dental Experts</a> - Kondapur, Hyderabad, Telangana, India</small><br />
              <span><i class="far fa-clock"></i> Mon-Fri 9AM - 9PM</span> <span><i class="far fa-money-bill-alt"></i> 500 INR per consultation</span>
            </div>
            <div class="col-2 col-md-2 text-center">
              <a href="" class="">253 Reviews</a><br />
              <a href=""><i class="fab fa-facebook-square"></i></a> <a href=""><i class="fab fa-twitter-square"></i></a> <a href=""><i class="fab fa-linkedin"></i></a> <a href=""><i class="fab fa-google"></i></a> <a href=""><i class="fab fa-youtube"></i></a>
              <a class="small" href="">Insurance Providers</a>
              <a href="{{url('search/professionals/'.$professional->slug)}}" class="btn btn-sm btn-primary">Full Profile</a>
            </div>
          </div>
        @empty
          No Results to display
        @endforelse
        <div class="pt-md-2 text-center">
          {{ $professionals->links('vendor.pagination.bootstrap-4') }}
        </div>
  </div>

MY SUGGESTION: Read the relationship mechanism properly to understand how it works. https://laravel.com/docs/5.5/eloquent-relationships

6 likes
srikanthgopi's avatar

Thank you @tisuchi I'll definitely go through the link and your suggestions again and try to implement with few examples to learn this.

Your code has worked for me.

1 like

Please or to participate in this conversation.