2
bookmaker table
id
name


bookmaker_user table
id 
account_name
bookmaker_id
user_id


user table
id
name

User Model :
public function bookmakers(){
    return $this->belongsToMany('Bookmaker', 'bookmaker_user', 'user_id', 'bookmaker_id')
            ->withPivot('id', 'accountname')->withTimestamps();
}


BookmakerController.php
public function update($id)
{
            $bookname = Input::get('booknamemodifselect');
            $accountname = Input::get('accountnamemodifinput');
            $bankrollinvested = Input::get('bankrollinvestedmodifinput');
            $bonus = Input::get('bonusmodifinput');
            $bankrollamount = Input::get('bankrollamountmodifinput');

            $bookmodif = DB::table('bookmakers')->where('name', $bookname)->first();


            $bookmaker = $this->user->bookmakers()->where('bookmaker_user.id','=',$id)->first();
            $bookmaker->pivot->bookmaker_id = $bookmodif->id;
            $bookmaker->pivot->save();
    }

$id is the id of the account

$this->user is the user authentified.

I want to update the bookmaker for an account by his id (by account id I mean) that belongs to the user auth. Because the user auth have multiple entries with the same bookmaker but different account name. It says 'Trying to get property of non-object'.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
edofthadead
  • 47
  • 1
  • 7
  • On which line exactly do you get the "Trying to get property of non-object" error? – lukasgeiter Nov 01 '14 at 18:32
  • $bookmaker->pivot->bookmaker_id = $bookmodif->id; – edofthadead Nov 01 '14 at 18:36
  • Could you check if it really finds a bookmaker? I suppose `$bookmodif` is null? – lukasgeiter Nov 01 '14 at 19:11
  • it really find a bookmaker but now it shows 'Trying to get property of non-object' when i change something... so weird – edofthadead Nov 01 '14 at 19:28
  • Ok this is not really about your problem, but don't you think the bookmaker_user table is a bit more than just a pivot table. I think it should get its own Model (`Account`) maybe. You are saying you have multiple records in the table that have the same user_id and bookmaker_id. Normally that shouldn't be the case with a many to many relationship – lukasgeiter Nov 01 '14 at 20:23
  • that's what i thought too – edofthadead Nov 01 '14 at 20:26
  • I would go for it. If you need some help, let me know – lukasgeiter Nov 01 '14 at 20:28
  • i find it in a successfull way but when i do a save() it doesn't save in my mysql db. – edofthadead Nov 01 '14 at 20:39
  • Yes I had that too when I tried it locally. If you wanna stay with this db setup for the moment I suggest you us the Fluent Query Builder to directly update it. Give me a few minutes, I'll write up a proper answer ;) – lukasgeiter Nov 01 '14 at 20:41

2 Answers2

5

To sum up the comments below the question, it isn't a very common database design. And this also means, that it would probably be a good idea to change that. By converting the bookmaker_user pivot table into a full grown table with its own model class. Reasons for that are:

  • Multiple records that belong to the same user and bookmaker (many-to-many should only have one)
  • Additional data stored in the pivot table (while normally okay, it adds to the first point)
  • Easier handling of changes (e.g. changing the bookmaker of an account)

But that's not really the problem the question poses so here's that:

I tried it out locally and I didn't get any errors, but I wouldn't save to the database.
It seems to be some kind of bug or unexpected behavior. However a workaround is using the Query Builder.
In my opinion its even more elegant for this case...

$bookmodif = DB::table('bookmakers')->where('name', $bookname)->first();

if($bookmodif !== null){  // just to make sure the bookmaker exists
    DB::table('bookmaker_user')
        ->where('id', $id)
        ->update(array('bookmaker_id' => $bookmodif->id);
}
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
0

I've been dealing with this. You can do:

$user = $this->user->bookmakers()->having('pivot_id', $myPivotId)->first(); 
echo $user->pivot->bookmaker_id; // bookmaker_id of the pivot row with id $myPivotId
antirealm
  • 408
  • 3
  • 10