项目作者: DanielArturoAlejoAlvarez

项目描述 :
Software of Development with Laravel and MySQL.
高级语言: PHP
项目地址: git://github.com/DanielArturoAlejoAlvarez/Eloquent-Relationships-Advanced-Laravel-7-and-MySQL.git


Eloquent-Relationships-Advanced-Laravel-7-and-MySQL

Description

This repository is a Software of Development with Laravel,MySQL and Bootstrap,etc

Installation

Using Laravel 7 preferably.

DataBase

Using MySQL preferably.
Create a MySQL database and configure the .env file.

Apps

Using Postman, Insomnia,etc

Usage

  1. $ git clone https://github.com/DanielArturoAlejoAlvarez/Eloquent-Relations-Laravel-7-and-MySQL[NAME APP]
  2. $ composer install
  3. $ copy .env.example .env
  4. $ php artisan key:generate
  5. $ php artisan migrate:refresh --seed
  6. $ php artisan serve
  7. $ npm install (Frontend)
  8. $ npm run dev

Follow the following steps and you’re good to go! Important:

alt text

Coding

Models

  1. ...
  2. class Post extends Model
  3. {
  4. public function user() {
  5. return $this->belongsTo(User::class);
  6. }
  7. public function category() {
  8. return $this->belongsTo(Category::class);
  9. }
  10. public function comments() {
  11. return $this->morphMany(Comment::class, 'commentable');
  12. }
  13. public function image() {
  14. return $this->morphOne(Image::class, 'imageable');
  15. }
  16. public function tags() {
  17. return $this->morphToMany(Tag::class, 'taggable');
  18. }
  19. }
  20. class User extends Authenticatable
  21. {
  22. use Notifiable;
  23. /**
  24. * The attributes that are mass assignable.
  25. *
  26. * @var array
  27. */
  28. protected $fillable = [
  29. 'name', 'email', 'password',
  30. ];
  31. /**
  32. * The attributes that should be hidden for arrays.
  33. *
  34. * @var array
  35. */
  36. protected $hidden = [
  37. 'password', 'remember_token',
  38. ];
  39. /**
  40. * The attributes that should be cast to native types.
  41. *
  42. * @var array
  43. */
  44. protected $casts = [
  45. 'email_verified_at' => 'datetime',
  46. ];
  47. public function profile() {
  48. return $this->hasOne(Profile::class);
  49. }
  50. public function level() {
  51. return $this->belongsTo(Level::class);
  52. }
  53. public function groups() {
  54. return $this->belongsToMany(Group::class)->withTimestamps();
  55. }
  56. public function location() {
  57. return $this->hasOneThrough(Location::class, Profile::class);
  58. }
  59. public function posts() {
  60. return $this->hasMany(Post::class);
  61. }
  62. public function videos() {
  63. return $this->hasMany(Video::class);
  64. }
  65. public function comments() {
  66. return $this->hasMany(Comment::class);
  67. }
  68. public function image() {
  69. return $this->morphOne(Image::class, 'imageable');
  70. }
  71. }
  72. ...

Routes

  1. ...
  2. Route::get('/profile/{id}', function($id) {
  3. $user = App\User::find($id);
  4. $posts = $user->posts()->with('category','image','tags')->withCount('comments')->get();
  5. $videos = $user->videos()->with('category','image','tags')->withCount('comments')->get();
  6. //dd($user->name);
  7. return view('profile', [
  8. 'user' => $user,
  9. 'posts' => $posts,
  10. 'videos'=> $videos
  11. ]);
  12. })->name('profile');
  13. ...

Factory

  1. ...
  2. $factory->define(User::class, function (Faker $faker) {
  3. return [
  4. 'level_id' => $faker->randomElement([null,1,2,3]),
  5. 'name' => $faker->name,
  6. 'email' => $faker->unique()->safeEmail,
  7. 'email_verified_at' => now(),
  8. 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
  9. 'remember_token' => Str::random(10),
  10. ];
  11. });
  12. $factory->define(Post::class, function (Faker $faker) {
  13. return [
  14. 'name' => $faker->sentence,
  15. 'category_id' => rand(1,4),
  16. 'user_id' => rand(1,5)
  17. ];
  18. });
  19. ...

Seeders

  1. ...
  2. factory(Group::class, 3)->create();
  3. factory(Level::class)->create(['name'=>'Gold']);
  4. factory(Level::class)->create(['name'=>'Silver']);
  5. factory(Level::class)->create(['name'=>'Bronze']);
  6. factory(User::class, 5)->create()->each(function($user) {
  7. $profile = $user->profile()->save(factory(Profile::class)->make());
  8. $profile->location()->save(factory(Location::class)->make());
  9. $user->groups()->attach($this->array(rand(1,3)));
  10. $user->image()->save(factory(Image::class)->make(['url'=>$this->getAvatar(['men','women'],rand(1,99))]));
  11. });
  12. factory(Category::class, 4)->create();
  13. factory(Tag::class, 12)->create();
  14. factory(Post::class, 40)->create()->each(function($post) {
  15. $post->image()->save(factory(Image::class)->make(['url' => $this->getPic(rand(1,249))]));
  16. $post->tags()->attach($this->array(rand(1,12)));
  17. $number_comments = rand(1,6);
  18. for ($i=0; $i < $number_comments; $i++) {
  19. $post->comments()->save(factory(Comment::class)->make());
  20. }
  21. });
  22. ...

Views

  1. ...
  2. <div class="row">
  3. @foreach($videos as $video)
  4. <div class="col-6">
  5. <div class="card mb-3">
  6. <div class="row no-gutters">
  7. <div class="col-md-4">
  8. <img src="{{ $video->image->url }}" class="card-img">
  9. </div>
  10. <div class="col-md-8">
  11. <div class="card-body">
  12. <h5 class="card-title">{{ $video->name }}</h5>
  13. <h6 class="card-subtitle text-muted">
  14. {{ $video->category->name }} |
  15. {{ $video->comments_count }}
  16. {{ Str::plural('Comment', $video->comments_count) }}
  17. </h6>
  18. <hr>
  19. <p class="text-small">
  20. @foreach($video->tags as $tag)
  21. <span class="badge badge-light">
  22. {{ $tag->name }}
  23. </span>
  24. @endforeach
  25. </p>
  26. </div>
  27. </div>
  28. </div>
  29. </div>
  30. </div>
  31. @endforeach
  32. </div>
  33. ...

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/DanielArturoAlejoAlvarez/Eloquent-Relationships-Advanced-Laravel-7-and-MySQL. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.