Laravel ajax file upload method not allowed - ajax

Using Laravel 5.4
I'm trying to submit a form with a file to upload with ajax.
My jquery code:
$("#uploadRoastsForm").submit(function() {
var url = $(this).attr('action');
var fd = new FormData();
var files = $('#roastFile')[0].files;
if(files.length > 0)
fd.append('file', files[0]);
fd.append('file', files[0]);
fd.append('roast_date', $('#roastDate').val());
$.ajax({
url: url,
type: 'POST',
data: fd,
contentType: false,
processData: false,
success: function (data) {...
My form:
<form action="/warehouse/upload" method="post" enctype="multipart/form-data" class="form" id="uploadRoastsForm">
{{ csrf_field() }}
<div class="form-group" style="width:150px">
<label>Roasting Date</label>
<div class="input-group">
<input name="roast_date" type="text" value="{{ old('roast_date') }}" class="form-control datepick" id="roastDate">
<div class="input-group-addon">
<span class="fa fa-calendar"></span>
</div>
</div>
</div>
<div class="form-group">
<label>Upload Roast Files</label>
<input type="file" name="file" id="roastFile">
</div>
I am getting this in firebug:
XHRPOSThttp://highland.local/warehouse/upload
[HTTP/1.1 419 unknown status 250ms]
message ""
exception
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php"
line 204
trace [ {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, … ]
0 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php", line: 176, function: "prepareException", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php"
line 176
function "prepareException"
class "Illuminate\\Foundation\\Exceptions\\Handler"
type "->"
1 Object { file: "C:\\wamp64\\www\\highland.local\\app\\Exceptions\\Handler.php", line: 77, function: "render", … }
file "C:\\wamp64\\www\\highland.local\\app\\Exceptions\\Handler.php"
line 77
function "render"
class "Illuminate\\Foundation\\Exceptions\\Handler"
type "->"
2 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 83, function: "render", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 83
function "render"
class "App\\Exceptions\\Handler"
type "->"
3 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 55, function: "handleException", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 55
function "handleException"
class "Illuminate\\Routing\\Pipeline"
type "->"
4 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php", line: 49, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php"
line 49
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
5 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\View\\Middleware\\ShareErrorsFromSession"
type "->"
6 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
7 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php", line: 63, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php"
line 63
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
8 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\Session\\Middleware\\StartSession"
type "->"
9 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
10 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php", line: 37, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php"
line 37
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
11 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse"
type "->"
12 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
13 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php", line: 66, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php"
line 66
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
14 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\Cookie\\Middleware\\EncryptCookies"
type "->"
15 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
16 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 104, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 104
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
17 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php", line: 684, function: "then", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php"
line 684
function "then"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
18 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php", line: 659, function: "runRouteWithinStack", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php"
line 659
function "runRouteWithinStack"
class "Illuminate\\Routing\\Router"
type "->"
19 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php", line: 625, function: "runRoute", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php"
line 625
function "runRoute"
class "Illuminate\\Routing\\Router"
type "->"
20 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php", line: 614, function: "dispatchToRoute", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Router.php"
line 614
function "dispatchToRoute"
class "Illuminate\\Routing\\Router"
type "->"
21 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php", line: 176, function: "dispatch", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php"
line 176
function "dispatch"
class "Illuminate\\Routing\\Router"
type "->"
22 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 30, function: "Illuminate\\Foundation\\Http\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 30
function "Illuminate\\Foundation\\Http\\{closure}"
class "Illuminate\\Foundation\\Http\\Kernel"
type "->"
23 Object { file: "C:\\wamp64\\www\\highland.local\\app\\Http\\Middleware\\ForceSsl.php", line: 29, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\app\\Http\\Middleware\\ForceSsl.php"
line 29
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
24 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "App\\Http\\Middleware\\ForceSsl"
type "->"
25 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
26 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php", line: 31, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php"
line 31
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
27 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest"
type "->"
28 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
29 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php", line: 31, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php"
line 31
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
30 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest"
type "->"
31 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
32 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php", line: 27, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php"
line 27
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
33 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 163, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 163
function "handle"
class "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize"
type "->"
34 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php", line: 53, function: "Illuminate\\Pipeline\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\Pipeline.php"
line 53
function "Illuminate\\Pipeline\\{closure}"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
35 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", line: 104, function: "Illuminate\\Routing\\{closure}", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Pipeline\\Pipeline.php"
line 104
function "Illuminate\\Routing\\{closure}"
class "Illuminate\\Routing\\Pipeline"
type "->"
36 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php", line: 151, function: "then", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php"
line 151
function "then"
class "Illuminate\\Pipeline\\Pipeline"
type "->"
37 Object { file: "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php", line: 116, function: "sendRequestThroughRouter", … }
file "C:\\wamp64\\www\\highland.local\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php"
line 116
function "sendRequestThroughRouter"
class "Illuminate\\Foundation\\Http\\Kernel"
type "->"
38 Object { file: "C:\\wamp64\\www\\highland.local\\public\\index.php", line: 53, function: "handle", … }
file "C:\\wamp64\\www\\highland.local\\public\\index.php"
line 53
function "handle"
class "Illuminate\\Foundation\\Http\\Kernel"
type "->"
I also tried to have an input as a submit button on the form and when I click it I call the exact same ajax, but then I got the error: MethodNotAllowed.
Something breaks when I use: contentType: false, processData: false. But if I don't use them I can't send the file to the controller.
Can anyone help please?
How do you upload file with ajax in Laravel?

At first you didn't posted what is the method in your route/web.php for your ajax request.
If it is MethodNotAllowed related problem then probably your ajax request and route method is not same.
If you are trying to update something then use PUT method in your route like below
Route::put('/warehouse/upload', "WarehouseController#upload');
And if you are trying to insert data into database table then use POST method:
Route::post('/warehouse/upload', "WarehouseController#upload');
As you are using POST method from your jQuery AJAX function, then just use post method in your route.
I think it should solve your query.

Related

Laravel API Request - “Unauthorized”

Good day to you all,
Today I decided to authenticate my experimental api. I installed Passport, but I get a constant response:
{
error: {
message: "Unauthorized"
}
}
I tried disable it, and just use in api.php:
Route::get('test', function (){
return 1;
});
As a testing point, but still I get the same message, so I assume it has nothing to do with the Passport package.
Kernel.php:
<?php
namespace App\Http;
use App\Http\Middleware\Authenticate;
use App\Http\Middleware\Cors;
use App\Http\Middleware\CrossDomain;
use App\Http\Middleware\EncryptCookies;
use App\Http\Middleware\ForceJsonResponse;
use App\Http\Middleware\isAdmin;
use App\Http\Middleware\isCompany;
use App\Http\Middleware\RedirectIfAuthenticated;
use App\Http\Middleware\TrimStrings;
use App\Http\Middleware\TrustProxies;
use App\Http\Middleware\VerifyCsrfToken;
use Cog\Laravel\Ban\Http\Middleware\ForbidBannedUser;
use Cog\Laravel\Ban\Http\Middleware\LogsOutBannedUser;
use Fruitcake\Cors\HandleCors;
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
use Illuminate\Auth\Middleware\Authorize;
use Illuminate\Auth\Middleware\EnsureEmailIsVerified;
use Illuminate\Auth\Middleware\RequirePassword;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull;
use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance;
use Illuminate\Foundation\Http\Middleware\ValidatePostSize;
use Illuminate\Http\Middleware\SetCacheHeaders;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Routing\Middleware\ValidateSignature;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;
use RenatoMarinho\LaravelPageSpeed\Middleware\ElideAttributes;
use RenatoMarinho\LaravelPageSpeed\Middleware\InlineCss;
use RenatoMarinho\LaravelPageSpeed\Middleware\InsertDNSPrefetch;
use RenatoMarinho\LaravelPageSpeed\Middleware\RemoveComments;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* #var array
*/
protected $middleware = [
TrustProxies::class,
HandleCors::class,
PreventRequestsDuringMaintenance::class,
ValidatePostSize::class,
TrimStrings::class,
ConvertEmptyStringsToNull::class,
];
/**
* The application's route middleware groups.
*
* #var array
*/
protected $middlewareGroups = [
'web' => [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
CrossDomain::class,
InlineCss::class,
ElideAttributes::class,
InsertDNSPrefetch::class,
RemoveComments::class,
//CollapseWhitespace::class,
//DeferJavascript::class,
],
'api' => [
//'throttle:api',
SubstituteBindings::class,
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* #var array
*/
protected $routeMiddleware = [
'auth' => Authenticate::class,
'auth.basic' => AuthenticateWithBasicAuth::class,
'cache.headers' => SetCacheHeaders::class,
'can' => Authorize::class,
'guest' => RedirectIfAuthenticated::class,
'password.confirm' => RequirePassword::class,
'signed' => ValidateSignature::class,
'throttle' => ThrottleRequests::class,
'verified' => EnsureEmailIsVerified::class,
'forbid-banned-user' => ForbidBannedUser::class,
'logs-out-banned-user' => LogsOutBannedUser::class,
'is_admin' => isAdmin::class,
'is_company' => isCompany::class,
'json.response' => ForceJsonResponse::class,
'cors' => Cors::class,
];
}
config/auth.php:
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'passport',
'provider' => 'users',
'hash' => false,
],
],
/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
/*
|--------------------------------------------------------------------------
| Password Confirmation Timeout
|--------------------------------------------------------------------------
|
| Here you may define the amount of seconds before a password confirmation
| times out and the user is prompted to re-enter their password via the
| confirmation screen. By default, the timeout lasts for three hours.
|
*/
'password_timeout' => 10800,
];
routes/api.php:
<?php
use Illuminate\Support\Facades\Route;
Route::get('test', function () {
return 1;
});
Route::group(['middleware' => ['cors', 'json.response']], function () {
Route::group(['prefix' => 'auth'], function () {
Route::post('login', 'Api\AuthController#login');
Route::group(['middleware' => 'auth:api'], function () {
Route::get('logout', 'Api\AuthController#logout');
Route::get('user', 'Api\AuthController#user');
});
});
Route::group(['middleware' => 'auth:api'], function () {
// Companies
Route::group(['prefix' => 'provider/company'], function () {
Route::post('store', 'Api\Provider\CompanyController#store');
Route::get('show/{id}', 'Api\Provider\CompanyController#show');
Route::patch('update', 'Api\Provider\CompanyController#update');
Route::delete('destroy/{id}', 'Api\Provider\CompanyController#destroy');
Route::get('branches/{id}', 'Api\Provider\CompanyController#branches');
});
// Branches
Route::group(['prefix' => 'provider/branch'], function () {
Route::post('store', 'Api\Provider\BranchController#store');
Route::get('show/{id}', 'Api\Provider\BranchController#show');
Route::patch('update', 'Api\Provider\BranchController#update');
Route::delete('destroy/{id}', 'Api\Provider\BranchController#destroy');
Route::get('classifieds/{id}', 'Api\Provider\BranchController#classifieds');
});
// Classifieds
Route::group(['prefix' => 'provider/classified'], function () {
Route::post('store', 'Api\Provider\ClassifiedController#store');
Route::get('show/{identifier}', 'Api\Provider\ClassifiedController#show');
Route::patch('update', 'Api\Provider\ClassifiedController#update');
Route::delete('destroy/{identifier}', 'Api\Provider\ClassifiedController#destroy');
});
// Attributes
Route::group(['prefix' => 'provider/data'], function () {
Route::get('categories', 'Api\Provider\DataController#categories');
Route::get('attributes/{parent_id}', 'Api\Provider\DataController#attributes');
});
});
});
Error Stack:
{
message: "Too Many Attempts.",
exception: "Illuminate\Http\Exceptions\ThrottleRequestsException",
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php",
line: 200,
trace: [
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php",
line: 121,
function: "buildException",
class: "Illuminate\Routing\Middleware\ThrottleRequests",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Routing\Middleware\ThrottleRequests.php",
line: 63,
function: "handleRequest",
class: "Illuminate\Routing\Middleware\ThrottleRequests",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "Illuminate\Routing\Middleware\ThrottleRequests",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 103,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
line: 697,
function: "then",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
line: 672,
function: "runRouteWithinStack",
class: "Illuminate\Routing\Router",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
line: 636,
function: "runRoute",
class: "Illuminate\Routing\Router",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Routing\Router.php",
line: 625,
function: "dispatchToRoute",
class: "Illuminate\Routing\Router",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
line: 166,
function: "dispatch",
class: "Illuminate\Routing\Router",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 128,
function: "Illuminate\Foundation\Http\{closure}",
class: "Illuminate\Foundation\Http\Kernel",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\app\Http\Middleware\ForceJsonResponse.php",
line: 20,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "App\Http\Middleware\ForceJsonResponse",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\renatomarinho\laravel-page-speed\src\Middleware\PageSpeed.php",
line: 28,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "RenatoMarinho\LaravelPageSpeed\Middleware\PageSpeed",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\renatomarinho\laravel-page-speed\src\Middleware\PageSpeed.php",
line: 28,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "RenatoMarinho\LaravelPageSpeed\Middleware\PageSpeed",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\renatomarinho\laravel-page-speed\src\Middleware\PageSpeed.php",
line: 28,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "RenatoMarinho\LaravelPageSpeed\Middleware\PageSpeed",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\renatomarinho\laravel-page-speed\src\Middleware\PageSpeed.php",
line: 28,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "RenatoMarinho\LaravelPageSpeed\Middleware\PageSpeed",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\renatomarinho\laravel-page-speed\src\Middleware\PageSpeed.php",
line: 28,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "RenatoMarinho\LaravelPageSpeed\Middleware\PageSpeed",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\renatomarinho\laravel-page-speed\src\Middleware\PageSpeed.php",
line: 28,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "RenatoMarinho\LaravelPageSpeed\Middleware\PageSpeed",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\fruitcake\laravel-cors\src\HandleCors.php",
line: 57,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "Fruitcake\Cors\HandleCors",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php",
line: 21,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull.php",
line: 31,
function: "handle",
class: "Illuminate\Foundation\Http\Middleware\TransformsRequest",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TransformsRequest.php",
line: 21,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\TrimStrings.php",
line: 40,
function: "handle",
class: "Illuminate\Foundation\Http\Middleware\TransformsRequest",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "Illuminate\Foundation\Http\Middleware\TrimStrings",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\ValidatePostSize.php",
line: 27,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "Illuminate\Foundation\Http\Middleware\ValidatePostSize",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance.php",
line: 86,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\fideloper\proxy\src\TrustProxies.php",
line: 57,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 167,
function: "handle",
class: "Fideloper\Proxy\TrustProxies",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Pipeline\Pipeline.php",
line: 103,
function: "Illuminate\Pipeline\{closure}",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
line: 141,
function: "then",
class: "Illuminate\Pipeline\Pipeline",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\vendor\laravel\framework\src\Illuminate\Foundation\Http\Kernel.php",
line: 110,
function: "sendRequestThroughRouter",
class: "Illuminate\Foundation\Http\Kernel",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\public\index.php",
line: 55,
function: "handle",
class: "Illuminate\Foundation\Http\Kernel",
type: "->"
},
{
file: "C:\Users\lopes\PhpstormProjects\Ocasiao-CMS\server.php",
line: 21,
function: "require_once"
}
]
}
Can any of you give me a hint or explanation on how to fix this/
If you removed Auth middleware but still getting unauthorized means there may be route cache. Try using php artisan route:clear and check.
I just realised there was another package installed conflicting with this one.
Thank you all!

Managing workflows with nodejs

I am trying to use nodejs to manage my workflows instead of using the console. The createWorkflow method returns a 200 code but I cannot find it in the console or when listing it. Any idea what is missing? the typescript typedef has too many partials to be useful on what is needed and what is not. In the code below the list method returns correctly the workflow names I created in the console which proves that I use the parent params correctly I guess. However when I call the create method with the name of a new workflow and some code, I get this this back and a return code of 200 which should mean it was successful but can't find it. Also trying to execute it returns a not found.
return value:
{
"_events": {},
"_eventsCount": 2,
"completeListeners": 0,
"hasActiveListeners": false,
"latestResponse": {
"name": "projects/xxxxx/locations/us-central1/operations/operation-1607572432499-5b6141fca342d-32b153bf-bfce0551",
"metadata": {
"type_url": "type.googleapis.com/google.cloud.workflows.v1beta.OperationMetadata",
"value": {
"type": "Buffer",
"data": [
10,
12,
8,
208,
183,
198,
254,
5,
16,
236,
212,
251,
180,
2,
26,
76,
112,
114,
111,
106,
101,
99,
116,
115,
47,
98,
117,
116,
116,
101,
114,
102,
108,
121,
45,
105,
116,
45,
55,
102,
55,
55,
98,
47,
108,
111,
99,
97,
116,
105,
111,
110,
115,
47,
117,
115,
45,
99,
101,
110,
116,
114,
97,
108,
49,
47,
119,
111,
114,
107,
102,
108,
111,
119,
115,
47,
116,
101,
115,
116,
95,
119,
111,
114,
107,
102,
108,
111,
119,
95,
118,
49,
34,
6,
99,
114,
101,
97,
116,
101,
42,
6,
118,
49,
98,
101,
116,
97
]
}
},
"done": false
},
"name": "projects/xxxxxxxxxxxx/locations/us-central1/operations/operation-1607572432499-5b6141fca342d-32b153bf-bfce0551",
"done": false,
"longrunningDescriptor": {
"operationsClient": {
"auth": {
"checkIsGCE": true,
"jsonContent": null,
"cachedCredential": {
"_events": {},
"_eventsCount": 0,
"transporter": {},
"credentials": {
"access_token": "ya29.c.KpcB6AdCAMGMr_FqY7veU-uQTAP2cenQDWOh3Msaw-CPjdodjeYKAEf7lw-m1joxmam06_4QgRJ5Atnlpcm7db37CAi0lz4LS5_KPkvaodE6oefkDChOly92BxyCfaJClrKqklcEbSt1yg-2iVwngXccgwtdko9sbM4UeUihNPYScdGY0bGT484x7Ai6e2vtAfZMC2r-DqGE-g",
"token_type": "Bearer",
"expiry_date": 1607573907456,
"refresh_token": "compute-placeholder"
},
"certificateCache": {},
"certificateExpiry": null,
"certificateCacheFormat": "PEM",
"refreshTokenPromises": {},
"eagerRefreshThresholdMillis": 300000,
"forceRefreshOnFailure": false,
"serviceAccountEmail": "default",
"scopes": [
"https://www.googleapis.com/auth/cloud-platform"
]
},
"_cachedProjectId": "xxxxxxxxxxxx",
"defaultScopes": [
"https://www.googleapis.com/auth/cloud-platform"
],
"_getDefaultProjectIdPromise": {}
},
"innerApiCalls": {},
"descriptor": {
"listOperations": {
"requestPageTokenField": "pageToken",
"responsePageTokenField": "nextPageToken",
"resourceField": "operations"
}
}
}
},
"result": null,
"metadata": {
"createTime": {
"seconds": "1607572432",
"nanos": 647948908
},
"target": "projects/xxxxxxxxxxxx/locations/us-central1/workflows/test_workflow_v1",
"verb": "create",
"apiVersion": "v1beta"
},
"backoffSettings": {
"initialRetryDelayMillis": 100,
"retryDelayMultiplier": 1.3,
"maxRetryDelayMillis": 60000,
"initialRpcTimeoutMillis": null,
"rpcTimeoutMultiplier": null,
"maxRpcTimeoutMillis": null,
"totalTimeoutMillis": null
},
"_callOptions": {
"timeout": 30000,
"retry": {
"retryCodes": [],
"backoffSettings": {
"initialRetryDelayMillis": 100,
"retryDelayMultiplier": 1.3,
"maxRetryDelayMillis": 60000,
"initialRpcTimeoutMillis": 60000,
"rpcTimeoutMultiplier": 1,
"maxRpcTimeoutMillis": 60000,
"totalTimeoutMillis": 600000
}
},
"autoPaginate": true,
"otherArgs": {
"headers": {
"x-goog-request-params": "parent=projects%2Fxxxxxxxxxxxx%2Flocations%2Fus-central1"
}
},
"bundleOptions": null,
"isBundling": true,
"apiName": "google.cloud.workflows.v1beta.Workflows"
}
}
** code: **
engine: ExecutionsClient;
builder: WorkflowsClient;
location = 'us-central1';
projectId: string;
constructor() {
this.engine = new ExecutionsClient();
this.builder = new WorkflowsClient();
this.projectId = serviceAccount.project_id;
}
async list(): Promise<any> {
console.log (`list workflows`);
const [ resp ] = await this.builder.listWorkflows({
parent: this.builder.locationPath(this.projectId, this.location),
});
console.log (`listWorkflow: ${JSON.stringify(resp)}`);
return resp;
}
async create(name: string, code: string): Promise<any> {
console.log (`creating workflow named: ${name}`);
const [ resp ] = await this.builder.createWorkflow({
parent: this.builder.locationPath(this.projectId, this.location),
workflow: {
sourceContents: code,
},
workflowId: name
});
console.log (`createWorkflow: ${JSON.stringify(resp)}`);
return resp;
}
async execute(name: string): Promise<any> {
const [resp] = await this.engine.createExecution({
parent: this.engine.workflowPath(this.projectId, this.location, name),
});
return resp;
}
After consulting the source for google-cloud/workflows I found where the problem was. createWorkflow is what they call a long operation and is actually 2 successive promises
const [operation] = await client.createWorkflow(request);
const [response] = await operation.promise();

How to check tags uniqueness of tags using spatie/laravel-tags plugin

Using spatie/laravel-tags plugin in my Laravel 5.7 app.
I make editor of tags, so that admin would be able to add tags . However, I need to make validation on tags uniqueness.
Usual validation rules like
'name' => [
'required',
'string',
'max:255',
Rule::unique(with(new Tag)->getTableName())->ignore($tag_id),
],
'order_column' => 'nullable|integer',
does not work here, as fields are in json format like:
{"en": "Drama"}
Could you, please, give a hint in which way this validation can be done?
MODIFIED BLOCK # 2
In my request app/Http/Requests/TagRequest.php I wrote validation rules:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Request;
use App\Rules\TagUniqueness;
class TagRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
// $request= Request();
return [
'name' => [
'required',
'string',
'max:255',
new TagUniqueness,
],
'order_column' => 'nullable|integer',
];
}
}
and created rule app/Rules/TagUniqueness.php has:
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
class TagUniqueness implements Rule
{
public function __construct()
{
$this->d( '<pre>TagUniqueness $_REQUEST::' . print_r( $_REQUEST, true ) );
}
public function passes($attribute, $value)
{
$this->d( '<pre>passes $attribute::' . print_r( $attribute, true ) );
$this->d( '<pre>passes $value::' . print_r( $value, true ) );
return false;
}
...
and I recieved this error:
"message": "trim() expects parameter 1 to be string, object given",
"exception": "ErrorException",
"file": "/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Validation/ValidationRuleParser.php",
"line": 217,
"trace": [
{
"function": "handleError",
"class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
"type": "->"
},
{
"file": "/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Validation/ValidationRuleParser.php",
"line": 217,
"function": "trim"
},
{
"file": "/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/laravel/framework/src/Illuminate/Validation/ValidationRuleParser.php",
"line": 199,
"function": "parseArrayRule",
"class": "Illuminate\\Validation\\ValidationRuleParser",
"type": "::"
},
{
"file": "/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/proengsoft/laravel-jsvalidation/src/Remote/Validator.php",
"line": 161,
"function": "parse",
"class": "Illuminate\\Validation\\ValidationRuleParser",
"type": "::"
},
{
"file": "/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/proengsoft/laravel-jsvalidation/src/Remote/Validator.php",
"line": 144,
"function": "purgeNonRemoteRules",
"class": "Proengsoft\\JsValidation\\Remote\\Validator",
"type": "->"
},
{
"file": "/mnt/_work_sdb8/wwwroot/lar/Votes/vendor/proengsoft/laravel-jsvalidation/src/Remote/Validator.php",
"line": 116,
"function": "setRemoteValidation",
"class": "Proengsoft\\JsValidation\\Remote\\Validator",
"type": "->"
},
{
as $this->d( my debugging method, I see output of TagUniqueness' s constructer, but passes method IS NOT CALLED and I can not define the reason of this error.
What is wrong?
Thanks!

How to normalize an array of flat objects into its entities?

Given an input that looks like this:
[
{
id: 11,
valueId: 22,
valueDescription: 'Some value',
referenceId: 33,
referenceDescription: 'Some reference',
groupId: 44,
groupDescription: 'Some group'
},
{
id: 55,
valueId: 66,
valueDescription: 'Another value',
referenceId: 77,
referenceDescription: 'Another reference',
groupId: 88,
groupDescription: 'Another group'
}
]
And a desired output of:
{
entities: {
types: {
"11": { id: 11, valueId: 22, referenceId: 33, groupId: 44 },
"55": { id: 55, valueId: 66, referenceId: 77, groupId: 88 },
},
values: {
"22": { id: 22, description: "Some value" },
"66": { id: 66, description: "Another value" },
},
references: {
"33": { id: 33, description: "Some reference" },
"77": { id: 77, description: "Another reference" },
},
groups: {
"44": { id: 44, description: "Some group" },
"88": { id: 88, description: "Another group" },
}
},
result: [ 11, 55 ]
}
I'm not sure how I'd define Schemas for my 4 entity types that would pull multiple fields off of the flattened root object and also rename them. I see there's the assignEntity argument I can pass which I think I would use for the renaming part, but I'm not sure how I would define a Schema to indicate that one flat object becomes four entities.

How do I keep the hash associations when building permutations

UPDATE: Possible solution at the end, but certainly not performant or ideal.
I created the following method that gives me close to what I want.
def multi_permutations(collection)
case collection.length
when 1
return collection.shift[1]
when 0
raise "You must pass in a multidimensional collection."
end
a = collection.shift[1]
b = multi_permutations(collection)
return_value = []
a.each do |a_value|
b.each do |b_value|
return_value << [a_value] + [b_value]
end
end
return return_value
end
When I pass in a hash with nested arrays that looks like this...
my_collection["item_9"] = [152]
my_collection["item_2"] = [139, 143, 145]
my_collection["item_13"] = [138, 142, 150]
my_collection["item_72"] = [137, 149, 151, 154]
my_collection["item_125"] = [140, 141]
my_collection["item_10"] = [144, 146, 147, 148, 153]
I want it to create an array of hashes with all permutations that looks like this...
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 144 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 146 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 147 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 148 }]
[{ "item_9": 152 }, { "item_2": 139 }, { "item_13": 138 }, { "item_72": 137 }, { "item_125": 140 }, { "item_10": 153 }]
.
.
.
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 144 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 146 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 147 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 148 }]
[{ "item_9": 152 }, { "item_2": 145 }, { "item_13": 150 }, { "item_72": 154 }, { "item_125": 141 }, { "item_10": 153 }]
What this function ends up doing is close but I lose the relationships.
[152, [139, [138, [137, [140, 144]]]]]
[152, [139, [138, [137, [140, 146]]]]]
[152, [139, [138, [137, [140, 147]]]]]
[152, [139, [138, [137, [140, 148]]]]]
[152, [139, [138, [137, [140, 153]]]]]
.
.
.
[152, [145, [150, [154, [141, 144]]]]]
[152, [145, [150, [154, [141, 146]]]]]
[152, [145, [150, [154, [141, 147]]]]]
[152, [145, [150, [154, [141, 148]]]]]
[152, [145, [150, [154, [141, 153]]]]]
The relationships are very important to me. The reason is, I plan to hydrate an object where the attrs are the keys in the hash. I'm sure this can be done in a better way, and I'm open to suggestions.
So one possible solution I've come up with is to create a keys array then flatten the permutations and zip them together in a hash.
results = []
permutations = multi_permutations(possibilities)
permutations.each do |permutation|
results << Hash[keys.zip permutation.flatten!]
end
This ends up giving me...
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>146}
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>147}
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>148}
{"item_9"=>152, "item_2"=>145, "item_13"=>150, "item_72"=>154, "item_125"=>141, "item_10"=>153}
I couldn't get your solution to run because you did not provide how to call it, so I couldn't compare the efficiency.
What do you think about this solution? (the single argument has to be a Hash of Arrays, just like your my_collection)
my_collection = {}
my_collection["item_9"] = [152]
my_collection["item_2"] = [139, 143, 145]
my_collection["item_13"] = [138, 142, 150]
my_collection["item_72"] = [137, 149, 151, 154]
my_collection["item_125"] = [140, 141]
my_collection["item_10"] = [144, 146, 147, 148, 153]
def permutations!(input)
input.each do |key, possibilities|
possibilities.map!{|p| {key => p} }
end
digits = input.keys.map!{|key| input[key] }
digits.shift.product(*digits)
end
results = permutations!(my_collection)
Please notice that this method modifies the input object because of the map! usage.

Resources