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'.
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:
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);
}