Ep19 - Laravel Exceptions | Laravel API Server

Ep19 - Laravel Exceptions | Laravel API Server

Acadea.io

2 года назад

7,436 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Amirul Islam Anirban
Amirul Islam Anirban - 28.04.2023 14:56

U are awesome man. Great video, thank you so much.

Ответить
Riza
Riza - 05.10.2022 04:43

Thanks so much

Ответить
root Default ¿
root Default ¿ - 16.06.2022 12:57

Great video much appreciated but i when i create my own exception class and at the render, function return a JSON object with a message saying failed I don't get that it returns a laravel view(the one you get when you start a new project).
Laravel 8 project

public function register(Request $request)
{
$request->validate([
'name' => 'required',
'email' => 'required',
'password' => 'required',
'user_type' => 'required',
'village' => 'required',
]);

try {
$user = new User;
$user->name = $request->name;
$user->email = $request->email;
$user->password = Hash::make($request->password);
$user->user_type = $request->user_type;
$user->village = $request->village;

if ($user->user_type == 'farmer') {
$user = new User;
$user->name = $request->name;
$user->email = $request->email;
$user->password = Hash::make($request->password);
$user->user_type = $request->user_type;
$user->village = $request->village;
if($user->save()){
return 'user success';
}

$farmer = new Farmer;
$farmer->farmer_id = $user->id;
$farmer->region = $request->region;
$farmer->crop_type = $request->crop_type;
if($farmer->save()){
return 'farmer success';
}
// throw when save failed
if (!$user->save())
throw new UserNotSavedException('User creation fail');

}

// throw when null
if ($request == null)
throw new NullException('Request is null');


}catch (UserNotSavedException $e) {
return $e->render();
}
catch (NullException $e) {
return $e->render();
}

-------------------------
my null exception class
-------------------------

<?php

namespace App\Exceptions;

use Exception;

class NullException extends Exception
{
public function report(){ }

public function render($request)
{
return new JsonResponse([
'errors' =>['message' => $this.getMessage()]]);
}
}

-------------------------
my userNotSaved exception
--------------------------
<?php

namespace App\Exceptions;

use Exception;

class UserNotSavedException extends Exception
{

public function render($request)
{
return new JsonResponse([
'errors' =>['message' => $this.getMessage()]]);
}
}

thanks in advance :)

Ответить
Amir Hadi Tutorials
Amir Hadi Tutorials - 25.09.2021 10:22

so great tutorials

Ответить
Volkan Kahraman
Volkan Kahraman - 24.09.2021 00:29

Greate video but you don't need to create exception for just converting response to json. Laravel already doing that. All you have to do is add "Content-Type" "application/json" inside header

Ответить
Drew Adorador
Drew Adorador - 17.09.2021 10:09

Again, thank you very much explaining the small details, and snippets of helper functions. They are very helpful in coding with laravel.

Ответить