skip to Main Content

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: IlluminateDatabaseEloquentRelationsBelongsToMany::$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>

2

Answers


  1. Chosen as BEST ANSWER

    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.


    1. Your pivot model name is incorrect. Change the pivot model name to MeetingUser without an underscore. MeetingUser is okay
    2. You don’t need any relationship in Pivot table. I mean
    class MeetingUser extends Model
    {
      // Specify the custom table name for the pivot model.
      protected $table = "your_db_table_name";
      ...
    }
    
    1. Check the correct name of your pivot table in the database; if it’s meeting__user, you may want to consider changing it to meeting_users for better convention. If you want to keep the existing name, adjust the relationship in Meetings model like this:
    class Meetings extends Model
    {
        ...
        public function users()
        {
            return $this->belongsToMany(Users::class, 'meeting__user','meeting_id','user_id');
        }
        ...
    

    Make sure to make a similar adjustment in the User model.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search