Skip to content

Commit

Permalink
Модели
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrignatushin committed Oct 26, 2024
1 parent 9add34f commit f8554be
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 37 deletions.
20 changes: 16 additions & 4 deletions app/Models/Achievement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Achievement extends Model
{
use HasFactory;

protected $fillable = ['name', 'description', 'image_id'];

public function challenges()
public function challenges(): BelongsToMany
{
return $this->hasMany(Challenge::class);
return $this->belongsToMany(Challenge::class);
}

public function image()
public function users(): belongsToMany
{
return $this->belongsTo(Image::class);
return $this->belongsToMany(User::class);
}

public function images(): hasOne
{
return $this->hasOne(Image::class);
}

public function teams(): belongsToMany
{
return $this->belongsToMany(Team::class);
}
}
18 changes: 0 additions & 18 deletions app/Models/Category.php

This file was deleted.

22 changes: 13 additions & 9 deletions app/Models/Challenge.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

class Challenge extends Model
{
Expand All @@ -12,30 +14,32 @@ class Challenge extends Model
protected $fillable = [
'name',
'description',
'category_id',
'start_date',
'end_date',
'result',
'created_at',
'achievement_id',
'image_id'
'image_id',
'type'
];

public function users()
public function users(): belongsToMany
{
return $this->belongsToMany(User::class, 'users_challenges');
}

public function image()
public function images(): hasOne
{
return $this->belongsTo(Image::class);
return $this->hasOne(Image::class);
}

public function category()
public function achievements(): belongsToMany
{
return $this->belongsTo(Category::class);
return $this->belongsToMany(Achievement::class);
}

public function achievement()
public function teams(): belongsToMany
{
return $this->belongsTo(Achievement::class);
return $this->belongsToMany(Team::class, 'teams_challenges');
}
}
7 changes: 4 additions & 3 deletions app/Models/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Image extends Model
{
use HasFactory;

protected $fillable = ['image_path'];

public function users()
public function users(): hasMany
{
return $this->hasMany(User::class);
}

public function challenges()
public function challenges(): hasMany
{
return $this->hasMany(Challenge::class);
}

public function achievements()
public function achievements(): hasMany
{
return $this->hasMany(Achievement::class);
}
Expand Down
33 changes: 33 additions & 0 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class Team extends Model
{
use HasFactory;

protected $fillable = [
'name',
'description',
'code'
];

public function users(): belongsToMany
{
return $this->belongsToMany(User::class, 'users_teams');
}

public function challenges(): belongsToMany
{
return $this->belongsToMany(Challenge::class, 'teams_challenges');
}

public function achievements(): belongsToMany
{
return $this->belongsToMany(Achievement::class, 'teams_achievements');
}
}
20 changes: 17 additions & 3 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

Expand All @@ -17,22 +18,35 @@ class User extends Authenticatable
*
* @var array<int, string>
*/

protected $fillable = [
'name',
'surname',
'patronymic',
'email',
'password',
'about',
'image_id',
'is_admin'
'is_admin',
'is_confirmed'
];

public function challenges()
public function challenges(): BelongsToMany
{
return $this->belongsToMany(Challenge::class, 'users_challenges');
}

public function image()
public function achievements(): BelongsToMany
{
return $this->belongsToMany(Achievement::class, 'users_achievements');
}

public function teams()
{
return $this->belongsToMany(Team::class, 'users_teams');
}

public function images()
{
return $this->belongsTo(Image::class);
}
Expand Down

0 comments on commit f8554be

Please sign in to comment.