how to add exception in laravel migration? - laravel

I'm doing a migration and I want the execution of "artisan migrate" to stop until a field does not have the value 'CONTACT_INFO' inside.
What I want to do is an exception when I detect that this value is not there.
public function up()
{
$emailConfiguration = EConfig::where('clave','CONTACTO_NAME')->get()->first();
$eConfig = EConfig::find($emailConfiguration->pages_id);
$Languages = Language::all();
foreach ($Languages as $key => $lang) {
$exists = !is_null($eConfig->pages_id);
if ($exists) {
$value = $eConfig->detail()->where('language_id', $lang->languages_id)->first()->pages_valor;
if (strpos($value,'CONTACTO_INFO') == false) {
InvalidOrderException::reportable(function (InvalidOrderException $e) {
echo 'error';
});
}
}
}
}

If I'm understanding your question correctly, you want to stop the migrations if a certain condition is not satisfied.
To do so, you need to throw the exception you want, for instance, with your code :
public function up()
{
//...
foreach ($Languages as $key => $lang) {
$exists = !is_null($eConfig->pages_id);
if ($exists) {
// ...
if (strpos($value,'CONTACTO_INFO') == false) {
throw new \RuntimeException('Migration stopped due to invalid data');
}
}
}
}
However you may want to wrap it in a transaction to avoid any undesired behaviors
use Illuminate\Support\Facades\DB;
public function up()
{
DB::transaction(function(){
//...
foreach ($Languages as $key => $lang) {
$exists = !is_null($eConfig->pages_id);
if ($exists) {
// ...
if (strpos($value,'CONTACTO_INFO') == false) {
throw new \RuntimeException('Migration stopped due to invalid data');
}
}
}
}
});
If you want to customize your exception, you can create your own with php artisan make:exception MyMigrationCustomException

Related

Laravel Create a request internally Resolved

I need to recreate a resquest so that it behaves like a call via api to go through the validator, but my $request->input('rps.number') always arrives empty, although I can see the data in the debug
I also couldn't get it to go through the laravel validator
I can't use a technique to make an http call, because I need to put this call in a transaction
<?php
$nota = new stdClass();
$rps = new stdClass();
$rps->numero = (int)$xml->Rps->IdentificacaoRps->Numero;
$rps->serie = (string)$xml->Rps->IdentificacaoRps->Serie;
$rps->tipo = (int)$xml->Rps->IdentificacaoRps->Tipo;
$nota->rps = $rps;
$controller = new NotaController(new Nota());
$content = new StoreNotaRequest();
$content->request->add($nota);
$result = $controller->store($content);
StoreNotaRequest
<?php
class StoreNotaRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
$request = $this->request;
return [
'rps.numero' => 'required_with:rps|numeric|between:1,999999999999999',
'rps.serie' => 'required_with:rps|string|min:1|max:5',
'rps.tipo' => 'required_with:rps|integer|in:1,2,3'
];
}
}
NotaController
<?php
class NotaController extends Controller
{
private Nota $nota;
public function __construct(Nota $nota)
{
$this->nota = $nota;
}
public function store(StoreNotaRequest $request): JsonResponse
{
// $validated = $request->validated();
try {
$nota = DB::transaction(function () use ($request) {
$request->input('rps.numero');
});
return response()->json($nota);
} catch (Throwable $e) {
return response()->json($data, 409);
}
}
}
Solution
the solution was a little too verbose, I believe it is possible to solve with less code.
more does what it needs to go through the validation of the data contained in the StoreNotaRequest
and it returns an http response, in addition to being able to put all these isolated calls in a single transaction
DB::beginTransaction();
$errors = [];
foreach ($itens as $item) {
$controller = new NotaController(new Nota());
$request = new StoreNotaRequest();
$request->setMethod('POST');
$request->request->add($nota);
$request
->setContainer(app())
->setRedirector(app(Redirector::class))
->validateResolved();
$response = $controller->store($request);
if ($response->statusText() !== 'OK') {
$errors[$item->id] = 'ERROR';
}
}
if (count($errors) === 0) {
DB::commit();
} else {
DB::rollBack();
}

Where should I define my logic that will be used in a blade template?

