Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Update User model and factory for Laravel 8 compatibility #271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions app/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use App\Traits\BelongsToUser;
use App\Traits\HasComments;
use App\Traits\HasTags;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

class Article extends Model
{
use SoftDeletes, BelongsToUser, HasComments, HasTags;
use SoftDeletes, BelongsToUser, HasComments, HasTags, HasFactory;

/**
* The attributes that should be mutated to dates.
Expand Down Expand Up @@ -101,7 +102,7 @@ public function setTitleAttribute($value)
*/
public function setUniqueSlug($value, $extra)
{
$slug = Str::slug($value.'-'.$extra);
$slug = Str::slug($value . '-' . $extra);

if (static::whereSlug($slug)->exists()) {
$this->setUniqueSlug($slug, (int) $extra + 1);
Expand Down
4 changes: 4 additions & 0 deletions app/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace App;

use App\Article;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Category extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
Expand Down
4 changes: 2 additions & 2 deletions app/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

use App\Tools\Markdowner;
use App\Traits\BelongsToUser;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jcc\LaravelVote\Traits\Votable;

class Comment extends Model
{
use SoftDeletes, Votable, BelongsToUser;
use SoftDeletes, Votable, BelongsToUser, HasFactory;

protected $vote = User::class;

Expand Down Expand Up @@ -56,5 +57,4 @@ public function setContentAttribute($value)

$this->attributes['content'] = json_encode($data);
}

}
3 changes: 2 additions & 1 deletion app/Discussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
use App\Traits\BelongsToUser;
use App\Traits\HasComments;
use App\Traits\HasTags;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;

class Discussion extends Model
{
use SoftDeletes, BelongsToUser, HasComments, HasTags;
use SoftDeletes, BelongsToUser, HasComments, HasTags, HasFactory;

/**
* The attributes that should be mutated to dates.
Expand Down
3 changes: 2 additions & 1 deletion app/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace App;

use App\Scopes\StatusScope;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;

class Link extends Model
{
use SoftDeletes;
use SoftDeletes, HasFactory;

/**
* The attributes that should be mutated to dates.
Expand Down
3 changes: 2 additions & 1 deletion app/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace App;

use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model as BaseModel;

class Model extends BaseModel
{
use Filterable;
use Filterable, HasFactory;
}
3 changes: 2 additions & 1 deletion app/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;

class Tag extends Model
{
use SoftDeletes;
use SoftDeletes, HasFactory;

/**
* The attributes that should be mutated to dates.
Expand Down
3 changes: 2 additions & 1 deletion app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Jcc\LaravelVote\Vote;
use App\Traits\FollowTrait;
use App\Scopes\StatusScope;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand All @@ -16,7 +17,7 @@

class User extends Authenticatable
{
use HasApiTokens, Notifiable, SoftDeletes, FollowTrait, Voter, HasRoles, Filterable;
use HasApiTokens, Notifiable, SoftDeletes, FollowTrait, Voter, HasRoles, Filterable, HasFactory;

/**
* The attributes that should be mutated to dates.
Expand Down
3 changes: 3 additions & 0 deletions app/Visitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;

class Visitor extends Model
{
use HasFactory;
/**
* The attributes that are mass assignable.
*
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
"yzalis/identicon": "^2.0"
},
"require-dev": {
"spatie/laravel-ignition": "^1.0",
"fzaninotto/faker": "^1.9.1",
"mockery/mockery": "^1.3.1",
"nunomaduro/collision": "^6.1",
"phpunit/phpunit": "^9.0"
"phpunit/phpunit": "^9.0",
"spatie/laravel-ignition": "^1.0"
},
"config": {
"optimize-autoloader": true,
Expand Down
6 changes: 3 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,


/*
* Package Service Providers...
Expand Down
39 changes: 39 additions & 0 deletions database/factories/ArticleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Database\Factories;

use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Article>
*/
class ArticleFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{

$user_ids = \App\User::pluck('id')->random();
$category_ids = \App\Category::pluck('id')->random();
$title = $this->faker->sentence(mt_rand(3, 10));
return [
'user_id' => $user_ids,
'category_id' => $category_ids,
'last_user_id' => $user_ids,
'slug' => Str::slug($title),
'title' => $title,
'subtitle' => strtolower($title),
'content' => $this->faker->paragraph,
'page_image' => $this->faker->imageUrl(),
'meta_description' => $this->faker->sentence,
'is_draft' => false,
'published_at' => $this->faker->dateTimeBetween($startDate = '-2 months', $endDate = 'now')
];
}
}
20 changes: 20 additions & 0 deletions database/factories/CategoryFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Database\Factories;

use App\Category;
use Illuminate\Database\Eloquent\Factories\Factory;

class CategoryFactory extends Factory
{
protected $model = Category::class;

public function definition()
{
return [
'name' => $this->faker->name,
'parent_id' => 0,
'path' => $this->faker->url,
];
}
}
30 changes: 30 additions & 0 deletions database/factories/CommentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Database\Factories;

use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Comment>
*/
class CommentFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
$user_ids = \App\User::pluck('id')->random();
$discussion_ids = \App\Discussion::pluck('id')->random();
$type = ['discussions', 'articles'];
return [
'user_id' => $user_ids,
'commentable_type' => $type[mt_rand(0, 1)],
'commentable_id' => $discussion_ids,
'content' => $this->faker->paragraph
];
}
}
29 changes: 29 additions & 0 deletions database/factories/DiscussionFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Database\Factories;

use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Discussion>
*/
class DiscussionFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
$user_ids = \App\User::pluck('id')->random();
return [
'user_id' => $user_ids,
'last_user_id' => $user_ids,
'title' => $this->faker->sentence,
'content' => $this->faker->paragraph,
'status' => true,
];
}
}
26 changes: 26 additions & 0 deletions database/factories/LinkFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Database\Factories;

use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Link>
*/
class LinkFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'name' => $this->faker->name,
'link' => $this->faker->url,
'image' => $this->faker->imageUrl()
];
}
}
26 changes: 26 additions & 0 deletions database/factories/TagFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Database\Factories;

use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Tag>
*/
class TagFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'tag' => $this->faker->word,
'title' => $this->faker->sentence,
'meta_description' => $this->faker->sentence,
];
}
}
35 changes: 35 additions & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Database\Factories;

use App\User;
use Illuminate\Support\Str;
use Faker\Generator as Faker;
use Illuminate\Database\Eloquent\Factories\Factory;

class UserFactory extends Factory
{
protected $model = User::class;

public function definition()
{
static $password;

return [
'name' => $this->faker->name,
'email' => $this->faker->safeEmail,
'status' => true,
'confirm_code' => Str::random(64),
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => Str::random(10),
];
}

public function admin()
{
return $this->state([
'is_admin' => 1,
'password' => bcrypt('admin'),
]);
}
}
Loading