Julles
Julles

Reputation: 41

Laravel: Passing data to view through route after querying

I recently asked a question about defining many to many relationships (using belongsToMany) and it was a huge help. So now in my models I have:

Users model

 public function subjects()
 {
     return $this->belongsToMany('App\Subject', 'users_subjects', 'user_id', 'subjects_id');
 }

Subjects model

  public function users()
  {
      return $this->belongsToMany('App\User', 'users_subjects', 'subject_id', 'user_id');
  }

This way I establish a relationship between users and subjects via the users_subjects table. My next step was to create a controller, SubjectsController, which ended up looking like this:

class SubjectsController extends Controller
{
  // returns the view where subjects will be displayed
  public function index()
  {
    return view('profiles.professor.prof_didactic_subjects');
  }

  // get users with subjects
  public function getSubjects()
  {
    $subjects = User::with('subjects')->get();
  }

  // get a single user with a subject
  public function getSubject($id)
  {
    $materia = User::where('id', '=', $id)->with('subjects')->first();
  }

}

I'm not very sure about the code in the controller though.

The final step is where it gets tricky for me, even after reading the docs: I want to pass each result to the view, so I can have multiple tiles, each populated with data from subjects the user is associated with:

@foreach ($subjects as $subject)
      <div class="tile is-parent">
        <article class="tile is-child box">
          <p class="title">{{ $subject['name'] }}</p>
          <div class="content">
            <p>{{ $subject['description'] }}</p>
          </div>
        </article>
      </div> 
@endforeach

I tried many different route configurations, but kept getting either the undefined variable error or trying to access non-object error.

What's the proper course of action here? I feel I'm missing something very basic. Thanks in advance for any help.


The answer

The solution provided below by @Sohel0415 worked perfectly. My index() method on the controller now looks like this:

  public function index()
  {
    // temporary value while I figure out how to get the id of the current user
    $user_id = 6; 

    $subjects = Subject::whereHas('users', function($q) use ($user_id){
      $q->where('user_id', $user_id);
    })->get();
    return view('profiles.professor.prof_didactic_subjects')->with('subjects', $subjects);
  }

My route looks like this:

Route::get('/professor', 'SubjectsController@index');

I was pretty lost, so this absolutely saved me, thanks again :)

Upvotes: 2

Views: 94

Answers (1)

Sohel0415
Sohel0415

Reputation: 9853

You need to pass $subjects to your view. You can use compact() method for that like -

public function index()
{
    $subjects = Subject::with('users')->get();
    return view('profiles.professor.prof_didactic_subjects', compact('subjects'));
}

Or using with() method like -

public function index()
{
    $subjects = Subject::with('users')->get();
    return view('profiles.professor.prof_didactic_subjects')->with('subjects', $subjects);
}

If you want to get Subject for a particular user_id, use whereHas() -

$subjects = Subject::whereHas('users', function($q) use ($user_id){
      $q->where('user_id', $user_id);
})->get();

Upvotes: 2

Related Questions