In this question I got a nice answer with some code. I now wonder what is the intended laravel way of implementing this.
The code is:
function getRoutesByGroup(array $group = [])
{
$list = \Route::getRoutes()->getRoutes();
if (empty($group)) {
return $list;
}
$routes = [];
foreach ($list as $route) {
$action = $route->getAction();
foreach ($group as $key => $value) {
if (empty($action[$key])) {
continue;
}
$actionValues = Arr::wrap($action[$key]);
$values = Arr::wrap($value);
foreach ($values as $single) {
foreach ($actionValues as $actionValue) {
if (Str::is($single, $actionValue)) {
$routes[] = $route;
} elseif($actionValue == $single) {
$routes[] = $route;
}
}
}
}
}
return $routes;
}
Route::group(['as' => 'main'], function () {
Route::get('/', function () {
return view('pages.start');
})->name('Home');
Route::get('/foobar', function () {
return view('pages.foobar');
})->name('Home');
Route::get('/business', function () {
return view('pages.business');
})->name('Business');
});
getRoutesByGroup(['as' => 'main']); // where to load this?
I want to use this in two blade templates to render a menu. My first thought was to put this in a Trait and use that Trait within the AppServiceProvider.php, but this seems to only have 5 internal ignite routes available so I guess its too early in the bootstrapping process.
What is the correct way in this scenario? Do I make a Facade, another Service Container, do I load this via inject in a template, do I make a global var?
I ended up making a new service provider and used view composer.
Still, if there is a better and more MVCish way I'm glad for any input.

Auth::user() Returns Null in API Controller

