create laravel resources
php artisan make:resource UserResource
create laravel resources
php artisan make:resource UserResource
use resource in laravel 8
php artisan make:controller PhotoController --resource --model=Photo
laravel create resource
php artisan make:resource ResourceName
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,
];
}
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(
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us