-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Friendlist controller view and migration
- Loading branch information
Showing
5 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Controllers\Controller; | ||
use Illuminate\Http\Request; | ||
use \App\Models\User; | ||
use \App\Models\Friendship; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class FriendshipsController extends Controller | ||
{ | ||
//Show every user friends | ||
public function index() | ||
{ | ||
$friendsFromUser = Friendship::where('user_id', Auth::id()) | ||
->where('status', 'accepted') | ||
->with(['user', 'friend']) | ||
->get(); | ||
|
||
$friendsFromFriends = Friendship::where('friend_id', Auth::id()) | ||
->where('status', 'accepted') | ||
->with(['user', 'friend']) | ||
->get(); | ||
|
||
$friends = $friendsFromFriends->merge($friendsFromUser); | ||
return view('friendships.index', ['friends' => $friends]); | ||
} | ||
|
||
|
||
|
||
|
||
//show one friend profile | ||
function friends() | ||
{ | ||
|
||
} | ||
|
||
function addFriend(Request $request) | ||
{ | ||
|
||
} | ||
|
||
function removeFriend(Request $request) | ||
{ | ||
|
||
} | ||
|
||
function declineFriend(Request $request) | ||
{ | ||
|
||
} | ||
|
||
function acceptFriend(Request $request) | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
class Friendship extends Model | ||
{ | ||
use HasFactory; | ||
|
||
// Spécifie le nom de la table associée à ce modèle | ||
protected $table = 'friendships'; | ||
|
||
// Les attributs qui peuvent être assignés en masse | ||
protected $fillable = [ | ||
'user_id', | ||
'friend_id', | ||
'status', | ||
]; | ||
|
||
/** | ||
* Obtenir l'utilisateur qui possède cette amitié. | ||
*/ | ||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class, 'user_id'); // Relation avec le modèle User | ||
} | ||
|
||
/** | ||
* Obtenir l'ami associé à cette amitié. | ||
*/ | ||
public function friend(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class, 'friend_id'); // Relation avec le modèle User | ||
} | ||
|
||
/** | ||
* Vérifier si l'amitié est acceptée. | ||
*/ | ||
public function isAccepted(): bool | ||
{ | ||
return $this->status === 'accepted'; // Retourne true si le statut est 'accepted' | ||
} | ||
|
||
/** | ||
* Vérifier si l'amitié est en attente. | ||
*/ | ||
public function isPending(): bool | ||
{ | ||
return $this->status === 'pending'; // Retourne true si le statut est 'pending' | ||
} | ||
|
||
/** | ||
* Vérifier si l'amitié est bloquée. | ||
*/ | ||
public function isBlocked(): bool | ||
{ | ||
return $this->status === 'blocked'; // Retourne true si le statut est 'blocked' | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
database/migrations/2024_10_24_063135_create_friendships_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
Schema::create('friendships', function (Blueprint $table) { | ||
$table->foreignId('user_id')->constrained('users')->onDelete('cascade'); | ||
$table->foreignId('friend_id')->constrained('users')->onDelete('cascade'); | ||
$table->timestamps(); | ||
$table->enum('status', ['pending', 'accepted', 'blocked'])->default('pending'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('friendships'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<x-app-layout> | ||
<h1 class="text-2xl font-bold mb-4">Friends List</h1> | ||
|
||
@if($friends->isEmpty()) | ||
<p class="text-gray-600">You have no friends yet.</p> | ||
@else | ||
<div class="overflow-x-auto"> | ||
<table class="min-w-full bg-white border border-gray-200"> | ||
<thead> | ||
<tr class="bg-gray-100 border-b"> | ||
<th scope="col" class="text-center px-4 py-2 border-r text-sm font-medium text-gray-700">Name</th> | ||
<th scope="col" class="text-center px-4 py-2 text-sm font-medium text-gray-700">Action</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
@foreach($friends as $friend) | ||
<tr class="border-b"> | ||
<td class="text-center px-4 py-2 border-r"> | ||
{{-- Affichage du nom de l'ami --}} | ||
{{ $friend->user_id == Auth::id() ? $friend->friend->name : $friend->user->name }} | ||
</td> | ||
</tr> | ||
@endforeach | ||
</tbody> | ||
</table> | ||
</div> | ||
@endif | ||
</x-app-layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters