I need that favourites table is a pivot table from users and entrys.
My Models
class User extends Model {
public function entrys() {
return $this->hasMany('App\Entrys');
}
public function favourites() {
return $this->hasMany('App\Favourite');
}
}
class Entrys extends Moldel {
public function favourite() {
return $this->hasMany('App\Favourite');
}
public function user()
{
return $this->belongsToMany('App\User');
}
}
class Favourite extends Model {
public function entrys() {
return $this->belongsTo('App\Entry');
}
public function user() {
return $this->belongsTo('App\User');
}
}
class User extends Model {
public function entrys() {
return $this->belongsToMany('App\Entrys', 'favourites');
}
}
class Entrys extends Moldel {
public function users()
{
return $this->belongsToMany('App\User', 'favourites');
}
}
you need belongsToMany relation on both sides, no need for Favourite model at all (unless you need it, ie. favourites is not just pivot table)
You need to specify pivot table name in the belongsToMany since it is not following the convention
I suggest you rename the entrys table and definitely Entrys model, because now it's: incorrect & not meningful
If you really need Favourite model, then it has belongsTo relations to both User and Entry, and there will be hasMany on the other sides
class User extends Model {
public function entries() {
// you need to specify the pivot table, since eloquent would search for entry_user as default
return $this->belongsToMany('App\Entry', 'favourites');
}
}
class Entry extends Model {
// if you rename the table to entries, then it's not necessary.
// Otherwise you need to specify the table:
protected $table = 'entrys';
public function users()
{
return $this->belongsToMany('App\User', 'favourites');
}
}
// now:
$entry = Entry::find($someId);
$entry->users; // collection of users
User::find($userId)->entries; // collection of related entries
$allEntry = Entry::all();
foreach($allEntry as $entry) {
echo $entry->user->favourites;
}
So this should check if a user has favourites...but i habe big problems to get it work :( @JarekTkaczyk, i tried your solution, but this not work for my case :(
@testy You're not doing it right. A favourite in this schema means, that a User has related Entry (or entries for that matter).
So here's something like what you asked for:
$users = User::with('entries')->get();
foreach ($users as $user)
{
if (count($user->entries))
{
// user has favourited at least one Entry, ->entries is full collection of all of them
}
else
{
// user have no favourite entries
}
}
@testy I told you it was not right. So instead tell me what you want to achieve - without pasting any code. Just say. For example who is ->user ?
Now, this is incorrect because obviously $entry->user is not there, $entry->users would be ok, but it is a collection, so you can't call $entry->users->favourite...
Ok, i will output a list of entrys. On the right is an icon that show if the current user has this entry as favourite. (In the favourites table). But I would also have access to all of a user's favourites ( in another view).
@testy Yeah, that's what I meant. Here it goes then:
// Assuming $user is current user, eg. got from Auth::getUser()
$favourites = $user->entries()->select('entries.id')->lists('id');
// show all with additional info if favourited
@foreach ($entries as $entry)
Title: {{ $entry->title }}
User has this entry as favourite? {{ in_array($entry->id, $favourites) ? 'yep' : 'no' }}
@endforeach
// another view - show only favourited
@foreach ($user->entries as $entry)
Title: {{ $entry->title }}
@endforeach