项目作者: DanielArturoAlejoAlvarez

项目描述 :
Software of Application with Laravel(RESTFull API)
高级语言: PHP
项目地址: git://github.com/DanielArturoAlejoAlvarez/RESTFull-Laravel-8.5-Eloquent-API-Resources.git


RESTFull API LARAVEL + VUE

Description

This repository is a Software of Development with Laravel,MySQL,VUE,Axios,Boostrap,etc

Installation

Using Laravel 8.5 and Vue.js 2.5 preferably.

DataBase

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

Apps

Using Postman,Talend API Tester,Insomnia,etc

Usage

  1. $ git clone https://github.com/DanielArturoAlejoAlvarez/RESTFull-Laravel-8.5-Eloquent-API-Resources[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

Component

  1. ...
  2. import axios from 'axios';
  3. export default {
  4. data() {
  5. return {
  6. posts: null
  7. }
  8. },
  9. mounted() {
  10. this.getPosts()
  11. },
  12. methods: {
  13. getPosts: function() {
  14. axios.get('api/posts')
  15. .then(res=>{
  16. this.posts = res.data
  17. })
  18. }
  19. }
  20. }
  21. ...

Controllers

  1. ...
  2. class PostController extends Controller
  3. {
  4. protected $post;
  5. /**HTTP Status
  6. * 1XX Info
  7. * 2XX Respose Successfully
  8. * 3XX Redirection
  9. * 4XX Error Client
  10. * 5XX Error Server
  11. */
  12. public function __construct(Post $post)
  13. {
  14. $this->post = $post;
  15. }
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index()
  22. {
  23. return response()->json(
  24. new PostCollection(
  25. $this->post->orderBy('id', 'DESC')->get()
  26. )
  27. );
  28. }
  29. /**
  30. * Store a newly created resource in storage.
  31. *
  32. * @param \Illuminate\Http\Request $request
  33. * @return \Illuminate\Http\Response
  34. */
  35. public function store(PostRequest $request)
  36. {
  37. $post = $this->post->create($request->all());
  38. return response()->json(new PostResource($post), 201);
  39. }
  40. /**
  41. * Display the specified resource.
  42. *
  43. * @param \App\Models\Post $post
  44. * @return \Illuminate\Http\Response
  45. */
  46. public function show(Post $post)
  47. {
  48. return response()->json(new PostResource($post), 200);
  49. }
  50. /**
  51. * Update the specified resource in storage.
  52. *
  53. * @param \Illuminate\Http\Request $request
  54. * @param \App\Models\Post $post
  55. * @return \Illuminate\Http\Response
  56. */
  57. public function update(PostRequest $request, Post $post)
  58. {
  59. $post->update($request->all());
  60. return response()->json(new PostResource($post));
  61. }
  62. /**
  63. * Remove the specified resource from storage.
  64. *
  65. * @param \App\Models\Post $post
  66. * @return \Illuminate\Http\Response
  67. */
  68. public function destroy(Post $post)
  69. {
  70. $post->delete();
  71. /**
  72. * VerifyCsrfToken apply except api/*
  73. */
  74. return response()->json(null, 204);
  75. }
  76. }
  77. ...

Models

  1. ...
  2. class Post extends Model
  3. {
  4. use HasFactory;
  5. protected $fillable = ['title','body'];
  6. }
  7. ...

Resources

  1. ...
  2. class Post extends JsonResource
  3. {
  4. /**
  5. * Transform the resource into an array.
  6. *
  7. * @param \Illuminate\Http\Request $request
  8. * @return array
  9. */
  10. public function toArray($request)
  11. {
  12. //return parent::toArray($request);
  13. return [
  14. 'id' => $this->id,
  15. 'post_name' => strtoupper($this->title),
  16. 'post_body' => strtoupper(substr($this->body,0,100)) . '...',
  17. 'published_at' => $this->created_at->diffForHumans(),
  18. 'created_at' => $this->created_at->format('d-m-Y'),
  19. 'updated_at' => $this->updated_at->format('d-m-Y'),
  20. ];
  21. }
  22. }
  23. ...

Middlewares

  1. ...
  2. class VerifyCsrfToken extends Middleware
  3. {
  4. /**
  5. * The URIs that should be excluded from CSRF verification.
  6. *
  7. * @var array
  8. */
  9. protected $except = [
  10. 'api/*'
  11. ];
  12. }
  13. ...

Routes

  1. ...
  2. #API
  3. Route::apiResource('posts', App\Http\Controllers\Api\PostController::class)->names([
  4. 'index' => 'api.posts.index',
  5. //'store' => 'api.posts.store',
  6. //'show' => 'api.posts.show',
  7. //'update' => 'api.posts.update',
  8. //'destroy' => 'api.posts.destroy',
  9. ])->only('index');
  10. #WEB
  11. Route::get('/', [App\Http\Controllers\PageController::class, 'index'])->name('welcome');
  12. Route::group(['prefix' => 'api'], function () {
  13. //Route::apiResource('posts', PostController::class);
  14. });
  15. Route::middleware('auth')->resource('posts', App\Http\Controllers\Backend\PostController::class)->only('index');
  16. Route::get('/home', [App\Http\Controllers\Backend\HomeController::class, 'index'])->name('home');
  17. Auth::routes();
  18. ...

Factory

  1. ...
  2. public function definition()
  3. {
  4. return [
  5. 'title' => $this->faker->sentence,
  6. 'body' => $this->faker->text,
  7. ];
  8. }
  9. ...

Seeders

  1. ...
  2. public function run()
  3. {
  4. // \App\Models\User::factory(10)->create();
  5. \App\Models\User::create([
  6. 'name' => [YOUR NAME],
  7. 'email' => [YOUR EMAIL],
  8. 'password' => bcrypt([YOUR PASSWORD])
  9. ]);
  10. \App\Models\Post::factory(18)->create();
  11. }
  12. ...

HTTP Resources

  1. $ http://127.0.0.1:8000/posts (frontend/backend[auth])
  2. $ http://127.0.0.1:8000/api/posts (API)

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/DanielArturoAlejoAlvarez/RESTFull-Laravel-8.5-Eloquent-API-Resources. 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.