laravel create resource controller
php artisan make:controller PhotoController --resource --model=Photo
laravel create resource controller
php artisan make:controller PhotoController --resource --model=Photo
make resource in laravel
php artisan make:resource UserResource
resource controller in laravel
php artisan make:controller RequestRevisionController --resource
how to create resource controller in laravel
Resource Controller:This controller will create all CRUD methods
php artisan make:controller nameOfController --resource
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,
];
}
how to create resource in laravel
php artisan make:model -a -r modelName
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