Комментарии:
Hahah! Glad that I found this video. I was maniacally using `try-catch` block and using FormRequest classes at the same time until chatGPT told me that I'm a jerk and I should stop bloating my controllers with unnecessary try-catch blocks 😄😄
So my code was :
```
try {
Project::create($request->validated());
} catch (\Throwable $th) {
return back()->withErrors($request);
} finally {
return to_route('projects.index'); // later I realised I don't even need the `finally` statement too.
}
```
then I refactored it to:
```
Project::create($request->validated());
return redirect()->route('projects.index');
```
Thanks for the informations,
PostMan tip:
click on collection, add this script to "pre-request script" tab (you don't need to set headers for each new request tab)
--------------------------------
pm.request.headers.add({
key: 'Accept',
value: 'application/json'
});
pm.request.headers.add({
key: 'Content-Type',
value: 'application/json'
});
Thz a million, u made my day sir😙
ОтветитьThank you.
ОтветитьThank you! This is just what I was searching for.
ОтветитьThank you very much, you helped me a lot.
ОтветитьThanks that solve my problem 😊😊
ОтветитьI love your videos.😍
ОтветитьIt is solution for me, thanks
Ответитьthe request must add the correct accept header lol
ОтветитьOne thing i find in Api response is that you enter price as a integer then why response in string please..
ОтветитьThank you, the "Accept application/json" did the trick.
ОтветитьMy Postman does not allow me to change the value for "Accept" to application/JSON . It is locked.
Ответитьthanks, very helpful
ОтветитьAt the end you skip to concatenate the rules to upper rules
Example
$rules = [
"name"=>'required',
"description"=>'required',
"price"=>'required',
]
$rules += [
"device_name"=>'required'
]
If you $this->wantsJson() condition true on that time only device_name rules validate other rules like name and description are skip because you skip to concatenate with existing rules
Love your videos ❤️
ОтветитьFor API responses I use a dedicated middleware that checks for the headers and take a look if the requested json is also valid. There were many times people were using invalid json, forgetting to put a comma or putting a comma after the last field. Not everyone uses Postman or insomnia that shows that before requesting.
ОтветитьHello, I have a question, not in that theme, but.
Is there any way to set belongstothrough relation in the laravel?
As simple as that... Thank you!
ОтветитьStarted with Laravel a couple of weeks ago, and im learning a lot with your channel, thx for all the effort you put in your videos.
ОтветитьWonderful!
ОтветитьCan you give you "how to create api" course free? Coz I've not enough money to buy but I need to learn, please, give me any coupon
Ответитьhappened to me 2 hrs ago!
ОтветитьWhat if in web we have email and in API we have email_address is conditional checking is good for this?
ОтветитьI just add a custom middleware that forces the API routes to always expect json and thus return json response.
ОтветитьWhat if i want to customize the error response style of json part??
Ответитьgreat, thank for take your time and respond me, best regard
ОтветитьIs this will give exact error instead of home page in postman without giving application/json in postman header?
ОтветитьAs always, excellent content.
I would recommend anything done via an API client should always use the Accept Application/Json header.
This is why i love laravel, it's all so easy to make it do what you want.
ОтветитьI use an "AcceptsJson" middleware in my API middleware group to force Laravel to render as JSON at any given time. Even if the client does not explicitly request it, because it does make absolutely no sense to return HTML from an API route.
Just a simple $request->headers->set('Accept', 'application/json'); et voilà.
expects json is one of those that you dont think its needed or done know about it until you do...... then you never forget.
ОтветитьGreat hint.
In the end you mistyped. Should be $rules['device_name'] = 'required'; Otherwise it adds a '0' key to $rules with value of array.
Nice video again! Are you going to be back to making public code review and publish as video here ? :)
ОтветитьThank you for your very helpful daily videos! Few questions comes from me, how I can define resources like you did for products? Where these resources stored on? I saw this resource thing in cloudcreativity/laravel-json-api and as far as I guess, if I define a resource, Laravel will automatically define some routes like list products (get), create product (post), show product (get), update product (patch), delete product (delete), and I will be able to use these routes in my API without writing controllers for each of them. Am I correct? This is the way in laravel-json-api and I found it awesome. Thanks in advance!
Ответитьthank you. With this video. I got an answer to my comment too
ОтветитьBig thanks
ОтветитьI'm gonna throw a few more questions your way since I like the current line of topics you are covering. Great Job and Thanks a Lot. Helps us little guys out a lot.
1. So say I have a validation for store method that accepts an image along with some other data (e.g name and email). Should look like this:
$request->validate([
'name' => ['required', 'string', 'min:3', 'max:255'],
'email => ['required', 'string', 'min:3', 'max:255'],
'image' => ['required', 'image', 'max:3000'],
]);
But I won't allow the the user to update the image after store. It is a one time thing only. So the validation in update method looks like this:
$request->validate([
'name' => ['required', 'string', 'min:3', 'max:255'],
'email => ['required', 'string', 'min:3', 'max:255'],
]);
Should I make 2 separate request classes or is there an easier way to handle this with a single request class ??
2. So I have a registration that, alongside some other rules, will check for a unique e-mail address in the database like this:
$request->validate([
'email => ['required', 'string', 'min:3', 'max:255', 'unique:users,email_address'],
]);
But say (with a grain of salt. This is just an example) I will no longer validate email asunique in the update method. Maybe because I am allowing duplicate e-mails for multiple accounts owned by the same person(say I can somehow check that and approve). So my update method validation looks like this:
$request->validate([
'email => ['required', 'string', 'min:3', 'max:255'],
]);
So basically my store method email validation has one extra rule(unique).
Again, do I need to make 2 separate request classes or can I somehow use one single request class to accomplish this ?
Thanks for the nice demo with the Json format check. Really Interesting
With the new Validation Rule: Rule::when($this->wantsJson(), ['required', '']) we could add this too
Your video came at the perfect time. I am testing an api and all the responses are html so far. I know what to do now.
ОтветитьAmazing video. 🙌🏼
Please can you do something on Elastic Search
Indeed great tutorial, Thank you so much for share amazing videos
ОтветитьGreat video
ОтветитьAmazing !! What a pretty code
ОтветитьYou are a superb person, as always. Now, I feel that I follow a great person 😍😍😍😍😍😍
ОтветитьAt the end of the video, the rule does work that way or should it actually be $rules['device_name'] = 'required' ? Anyway, nice tip, thanks for the videos.
ОтветитьAlways Learning from your video. <3
Ответить