/** * Run the database seeds. * * @return void */ public function run() { if (Article::count() > 0 && Topic::count() > 0) { foreach (range(1, 10) as $index) { DB::table('article_topic')->insert(['article_id' => rand(1, Article::count()), 'topic_id' => rand(1, Topic::count())]); } } }
/** * Show the application dashboard. * * @return Response */ public function index() { $perPage = 2; $article = Article::paginate($perPage); $maxPage = intval(ceil(Article::count() / $perPage)); $firstArticle = $article->shift(); $others = $article; return view('home')->with(['first' => $firstArticle, 'others' => $others, 'maxPage' => $maxPage]); /*$categories = Category::all(); $articles = Article::paginate(15); return view('home')->withArticles($articles)->withCategories($categories);*/ }
public function testDeleteOne() { $entities = factory(Article::class, 4)->create()->all(); $this->addPermalinksToArticles($entities); foreach ($entities as $oneEntity) { $oneEntity->push(); } $entity = $entities[0]; $id = $entity->article_id; $entityPermalinksCount = $entity->permalinks->count(); $this->assertEquals($entityPermalinksCount, ArticlePermalink::where('article_id', '=', $id)->count()); $rowCount = Article::count(); $permalinksTotalCount = ArticlePermalink::all()->count(); $this->delete('/articles/' . $id); $permalinksTotalCountAfterDelete = ArticlePermalink::all()->count(); $this->assertResponseStatus(204); $this->assertResponseHasNoContent(); $this->assertEquals($rowCount - 1, Article::count()); $this->assertEquals($permalinksTotalCount - $entityPermalinksCount, $permalinksTotalCountAfterDelete); }
/* |-------------------------------------------------------------------------- | Model Factories |-------------------------------------------------------------------------- | | Here you may define all of your model factories. Model factories give | you a convenient way to create models for testing and seeding your | database. Just tell the factory how a default model should look. | */ use App\Models\Article; use App\Models\Category; use App\Models\Comment; use App\Models\Topic; use App\Models\User; $factory->define(User::class, function (Faker\Generator $faker) { return ['name' => $faker->name, 'email' => $faker->email, 'password' => bcrypt(str_random(10)), 'remember_token' => str_random(10)]; }); $factory->define(Category::class, function (Faker\Generator $faker) { return []; }); $factory->define(Article::class, function (Faker\Generator $faker) { return ['user_id' => rand(1, User::count()), 'category_id' => rand(1, Category::count()), 'title' => $faker->sentence(mt_rand(3, 10)), 'content' => join('<br>', $faker->paragraphs(mt_rand(3, 6))), 'published_at' => rand(0, 2) >= 1 ? Carbon\Carbon::now() : null]; }); $factory->define(Topic::class, function (Faker\Generator $faker) { return []; }); $factory->define(Comment::class, function (Faker\Generator $faker) { return ['user_id' => rand(1, User::count()), 'article_id' => rand(1, Article::count()), 'content' => join("\n", $faker->paragraphs(mt_rand(1, 2)))]; });