I'm at Lesson 16 with using laravel 7 and API resource.
return $this->morphToMany(Like::class, 'likeable');
doesnt work (got an error of likeable table not exist) so had to change
return $this->morphOne(Like::class, 'likeable');
instead. Hope this solution is ok
You have a slight error in there. It's not 'morphToMany' but instead 'morphMany'. The goal is an one to many relationship. Alex has it correctly.
If it was a many to many relationship, it would indeed require an intermediate table - as the error message was saying.
Hope it helps.
For video 19: Apparently Laravel 5.3 has the response() method inside FormRequest class.
For those who are using the latest versions of Laravel should override this method instead:
1 /** 2* Handle a failed validation attempt. 3* 4* @param \Illuminate\Contracts\Validation\Validator $validator 5* @return void 6* 7* @throws \Illuminate\Validation\ValidationException 8*/ 9protected function failedValidation(Validator $validator) : void10{11 if ($this->expectsJson()) {12 throw new HttpResponseException(response()->json(13 ['data' => $validator->errors()]14 , 422));15 }1617 throw (new ValidationException($validator))18 ->errorBag($this->errorBag)19 ->redirectTo($this->getRedirectUrl());20}
This is working for me and i think is the correct approach.
Hi Christos
I'm working my way through this series and am using Laravel 5.8.36. There have been a few hiccoughs but, in general, my experience has been similar to that displayed by Alex in the videos. I came to 19 and also saw that response() isn't in FormRequest.php any longer. I have tried the approach you suggested but it gave an error that HttpResponseException wasn't found. I added it but then another error cropped up. Has anyone else had better luck with this? The error format I have with Laravel 5.8.36 is
1{2 "message": "The given data was invalid.",3 "errors": {4 "body": [5 "The body field is required."6 ]7 }8}
Is it my idea or video 18 at the end is broken?
For people watching this course now, you can use the build in API resources (php artisan make:resource UserResource) instead of Fractal.
Does Fractal still make sense?
How can I get the password grant auth to work with "CreateFreshApiToken" as mentioned here https://laravel.com/docs/5.8/passport#consuming-your-api-with-javascript so I don't have to pass in the token with every request making the API vulnerable to attacks?
Lesson 18: There's only the first 7:55 :-( I tired in Firefox and Google Chrome.
yeah you're right. same
This is great! Anyone knows what is the best practice to create an endpoint to return the current user details?
Hi, I got stuck in lesson #4. I created a fresh laravel 5.7 project and when I tried to login to the endpoint /oauth/token I am sending the same data as you:
1{2 "grant_type": "password",3 "client_id": "2",4 "client_secret": "BsUKWr9kYFDX1PcbywK3uuCWjbQmnrCjx2uav0aT",5 "username": "alex@codecourse.com",6 "password": "ilovecats",7 "scope": "*"8}
But for some reason I am not getting the token as you showed
1{2 "error": "invalid_credentials",3 "message": "The user credentials were incorrect."4}
Also, why do you send the email as username? I tried to change username for email as parameter but it seems passport specifically requires a username, and in this case it must be the email, which causes confusion because in the example you are using a username field but with a different purpose.
I hope you can help me with this error.
I'm also using laravel 5.7 and work fine.
Did you chang name column as username (users table)? If not it may causing the error...
https://stackoverflow.com/questions/50204308/laravel-passport-always-say-require-username-and-password-even-the-valid-credent
Hi! Anyone can explain me why in this tutorial are we using Laravel Passport rather than JWT tymondesigns?
Well, creators are free to decide whether to use JWT or Laravel Passport.
There's another course with JWT Auth and laravel:
https://codecourse.com/watch/nuxt-js-laravel-authentication
Have a nice coding day!
Thank you very much for your reply Ache. My questions is more about the benefits or when using one vs other. For instance, in one of the first videos Alex says that Passport is not suitable for SPA.
Hi Alex, what is the best way to validate if the post belongs to the last topic in the url?
is it possible to do this using policy?
I tried something like this:
1public function update(UpdatePostRequest $request, Topic $topic, Post $post) 2{ 3 if ($topic->id !== $post->topic->id) { 4 throw new \Exception("message", 1); 5 } 6 7 $this->authorize('update', $post); 8 9 $post->body = $request->get('body', $post->body);10 $post->save();1112 return fractal()13 ->item($post)14 ->parseIncludes(['user'])15 ->transformWith(new PostTransformer)16 ->toArray();17}
Any suggestion?
Hi Alex
how we can get all posts that liked by one user
Hi! Does anyone now how the User Model ist connected to the Users table? Looked up in the Laravel Documentation but the suggested answer does not match the project. (video 3) That would be cool. Thanks