I'm using Laravel 7 and my problem is to get null from Auth::user();
auth()->user and Auth::id() return null as well.
BTW, in balde template Auth::user() works.
It returns null when I try to use it in controller.
What I'm trying to do is to create a comment page in backend (Vuejs) and I want to build up a filter logic. In order to do that, I want to add a new property named repliedBy into each comment in controller. If a comment isn't replied by the current user, repliedBy will be notByMe. So I don't event try to return user id to Vuejs. I can't get id even in the controller. BTW, login, registration etc work normal way.
Here is my CommentsController:
public function index()
{
$comments = Comment::join("site_languages", "language_id", "=", "site_languages.id")
->select("content_comments.*", "site_languages.shorthand as lang_shorthand")
->with(["replies", "post", "user"])
->orderBy('id', 'desc')
->get()
->groupBy("commentable_type");
$grouppedComments = [];
foreach ($comments as $type => $typeSet) {
$newType = strtolower(explode("\\", $type)[1]);
$grouppedByLanguage = $typeSet->groupBy("lang_shorthand");
$langSet = [];
foreach ($grouppedByLanguage as $lang => $commentSet) {
$grouppedBycontent = [];
foreach ($commentSet as $comments) {
$content = $newType . "_" . $comments->commentable_id;
if (array_key_exists($content, $grouppedBycontent)) {
array_push($grouppedBycontent[$content], $comments);
} else {
$grouppedBycontent[$content] = [$comments];
}
}
$groupAfterOrganized = [];
foreach ($grouppedBycontent as $content => $comments) {
$order = 1;
$commentAndReplies = [];
foreach ($comments as $comment) {
if ($comment->parent_id === null) {
if (isset($comment->order) === false || $comment->order > $order) {
$comment->order = $order;
}
array_push($commentAndReplies, $comment);
} else {
foreach ($comments as $parentComment) {
if ($parentComment->id === $comment->parent_id) {
$parent = $parentComment;
break;
}
}
foreach ($parent->replies as $replyInParent) {
if ($replyInParent->id === $comment->id) {
$reply = $replyInParent;
break;
}
}
if (isset($comment->order) === false) {
$comment->order = $order;
$order++;
}
if (isset($parent->order) === false || $parent->order > $comment->order) {
$parent->order = $comment->order;
}
$reply->order = $comment->order;
$reply->replies = $comment->replies;
$reply[$newType] = $comment[$newType];
$basePower = 6;
if ($comment->user_id !== null) {
if ($comment->user_id === Auth::id()) {
$reply->replyFrom = "me";
} else if ($comment->user->role->power >= $basePower) {
$reply->replyFrom = "staff";
} else {
$reply->replyFrom = "user";
}
} else {
$reply->replyFrom = "visitor";
}
$iReplied = false;
$staffReplied = false;
foreach ($reply->replies as $replyOfReply) {
if ($replyOfReply->user_id !== null) {
$power = $replyOfReply->user->role->power;
if ($power >= $basePower) {
$staffReplied = true;
}
}
if ($replyOfReply->user_id === Auth::id()) {
$iReplied = true;
}
}
if ($staffReplied === false) {
if ($reply->replyFrom === "user" && $reply->replyFrom === "visitor") {
$reply->replied = "notReplied";
} else {
$reply->replied = "lastWords";
}
} else if ($staffReplied && $iReplied === false) {
$reply->replied = "notByMe";
} else if ($staffReplied) {
$reply->replied = "replied";
}
}
}
$groupAfterOrganized[$content] = $commentAndReplies;
}
$langSet[$lang] = $groupAfterOrganized;
}
$grouppedComments[$newType] = $langSet;
}
return $grouppedComments;
}
api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::apiResources([
'languages' => 'API\LanguagesController',
'users' => 'API\UsersController',
'roles' => 'API\RolesController',
'tags' => 'API\TagsController',
'categories' => 'API\CategoryController',
'pictures' => 'API\PicturesController',
'posts' => 'API\PostsController',
'comments' => 'API\CommentsController'
]);
EDIT
I'm using the code down below in RedirectIfAuthenticated.php and when I try with
dd(Auth::user());
it returns null as well. BTW obviosly, redirect to backend doesn't work.
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
if (Auth::user()->role->power > 5) {
return redirect('backend');
}
return redirect(RouteServiceProvider::HOME);
}
return $next($request);
}
The solution to this problem is fairly simple . because you are using api request laravel default auth can not understand the user so here the passport comes :
https://laravel.com/docs/7.x/passport
as written in documenation you should go 3 steps :
composer require laravel/passport
php artisan migrate
php artisan passport:install
after that you can generate token for logged in users and use that token in api authentication to use for your api which is the only and more reliable way that laravel default auth .
this link can be helpful to you too :
https://laravel.io/forum/laravel-passport-vue-check-user-authentication
this way if you intent to use you api in mobile or any other application you can simply authenticate your user in that :)
hope this helps
EDIT
according To your comment now you must generate token for your vue api to use so this would be like below :
$token = $user->createToken(config('app.name'))->accessToken;
if ($this->isApiType()) {
$token = $user->createToken(config('app.name'))->accessToken;
} else {
Auth::login($user);
$redirect = $this->getRedirectTo($user);
}
this must be added in the end of your login method so if the request comes from api it generates a JWT token for you which can be used in vue for login
yes for getting the authencated user detail your API must under the auth:API middleware.
Route::group(['middleware' => 'auth:api'], function () {
}
As you are using Resource those are not under the Api middleware just put that into that and Auth::user will return the result set.
Route::group(['middleware' => 'auth:api'], function () {
Route::apiResources([
'comments' => 'API\CommentsController'
]);
}
will fix the issue.

Undefined property: GuzzleHttp\Exception\ConnectException::$status

I keep getting this error when trying to get the status of the request.
This is my code
ExpenseRepository.php
<?php
namespace Expensetrim\Api\v1\Repositories;
use Auth;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Expensetrim\Models\Company;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ExpenseRepository
{
private $api_url;
const API_GET = 'GET';
const API_POST = 'POST';
const API_PUT = 'PUT';
function __construct()
{
$this->api_url = "http://xxx-xxx-xxx/api/v1/";
}
public function api_call($method, $uri, $request = NULL)
{
try {
$url=$this->api_url.$uri;
$client = new Client(['base_uri' => $this->api_url]);
$response = ($request) ? $client->request($method, $uri, $request) : $client->request($method, $uri);
}
catch (RequestException $e) {
return $e;
}
return $this->formatResponseBody($response);
}
public static function formatResponseBody($response)
{
$body = $response->getBody(true)->getContents();
return json_decode($body);
}
public function addExpenseType($data)
{
$uri = 'expense/types/add';
$response = $this->api_call(self::API_POST, $uri, ['form_params' => $data]);
return $response;
}
Also CompanyRepository.php
public function addExpenseType($company_id, $data)
{
$data['company_id'] = $company_id;
$expense = new ExpenseRepository;
$done = $expense->addExpenseType($data);
if($done->status == 'success') {
return true;
}
return true;
}
I need to check if the status is a success or not but keep getting this error: Undefined property: GuzzleHttp\Exception\ConnectException::$status.
Please what am i doing wrong?
There is an exception thrown at this line:
catch (RequestException $e) {
return $e;
}
and you are returning the exception. The return value of the method addExpenseType is actually an exception thrown by Guzzle.
throw the exception to see the error.
change your code to
catch (RequestException $e) {
throw $e;
}
Change your formatResponseBody function to add $response->getBody()->rewind();
public static function formatResponseBody($response)
{
$response->getBody()->rewind();
$body = $response->getBody(true)->getContents();
return json_decode($body);
}
In the old version of guzzle, it read the full body without resetting the pointer after. Using rewind() will reset the pointer. If this is the source of your issue rewind() will resolve this. It has aleady been resolved in newer versions.

Symfony 1.4 form, conditional throwing sfValidatorError in embedded form

I have Page object:
Page:
actAs:
Timestampable: ~
I18n:
fields: [name,content,attachment,course_name, course_description ]
actAs:
Sluggable: { fields: [name], uniqueBy: [lang, name], canUpdate: true }
columns:
...
is_course: { type: boolean }
course_name: { type: string(255) }
course_description: { type: string(500) }
And PageForm with embedded i18n forms:
//PageForm.class.php
public function configure()
{
...
$this->embedI18n(array('pl','en'));
$this->widgetSchema->setLabel('en', 'Angielski');
$this->widgetSchema->setLabel('pl', 'Polski');
}
Fields course_name and course_description aren't required while is_course is set to false. But if is_course is enabled validation should throw error that course_name and course_description are required.
I have readed "Symfony Advanced Forms" guide and some other posts, but I don't know should I use sfValidatorCallback in PageForm or PostValidator in PageTranslationForm? I tried to use sfValidatorCallback in this way:
//PageForm.class.php
public function configure()
{
...
$this->validatorSchema->setPostValidator(
new sfValidatorCallback(array('callback' => array($this,'checkCourses')))
);
}
public function checkCourses($validator, $values)
{
if($values['is_course'])
{
if($values['pl']['course_name'] && !$values['pl']['course_description'])
{
$error = new sfValidatorError($validator,'Required filed');
throw new sfValidatorErrorSchema($validator, array( _what_name_ => $error));
}
}
return $values;
}
But i don't know how to throw error in $values['pl']['course_description'] because Symfony API says _what_name_ should be an array of errors..
I'm really confused what is what during process of validating forms in symfony.
//Edit
I have done some changes in PageTranslationForm and now it looks like this...
//PageTranslationform.class.php
public function configure()
{
//......
$this->validatorSchema->setPostValidator(
new sfValidatorCallback(array('callback' => array($this,'checkCourses')))
);
//.....
}
public function checkCourses($validator, $values)
{
if($values['course_name'] && !$values['course_description'])
{
$error = new sfValidatorError($validator,'Required');
throw new sfValidatorErrorSchema($validator, array( 'course_description' => $error));
}
elseif(!$values['course_name'] && $values['course_description'])
{
$error = new sfValidatorError($validator,'Required');
throw new sfValidatorErrorSchema($validator, array( 'course_name' => $error));
}
return $values;
}
And it almost works but... this validator should be enabled only if in PageForm is_course is set to "true". How can I access field "is_course" from PageForm in checkCourses function in PageTranslationForm?
//SOLUTION
Thanks Jeremy, I used your idea and finally got this solution:
//PageForm.class.php
public function configure()
{
$this->embedI18n(array('pl','en'));
$this->widgetSchema->setLabel('en', 'Angielski');
$this->widgetSchema->setLabel('pl', 'Polski');
//.....
if($this->getObject()->getIsCourse())
{
foreach($this->getEmbeddedForms() as $key => $form)
{
$this->validatorSchema[$key]->setPostValidator(
new sfValidatorCallback(array('callback' => array($form,'checkCourses')))
);
}
}
}
//PageTranslationForm.class.php
//configure etc
public function checkCourses($validator, $values)
{
$errorSchema = new sfValidatorErrorSchema($validator);
if($values['course_name'] && !$values['course_description'])
{
$errorSchema->addError(new sfValidatorError($validator,'required'), 'course_description');
}
elseif(!$values['course_name'] && $values['course_description'])
{
$errorSchema->addError(new sfValidatorError($validator,'required'), 'course_name');
}
elseif(!$values['course_name'] && !$values['course_description'])
{
$errorSchema->addError(new sfValidatorError($validator,'required'), 'course_name');
$errorSchema->addError(new sfValidatorError($validator,'required'), 'course_description');
}
if (count($errorSchema))
{
throw new sfValidatorErrorSchema($validator, $errorSchema);
}
return $values;
}
Thanks for you advice, It works perfectly and I hope it would be helpful :)
This should be a post validator because you are using multiple values.
When validating via a post validator, you can throw the error in two different ways:
Globally
When the error is thrown globally, it will show up as part of sfForm::renderGlobalErrors. To throw globally, simply throw the error in the callback:
public function checkCourses($validator, $values)
{
if ($invalid)
{
throw new sfValidatorError($validator, 'Required.'); //global messages should be more specific than this
}
}
Locally
To have the error be rendered locally, throw a schema with an array, as you are doing. The keys of the array will determine the fields the errors are rendered on. This is probably want you want here.
public function checkCourses($validator, $values)
{
if ($invalid)
{
$error = new sfValidatorError($validator,'Required filed');
throw new sfValidatorErrorSchema($validator, array('course_description' => $error));
}
}

Resources