Answers for "resources laravel"

PHP
1

create laravel resources

php artisan make:resource UserResource
Posted by: Guest on August-25-2021
7

use resource in laravel 8

php artisan make:controller PhotoController --resource --model=Photo
Posted by: Guest on April-23-2020
1

laravel create resource

php artisan make:resource ResourceName
Posted by: Guest on October-04-2021
0

resources laravel

public function toArray($request)
    {
      return [
        'id' => $this->id,
        'title' => $this->title,
        'description' => $this->description,
        'created_at' => (string) $this->created_at,
        'updated_at' => (string) $this->updated_at,
        'user' => $this->user,
        'ratings' => $this->ratings,
      ];
    }

	public function index()
    {
      return BookResource::collection(Book::with('ratings')->paginate(25));
    }

	public function getAllStudents() {
      $students = Student::get()->toJson(JSON_PRETTY_PRINT);
      return response($students, 200);
    }

	public function index()
    {
      $comments = Comment::all();
      return response()->json($comments);
    }

    public function store(Request $request)
    {
      $book = Book::create([
        'user_id' => $request->user()->id,
        'title' => $request->title,
        'description' => $request->description,
      ]);

      return new BookResource($book);
    }

	public function createStudent(Request $request) {
      $student = new Student;
      $student->name = $request->name;
      $student->course = $request->course;
      $student->save();

      return response()->json([
          "message" => "student record created"
      ], 201);
    }

	public function store(Request $request)
    {
      $request->validate([
        'name' => 'required|max:255',
        'text' => 'required'
      ]);

      $newComment = new Comment([
        'name' => $request->get('name'),
        'text' => $request->get('text')
      ]);

      $newComment->save();

      return response()->json($newComment);
    }
	

	//show
    public function show(Book $book)
    {
      return new BookResource($book);
    }

	public function getStudent($id) {
      if (Student::where('id', $id)->exists()) {
          $student = Student::where('id', $id)->get()->toJson(JSON_PRETTY_PRINT);
          return response($student, 200);
        } else {
          return response()->json([
            "message" => "Student not found"
          ], 404);
        }
    }

  public function show($id)
  {
    $comment = Comment::findOrFail($id);
    return response()->json($comment);
  }



    public function update(Request $request, Book $book)
    {
      // check if currently authenticated user is the owner of the book
      if ($request->user()->id !== $book->user_id) {
        return response()->json(['error' => 'You can only edit your own books.'], 403);
      }

      $book->update($request->only(['title', 'description']));

      return new BookResource($book);
    }

	public function updateStudent(Request $request, $id) {
        if (Student::where('id', $id)->exists()) {
            $student = Student::find($id);
            $student->name = is_null($request->name) ? $student->name : $request->name;
            $student->course = is_null($request->course) ? $student->course : $request->course;
            $student->save();

            return response()->json([
                "message" => "records updated successfully"
            ], 200);
            } else {
            return response()->json([
                "message" => "Student not found"
            ], 404);

        }
    }

	public function update(Request $request, $id)
    {
      $comment = Comment::findOrFail($id);

      $request->validate([
        'name' => 'required|max:255',
        'text' => 'required'
      ]);

      $comment->name = $request->get('name');
      $comment->text = $request->get('text');
      $comment->save();

      return response()->json($comment);
    }


    public function destroy(Book $book)
    {
      $book->delete();

      return response()->json(null, 204);
    }

	public function deleteStudent ($id) {
      if(Student::where('id', $id)->exists()) {
        $student = Student::find($id);
        $student->delete();

        return response()->json([
          "message" => "records deleted"
        ], 202);
      } else {
        return response()->json([
          "message" => "Student not found"
        ], 404);
      }
    }

	  public function destroy($id)
      {
        $comment = Comment::findOrFail($id);
        $comment->delete();

        return response()->json($comment::all());
      }



    public function toArray($request)
    {
      return [
        'user_id' => $this->user_id,
        'book_id' => $this->book_id,
        'rating' => $this->rating,
        'created_at' => (string) $this->created_at,
        'updated_at' => (string) $this->updated_at,
        'book' => $this->book,
      ];
    }
Posted by: Guest on October-14-2021
0

none

return [
            'data' => $this->collection,
            'meta' => ['song_count' => $this->collection->count()],
        ];

{
  "data": [  ],
  "meta": {
    "song_count": 3
  }
}

    return [
        'id' => $this->id,
        'title' => $this->title,
        'rating' => $this->rating,
    ];
    return [
            'data' => SongResource::collection($this->collection),
            'meta' => ['song_count' => $this->collection->count()]
        ];

{
  "data": {
    "id": 1,
    "title": "Mouse.",
    "rating": 3
  },
  "meta": {
    "anything": "Some Value"
  }
}

return [
            'type' => 'articles',
            'id' => (string) $this->id,
            'attributes' => [
                'id' => (string) $this->id,
                'title' => $this->title,
                'slug' => $this->slug,
                'body' => $this->body, //\Str::words($this->body, 87, ''),
                'votes' => $this->voters()->count(),
                'upvotes' => $this->up ?? $this->upVoters()->count(),
                'downvotes' => $this->down ?? $this->downVoters()->count(),
                'views' => $this->views_count,
                'created_at' => $this->created_at,
                'is_up_voted' => auth()->guard('api')->id() ? auth()->guard('api')->user()->hasUpVoted($this->setAppends(
Posted by: Guest on January-01-1970

Browse Popular Code Answers by Language