Files
Sistema-Educativo-Laravel/app/Http/Controllers/UserSearchController.php
fernando 97c04fbfb4 feat: funcionalidad de red social (feed, amigos, búsqueda)
- Migraciones: posts, post_likes, post_comments
- Modelos Post, PostLike, PostComment con relaciones
- User: métodos friendshipWith, isFriendWith, friendIds, posts
- PostController: feed, crear publicación con imagen, like toggle, comentarios, eliminar
- FriendshipController: listar solicitudes, enviar, aceptar, rechazar, toggle AJAX
- SocialProfileController: perfil público con publicaciones y estado de amistad
- UserSearchController: búsqueda AJAX por nombre con estado de amistad
- FriendRequestNotification: notificación al recibir solicitud
- Vistas: feed/index, feed/_post, friendships/index, social/profile
- Header: barra de búsqueda con autocomplete AJAX (agregar amigo / mensaje)
- Menú lateral: Comunidad, Amigos, Mensajes

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-28 17:09:04 -06:00

50 lines
1.6 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserSearchController extends Controller
{
public function __invoke(Request $request)
{
$q = trim($request->get('q', ''));
$me = auth()->user();
if (strlen($q) < 2) {
return response()->json([]);
}
$users = User::where('id', '!=', $me->id)
->where(function ($query) use ($q) {
$query->where('name', 'like', "%{$q}%")
->orWhere('apellidoPaterno', 'like', "%{$q}%")
->orWhere('apellidoMaterno', 'like', "%{$q}%");
})
->limit(8)
->get();
return response()->json(
$users->map(function (User $user) use ($me) {
$friendship = $me->friendshipWith($user);
$status = $friendship?->status ?? 'none';
$isReceiver = $friendship?->receiver_id === $me->id;
return [
'id' => $user->id,
'name' => $user->name . ' ' . $user->apellidoPaterno,
'avatar' => $user->profile_photo_url,
'profile_url' => route('social.profile', $user),
'chat_url' => route('chat.show', $user),
'friendship' => [
'status' => $status,
'id' => $friendship?->id,
'is_receiver' => $isReceiver,
],
];
})
);
}
}