Search code examples
phplaraveleloquenteloquent-relationship

Laravel: Pivot table access in a blade


I'm a newbie to Laravel so I'm probably making some sort of noob error. That said, here's my problem.

I'm trying to access a related table from within my blade,. Here are relevant portions of my models:

class _MeetingUser extends Model
{
    ...
    public function meeting()
    {
        return $this->belongsToMany(Meetings::class);
    }
    public function user()
    {
        return $this->belongsToMany(Users::class);
    }
}
class Meetings extends Model
{
    ...
    public function users()
    {
        return $this->belongsToMany(Users::class, 'meeting__user');
    }
    ...

I have this in my controller:

class MeetingController extends Controller
{
    public function edit(Request $request): View
    {
        return view('meetings.edit', [
            'meetings' => Meetings::all(),
            'secretaries' => Users::all(),
        ]);
    ...

And my blade has this:

             @foreach($meetings as $meeting)
            ...
                        <td><select name="secretaries[]" id="secretaries">
                                <option value="0">Select Secretary</option>
                                @foreach($secretaries as $secretary)
                                    <option value="{{$secretary->id}}"
                                    @if($meeting->users()->id == $secretary->id)
                                        selected="selected"
                                    @endif
                                    >{{$secretary->name}}</option>
                                @endforeach
                            </select>
                        </td>
            ...

What I tried is detailed above.

What I expect is for the related user id to be selected in my dropdown. When I load the page, I get this:

Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$id

Although I can see it would be wrong, the same thing occurs when I try users_id instead of id.

The pivot table works in the controller. What am I doing wrong?

UPDATE: This works. But it seems ugly. Isn't there a nice ORM way to do this?

  {{  $existing_secretary_id=DB::scalar('select users_id from meeting__user where meetings_id = ?',
                                [$meeting->id]);
                        }}
                        <td><select name="secretaries[]" id="secretaries">
                                <option value="0">Select Secretary</option>
                                @foreach($secretaries as $secretary)
                                    <option value="{{$secretary->id}}"
                                    @if($secretary->id == $existing_secretary_id)
                                        selected="selected"
                                    @endif
                                    >{{$secretary->name}}</option>
                                @endforeach
                            </select>
                        </td>

Solution

  • I extensively rewrote this to do eager loading in the controller. As I was working through the process, I realized what the original problem was.

    @if($meeting->users()->id == $secretary->id)
    

    $meeting->users() is a collection. To extract the id, I needed $meeting->users()->first()->id; The problem stemmed from my using a many-to-many relationship between users and meetings (which I want for future changes) and expecting user() to be a single object.