Answer the question
In order to leave comments, you need to log in
What are the create, edit methods for?
Good day, dear programmers, please tell the encoder. What are the create, edit methods for? If they are not used in route:list there is only
question.index
question.store
question.show
question.update
question.destroy
And I can't figure out why it gives an error "message": "No query results for model [App\\Models\\Question ] 20" in the postmen when requesting GET app.local/api/question/20 where there is no such id and not a 404 page or should it be?
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use App\Models\Question;
use App\Http\Resources\QuestionResource;
class QuestionController extends Controller
{
public function index()
{
return QuestionResource::collection(Question::latest()->get());
}
public function create()
{
//
}
public function store(Request $request)
{
Question::create($request->all());
return response('create', Response::HTTP_CREATED);
}
public function show(Question $question)
{
return new QuestionResource($question);
}
public function edit(Question $question)
{
//
}
public function update(Request $request, Question $question)
{
$question->update([
'title' => $request->title,
'slug' => $request->slug,
'body' => $request->body
]);
return response('Update', Response::HTTP_ACCEPTED);
}
public function destroy(Question $question)
{
$question->replies()->delete();
$question->delete();
return response('Delete', Response::HTTP_NO_CONTENT);
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class QuestionResource extends JsonResource
{
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'slug' => $this->slug,
'body' => $this->body,
'created_at' => $this->created_at->diffForHumans(),
'user' => $this->user->name,
];
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question