- implement factories into models

- Model class refactored in relations
This commit is contained in:
Peter Papp
2021-02-26 17:57:21 +01:00
parent 1359b78d21
commit b7e1be7518
16 changed files with 55 additions and 106 deletions

View File

@@ -1,8 +1,9 @@
<?php
namespace App;
namespace App\Models;
use ByteUnits\Metric;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
@@ -13,7 +14,7 @@ use Kyslik\ColumnSortable\Sortable;
class File extends Model
{
use Searchable, SoftDeletes , Sortable;
use Searchable, SoftDeletes, Sortable, HasFactory;
public $public_access = null;
@@ -167,33 +168,27 @@ class File extends Model
}
/**
* Get parent
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent()
{
return $this->belongsTo('App\Folder', 'folder_id', 'unique_id');
return $this->belongsTo(Folder::class, 'folder_id', 'unique_id');
}
/**
* Get folder
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function folder()
{
return $this->hasOne('App\Folder', 'unique_id', 'folder_id');
return $this->hasOne(Folder::class, 'unique_id', 'folder_id');
}
/**
* Get sharing attributes
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function shared()
{
return $this->hasOne('App\Share', 'item_id', 'unique_id');
return $this->hasOne(Share::class, 'item_id', 'unique_id');
}
/**

View File

@@ -1,21 +1,18 @@
<?php
namespace App;
namespace App\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Laravel\Scout\Searchable;
use RecursiveArrayIterator;
use RecursiveIteratorIterator;
use TeamTNT\TNTSearch\Indexer\TNTIndexer;
use \Illuminate\Database\Eloquent\SoftDeletes;
use Kyslik\ColumnSortable\Sortable;
class Folder extends Model
{
use Searchable, SoftDeletes, Sortable;
use Searchable, SoftDeletes, Sortable, HasFactory;
protected $guarded = [
'id'
@@ -115,7 +112,7 @@ class Folder extends Model
*/
public function parent()
{
return $this->belongsTo('App\Folder', 'parent_id', 'unique_id');
return $this->belongsTo(Folder::class, 'parent_id', 'unique_id');
}
public function folderIds()
@@ -130,7 +127,7 @@ class Folder extends Model
*/
public function files()
{
return $this->hasMany('App\File', 'folder_id', 'unique_id');
return $this->hasMany(File::class, 'folder_id', 'unique_id');
}
/**
@@ -141,7 +138,7 @@ class Folder extends Model
public function trashed_files()
{
return $this->hasMany('App\File', 'folder_id', 'unique_id')->withTrashed();
return $this->hasMany(File::class, 'folder_id', 'unique_id')->withTrashed();
}
/**
@@ -171,7 +168,7 @@ class Folder extends Model
*/
public function children()
{
return $this->hasMany('App\Folder', 'parent_id', 'unique_id');
return $this->hasMany(Folder::class, 'parent_id', 'unique_id');
}
/**
@@ -181,7 +178,7 @@ class Folder extends Model
*/
public function trashed_children()
{
return $this->hasMany('App\Folder', 'parent_id', 'unique_id')->withTrashed();
return $this->hasMany(Folder::class, 'parent_id', 'unique_id')->withTrashed();
}
/**
@@ -191,7 +188,7 @@ class Folder extends Model
*/
public function shared()
{
return $this->hasOne('App\Share', 'item_id', 'unique_id');
return $this->hasOne(Share::class, 'item_id', 'unique_id');
}
// Delete all folder children

View File

