I have a page which views posts as well as add posts.
In my controller, I have two methods which return to the same page:
public function index () {
return view('posts.index', [
'posts' => Post::all()
]);
}
and
public function store (Request $request) {
$validator = validate($request);
if ($validator->fails()) {
return view('posts.index', [
'message' => $validator->messages(),
'status' => '400'
]);
}
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->user_id = 1;
$post->save();
return view('posts.index', [
'message' => 'Successfully published post!',
'status' => '200'
]);
}
Now when storing new post, the index view loses the posts data from the index method. Does it mean that in my every update, I should include posts variable?
public function store (Request $request) {
$validator = validate($request);
if ($validator->fails()) {
return view('posts.index', [
'message' => $validator->messages(),
'status' => '400',
'posts' => Post::all()
]);
}
$post = new Post;
$post->title = $request->title;
$post->body = $request->body;
$post->user_id = 1;
$post->save();
return view('posts.index', [
'message' => 'Successfully published post!',
'status' => '200',
'posts' => Post::all()
]);
}
I am new to Laravel by the way.
Usually, the store() (and update() and delete()) methods of a Controller don't need a view. Instead of showing a view, redirect to a different route when storing/updating/deleting was successful.
Replace
return view('posts.index', [
'message' => 'Successfully published post!',
'status' => '200'
]);
with
return redirect()
->route(NAMEOFYOURINDEXROUTE)
->with('message', 'Successfully published post!');
where NAMEOFYOURINDEXROUTE is the name of the route you want to redirect to. (This could be a dashboard or the list/index of products etc. - you decide)
More on redirecting: https://laravel.com/docs/master/redirects
and redirecting with flash messages: https://laravel.com/docs/master/redirects#redirecting-with-flashed-session-data
Edit:
As #user3532758 pointed out, here's a link worth mentioning: https://en.wikipedia.org/wiki/Post/Redirect/Get (Basically, redirecting to a different route prevents accidental re-submission of the data when refreshing the page in the browser)
Related
I am trying to store logged user's id but I am getting this error
ErrorException
array_map(): Argument #2 should be an array
This is the code in the controller
public function store(Request $request)
{
if (!auth()->check()) {
abort(403, 'Only authenticated users can create new posts.');
}
$data = request()->validate([
'id' => $id = Auth::id(),
'content' => 'required',
'topic' => 'required',
'hashtag' => 'required'
]);
$check = Tweets::create($data);
return Redirect::to("form")->withSuccess('Great! Form successfully submit with validation.');
}
The error is in this line of code.
'id' => $id = Auth::id(),
I know that should be a string but to explain to you what I am trying to do, and I still have not found any solution.
Do it Like this.
public function store(Request $request)
{
if (!auth()->check()) {
abort(403, 'Only authenticated users can create new posts.');
}
$request->validate([
'content' => 'required',
'topic' => 'required',
'hashtag' => 'required'
]);
$data = $request->all();
$data['id'] = Auth::id();
$check = Tweets::create($data);
return Redirect::to("form")->withSuccess('Great! Form successfully submit with validation.');
}
Delete this
'id' => $id = Auth::id(),
and add
$data['id'] = Auth::id();
before
$check = Tweets::create($data);
That should work
I am implementing API in Laravel and get comment that my POST and PUT methods are not accurate according to the REST standards.
I am using POST for create new resource and PUT for updating existing one. Can not see problem.
endpoints:
Route::post('/cities', [
'uses' => 'CityController#store'
]);
Route::put('/cities/{id}', [
'uses' => 'CityController#update'
]);
PUT and POST method :
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required|min:3',
'latitude' => 'required|numeric',
'longitude' => 'required|numeric'
]);
// update model and only pass in the fillable fields
$this->cityRepository->update(
$request->only($this->cityRepository->getModel()->fillable), $id
);
return $this->cityRepository->show($id);
}
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|min:3',
'latitude' => 'required|numeric',
'longitude' => 'required|numeric'
]);
$data = $this->cityRepository->create(
$request->only($this->cityRepository->getModel()->fillable));
if ($data) {
$message = self::SUCCESSFULLY_CREATED;
$code = self::HTTP_CODE_CREATED;
} else {
$message = self::UNSUCCESSFULLY_CREATED;
$code = 409;
}
return $this->sendResponse($message, $data, $code);
}
Send response:
public function sendResponse($message, $result = [], $code = 200)
{
$response = [
'message' => $message,
];
if (!empty($result)) {
$response['data'] = $result;
}
return response()->json($response, $code);
}
Show method:
public function show($id)
{
return $this->model->findOrFail($id);
}
You could return the created object from your store method instead of SUCCESSFULLY_CREATED. Aside from that the code looks good.
Take a look at the table on https://laravel.com/docs/5.8/controllers#resource-controllers, which has a fairly useful REST definition of various CRUD routes:
GET /photos index photos.index
GET /photos/create create photos.create
POST /photos store photos.store
GET /photos/{photo} show photos.show
GET /photos/{photo}/edit edit photos.edit
PUT/PATCH /photos/{photo} update photos.update
DELETE /photos/{photo} destroy photos.destroy
Here's a good resource for which HTTP methods you should return:
https://www.restapitutorial.com/lessons/httpmethods.html
I have a form where users can edit a branch's info, once the user submits that form, the update() method checks for the validity of the submitted data such as the description must be unique to every subscriber. While the validation WORKS, it doesn't redirect to the exact url/page that I want if the validation fails. It stays in the same edit form.
here's the code of my update() method:
public function update(Request $request, $id)
{
$description = $request->input('description');
$message = $request->input('message');
$subscriber_id = auth()->user()->subscriber_id;
$messages = [
'description.unique' => 'Branch already exists!',
];
$this->validate($request, [
'description' => Rule::unique('branches')->where(function ($query) use($subscriber_id) {
return $query->where('subscriber_id', $subscriber_id);
})
], $messages);
Branch::where('id', $id)->update([
'description' => $description,
'message' => $message,
]);
return redirect('branches')->with('success', 'Branch info successfully updated!');
}
Note: the url of the edit form is /branch/edit/{id} while the page I want to redirect after submission is /branches.
Is my validation wrong? Did I miss something?
Thanks! :)
According to the laravel docs you can redirect to a different route by using the Validator facade
public function update(Request $request, $id)
{
$description = $request->input('description');
$message = $request->input('message');
$subscriber_id = auth()->user()->subscriber_id;
$messages = [
'description.unique' => 'Branch already exists!',
];
$validator = Validator::make($request->all(), [
'description' => Rule::unique('branches')->where(function ($query) use($subscriber_id) {
return $query->where('subscriber_id', $subscriber_id);
})
],
$messages);
if ($validator->fails()) {
return redirect('/branches')
->withErrors($validator)
->withInput();
}
Branch::where('id', $id)->update([
'description' => $description,
'message' => $message,
]);
return redirect('branches')->with('success', 'Branch info successfully updated!');
}
Make sure you use the Validator facade at the beginning of your controller file use Validator;
I can not pass a foreign key value (which is user_id) to my newly created article.
Here is my code...
<?php
if (is_null($request->user_id)) {
$request->user_id = $user->user_id;
}
$request->validate(['title' => 'Required|string|min:3', 'body' => 'Required|string|min:5', 'user_id' => 'Required|exists:users,user_id']);
if ($article = Article::create($request->all())) {
event(new ArticleCreated($user));
return response()->json(['success' => true, 'reason' => 'Article Created successfully', $article]);
} else {
return 'Article could not be created';
}
Change this:
if($article = Article::create($request->all())) {
$article->user_id = $request->user_id;
$article->save();
event(new ArticleCreated($user));
return response()->json(['success' => true, 'reason' => 'Article Created successfully', $article]);
}
Try this,
public function store(Request $request)
{
$request->validate([
'title' => 'Required|string|min:3',
'body' => 'Required|string|min:5',
]);
$data = $request->all();
//you don't need to validate user_id is correct
//if you are using auth middleware in the route
$user = auth()->user()
$data['user_id] = $user->id
if ($article = Article::create($data)) {
event(new ArticleCreated($user));
return response()->json([
'success' => true,
'reason' => 'Article Created successfully',
$article
]);
} else {
return 'Article could not be created';
}
}
Hope this helps
Check your fillable array in Article model, there must be user_id, and check if the user id is passed in the $request->all().
I have a Laravel Lumen API. I'm seeing an issue with the update functionality.
In my controller, the code for updating an item is:
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'description' => 'required',
'completed' => 'required',
]);
$todo = Todo::find($id);
$todo->name = $request->name;
$todo->description = $request->description;
$todo->completed = $request->completed;
$todo->save();
return response()->json(['status' => 'success']);
}
I can update the todo item using:
http://lumen-todo.app/api/51?name=test&description=test&completed=1
however was hoping I could send the parameters in a json body, like this
PUT http://lumen-todo.app/api
{
"id": 1
"name": "Test",
"description": "Test",
"completed": 1,
}
For adding items, it works via a json body, so don't understand why it does not work for updates. For info, the 'add item' controller code is here:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required',
'description' => 'required',
'completed' => 'required'
]);
$todo = new Todo();
$todo->name = $request->name;
$todo->description = $request->description;
$todo->completed = $request->completed;
$todo->save();
return response()->json(['status' => 'success']);
}
If you want to get the json data from request payload, validate and store it, use
public function store(Request $request)
{
$data = $request->json()->all();
$this->validate($data, [
'name' => 'required',
'description' => 'required',
'completed' => 'required'
]);
$resource = $this->model->find($id);
$resource->fill($request);
$resource->save();
return response()->json(['status' => 'success']);
}
Instead of doing this:
$todo = new Todo();
$todo->name = $request->name;
$todo->description = $request->description;
$todo->completed = $request->completed;
$todo->save();
Do, this:
use App\Todo;
protected $model;
public function __construct(Todo $model) {
$this->model = $model;
}
$resource = $this->model->find($id);
$resource->fill($request);
$resource->save();
Also, you can do json_decode() function to change your json params to array and use that to validate and save data.