Your relationships are wrong, If you speak your relationships, your Group model has many user. So, you have to define that relationship this way:
class Group extends Model
{
public function users()
{
return $this->HasMany('App\User');
}
}
Now, your User model belongs to one Group, so it should be:
class User extends Authenticatable
{
public function group()
{
return $this->belongsTo('App\Group');
}
}
** Notice that the function is called group not groups, that way when you read your relationship it will more intuitive.
As for your question, I don't know how to answer it since is not clear for me. Could you explain a little bit more or give me more context.
For example, if you are trying to access it from a controller where you already have the Group then you can simply do this:
public function view(Group $group)
{
$group->load('users');
return view('paises.index', compact('group'));
}
Now in your view, for example:
@foreach ($group->users as $user)
{{ $user->id }} {{ $user->name }}
@endforeach
The ->load() function is called Lazy Eager Load
Oh I did many to many relation. Now i see. Thanks for advice about naming functions it will be helpfull for me.
zuku199106 said:
i want to select all users which are belongs to group with id 1 (for example). How to do that using query builder? There is any way to do it?
I can use raw query : select users.* from users where users.id_group = 1; but i want to know it there are another methods.
I always used User::orderBy('surname')->get(); method to retrieve data.
I want to make list of registered users in my admin panel. I specified route:
Route::any('/users/list/{id?}','UsersController@list);
When I reach route without id (/users/list/), view should contain all users from the database. When I reach route with id (/users/list/1), view should contain only users which are belongs to group with that id passed to route.
For changing id's I used select field and jquery redirect. So how to select only users which are to group with id 1.
I can select all users and then in my view and then in the loop check if they group belongs to group with selected id. If not just to show them. But i think it isn't good solution?
Controller:
public function list($id = null)
{
if(is_null($id))
{
$users = User::orderBy('name')->get();
}
else
{
//I dont know whats here
}
return view('users.lists', ['users' => $users, 'groups'=> Group::orderBy('name')->get(), 'id'=>$id]);
}
View:
@foreach($groups as $g)
@if($id)
<option value="{{$g->id}}" selected>{{$g->name}}</option>
@else
<option value="{{$g->id}}">{{$g->name}}</option>
@endif
@endforeach
<table>
@foreach($users as $user)
{{-- checking if user belongs to group --}}
<tr>
<td><a href="{{ route('show_user_details', [$user->id]) }}">{{ $user->surname }} {{ $user->forename }}</a></td>
<td>{{ $user ->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->created_at }}</td>
<td>{{ $user->updated_at }}</td>
<td>
@if(!empty($user->groups()))
{{ $user->groups }}
@endif
</td>
</tr>
@endforeach
</table>
Is this what you are looking for?
public function list($id = null)
{
if(is_null($id))
{
$users = User::orderBy('name')->get();
}
else
{
$users = Group::find($id)->users;
}
return view('users.lists', [
'users' => $users,
'groups'=> Group::orderBy('name')->get(),
'id'=>$id
]);
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community