Answers for "resource in resource laravel"

7

laravel create resource controller

php artisan make:controller PhotoController --resource --model=Photo
Posted by: Guest on April-23-2020
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

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language