@@ -1,47 +1,9 @@
<?php
namespace App;
namespace App\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
/**
* App\Invoice
*
* @property int $id
* @property string $token
* @property string $order
* @property string|null $provider
* @property string $user_id
* @property string $plan_id
* @property array $seller
* @property array $client
* @property array $bag
* @property string|null $notes
* @property string $total
* @property string $currency
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\User|null $user
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereBag($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereClient($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereCurrency($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereNotes($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereOrder($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice wherePlanId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereProvider($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereSeller($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereToken($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereTotal($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Invoice whereUserId($value)
* @mixin \Eloquent
*/
class Invoice extends Model
{
protected $guarded = [
@@ -55,8 +17,6 @@ class Invoice extends Model
];
/**
* Get user instance
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function user() {

View File

@@ -1,13 +1,14 @@
<?php
namespace App;
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Kyslik\ColumnSortable\Sortable;
class Page extends Model
{
use Sortable;
use Sortable, HasFactory;
/**
* Sortable columns

View File

@@ -1,25 +1,14 @@
<?php
namespace App;
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
/**
* App\Setting
*
* @property int $id
* @property string $name
* @property string|null $value
* @method static \Illuminate\Database\Eloquent\Builder|Setting newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Setting newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Setting query()
* @method static \Illuminate\Database\Eloquent\Builder|Setting whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Setting whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|Setting whereValue($value)
* @mixin \Eloquent
*/
class Setting extends Model
{
use HasFactory;
public $timestamps = false;
protected $guarded = ['id'];

View File

@@ -1,15 +1,15 @@
<?php
namespace App;
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Notifications\SharedSendViaEmail;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Str;
class Share extends Model
{
use Notifiable;
use Notifiable, HasFactory;
protected $guarded = ['id'];

View File

@@ -1,12 +1,15 @@
<?php
namespace App;
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Traffic extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'upload',

View File

@@ -3,8 +3,6 @@
namespace App\Models;
use App\Notifications\ResetPassword;
use App\Notifications\ResetUserPasswordNotification;
use App\UserSettings;
use ByteUnits\Metric;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -139,9 +137,7 @@ class User extends Authenticatable
*/
public function getFolderTreeAttribute()
{
// Get sorting setup
return FileManagerFolder::with(['folders.shared', 'shared:token,id,item_id,permission,protected,expire_in'])
return File::with(['folders.shared', 'shared:token,id,item_id,permission,protected,expire_in'])
->where('parent_id', 0)
->where('user_id', $this->id)
->sortable()
@@ -247,7 +243,7 @@ class User extends Authenticatable
*/
public function favourite_folders()
{
return $this->belongsToMany(FileManagerFolder::class, 'favourite_folder', 'user_id', 'folder_unique_id', 'id', 'unique_id')->with('shared:token,id,item_id,permission,protected,expire_in');
return $this->belongsToMany(Folder::class, 'favourite_folder', 'user_id', 'folder_unique_id', 'id', 'unique_id')->with('shared:token,id,item_id,permission,protected,expire_in');
}
/**
@@ -257,7 +253,7 @@ class User extends Authenticatable
*/
public function latest_uploads()
{
return $this->hasMany(FileManagerFile::class)->with(['parent'])->take(40);
return $this->hasMany(File::class)->with(['parent'])->take(40);
}
/**
@@ -267,7 +263,7 @@ class User extends Authenticatable
*/
public function files()
{
return $this->hasMany(FileManagerFile::class);
return $this->hasMany(File::class);
}
/**
@@ -277,7 +273,7 @@ class User extends Authenticatable
*/
public function files_with_trashed()
{
return $this->hasMany(FileManagerFile::class)->withTrashed();
return $this->hasMany(File::class)->withTrashed();
}
/**

View File

@@ -1,6 +1,6 @@
<?php
namespace App;
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
@@ -8,5 +8,8 @@ class UserSettings extends Model
{
public $timestamps = false;
protected $guarded = ['id', 'storage_capacity'];
protected $guarded = [
'id',
'storage_capacity'
];
}

View File

@@ -1,12 +1,15 @@
<?php
namespace App;
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class Zip extends Model
{
use HasFactory;
protected $guarded = ['id'];
public $incrementing = false;

View File

@@ -2,9 +2,9 @@
namespace Database\Factories;
use App\File;
use App\Models\User;
use App\Models\File;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class FileFactory extends Factory
{
@@ -25,7 +25,10 @@ class FileFactory extends Factory
return [
'id' => $this->faker->uuid,
'user_id' => $this->faker->uuid,
'name' => $this->faker->name,
'name' => $this->faker->word,
'basename' => Str::slug($this->faker->name),
'mimetype' => $this->faker->mimeType,
'filesize' => $this->faker->numberBetween(10000, 99999),
'type' => $this->faker->randomElement(
['image', 'file', 'video', 'audio']
),

View File

@@ -2,7 +2,7 @@
namespace Database\Factories;
use App\Folder;
use App\Models\Folder;
use Illuminate\Database\Eloquent\Factories\Factory;
class FolderFactory extends Factory

View File

@@ -3,7 +3,6 @@
namespace Database\Factories;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

View File

@@ -16,7 +16,7 @@ class CreateFileManagerFolders extends Migration
Schema::create('folders', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('user_id');
$table->uuid('parent_id');
$table->uuid('parent_id')->nullable();
$table->text('name');
$table->string('color')->nullable();
$table->string('emoji')->nullable();

View File

@@ -16,7 +16,7 @@ class CreateFileManagerFiles extends Migration
Schema::create('files', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->uuid('user_id');
$table->uuid('folder_id');
$table->uuid('folder_id')->nullable();
$table->text('thumbnail')->nullable();
$table->text('name');

View File

@@ -2,10 +2,10 @@
namespace Tests\Feature;
use App\Models\File;
use App\Models\Folder;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;