jeudi 31 mars 2022

upload file with laravel

I´m trying upload one file to storage/app with laravel for this, i´m checking that my folder exist:

if(isset($request->file_job)){
            $pathFolder = storage_path('app/contratos/'.$tarea->contratoTrey->serie->SERIE.'-'.$tarea->contratoTrey->N_CONTRATO.'/tareas/T-'.$tarea->ntarea);
           
            if(!\File::exists($pathFolder)) {
                \File::makeDirectory($pathFolder, 0755, true, true);
            }


            $filePath = 'app/contratos/'.$tarea->contratoTrey->serie->SERIE.'-'.$tarea->contratoTrey->N_CONTRATO.'/tareas/T-'.$tarea->ntarea;
            \Storage::disk('local')->put($filePath, $request->file_job);
        }

if not exist i´m building my structure folder and put my file in this folder.

My problem it´s in my DB i´m insert my file name with getClientOriginalName() but in this route

'app/contratos/'.$tarea->contratoTrey->serie->SERIE

my app was builded a file, not directory and in this directory my file with his original name...

i don´t know that i´m doing wrong.. i trayed creating a new variable with this name and save... same error or problem...

Thanks for readme and help me. Sorry for my bad english



via Chebli Mohamed

mercredi 30 mars 2022

Correct setup for a Laravel resource of a jsonb column?

I have a jsonb column in Postgres that I'm trying to make use of with Laravel. When the resource itself is returned (rest, etc), the value for that column is returned null:

{
    "content": [
        {
            "partner_uuid": "bc844aa7-283e-4b82-8188-2f8764bc9d59",
            "metadata": null
        }
    ]
}

From the backend, this row definitely is non-null:

bc844aa7-283e-4b82-8188-2f8764bc9d59 | {"test": "test"}

I'm not very familiar with Laravel, and so I'm struggling a bit. The problem seems to be in the resource itself, which has a single function:

class PartnerMetadataResource extends JsonResource
{
    public function toArray($request)
    {
        return [
            'partner_uuid' => $this->partner_uuid,
            'metadata' => $this->metadata,
        ];
    }
}

If the metadata column is any other column type, this code seems sufficient to return the value as expected. I've tested with integers, text/varchar (and even the other column which is type uuid).

What is needed to return a value for that column? Either as escaped text, or as proper nested json?



via Chebli Mohamed

Multiple queue workers: some restart, some don't with ERROR (spawn error)

Our application provides a separate database for each of our users. I have set up an emailing job which users may dispatch to run in background via a Laravel 5.3 Queue. Some users succeed in evoking this facility (so it does work - same codebase) but some users fail.

The users who fail to generate the email are all characterised by the following error when trying to restart all user queues using sudo supervisor start all, eg:

shenstone-worker:shenstone-worker_02: ERROR (spawn error)
shenstone-worker:shenstone-worker_00: ERROR (spawn error)
shenstone-worker:shenstone-worker_01: ERROR (spawn error)

An example of a user who's email facility works:

meadowwood-worker:meadowwood-worker_02: started
meadowwood-worker:meadowwood-worker_00: started
meadowwood-worker:meadowwood-worker_01: started

The log of all attempted restarts has a load of these spawn errors at the beginning then all the successful queue restarts at the end.

The worker config files for these two users are:

[program:shenstone-worker]
process_name=%(program_name)s_%(process_num)02d
directory=/var/www/solar3/current
environment=APP_ENV="shenstone"
command=php artisan queue:work --tries=1 --timeout=300
autostart=true
autorestart=true
user=root
numprocs=3
redirect_stderr=true
stdout-logfiles=/var/www/solar3/storage/logs/shenstone-worker.log

and

[program:meadowwood-worker]
process_name=%(program_name)s_%(process_num)02d
directory=/var/www/solar3/current
environment=APP_ENV="meadowwood"
command=php artisan queue:work --tries=1 --timeout=300
autostart=true
autorestart=true
user=root
numprocs=3
redirect_stderr=true
stdout-logfiles=/var/www/solar3/storage/logs/meadowwood-worker.log

As you see, generically identical. Yet shenstone does not restart its queues to capture requests from its jobs table, but meadowwood does. No logfiles appear in storage.

So why do some of these queues restart successfully, and a load don't?

Looking at the stackoverflow issue Running multiple Laravel queue workers using Supervisor inspired me to run sudo supervisorctl status and yes I can see a more elaborate explanation of my problem:

shenstone-worker:shenstone-worker_00                               FATAL     too many open files to spawn 'shenstone-worker_00'
shenstone-worker:shenstone-worker_01                               FATAL     too many open files to spawn 'shenstone-worker_01'
shenstone-worker:shenstone-worker_02                               FATAL     too many open files to spawn 'shenstone-worker_02'

As opposed to:

meadowwood-worker:meadowwood-worker_00                             RUNNING   pid 32459, uptime 0:51:52
meadowwood-worker:meadowwood-worker_01                             RUNNING   pid 32460, uptime 0:51:52
meadowwood-worker:meadowwood-worker_02                             RUNNING   pid 32457, uptime 0:51:52

But I still cannot see what I can do to resolve the issue.



via Chebli Mohamed

mardi 29 mars 2022

where is forceScheme function y laravel

I want to work in https app I cant geerate https links whit "url" blade function in some ask question say have use this, but, not works

\Illuminate\Support\Facades\URL::forceScheme('https');


via Chebli Mohamed

Laravel validation rule based on other fields value

I have this radio button

<input class="form-check-input" type="radio" name="discount" value="Yes">
<input class="form-check-input" type="radio" name="discount" value="No" checked>

and this hidden field

 <input type="hidden" name="discount_valid" value="true">

by default this hidden field is true

Now when i'm trying to validate if discount_valid is true then it should submit the form this code is working but i want to add another condition, if discount is No then it should submit the form irrespective of whether discount value is true or false. If discount is Yes and discount_valid is false then form should not submit.

$validator = Validator::make($request->all(), [
            'discount_valid'=>'in:true',
]);                           

                                         


via Chebli Mohamed

lundi 28 mars 2022

realpath(): open_basedir restriction in effect

everyone. I get this error on my shared host. I contact the support and they said that they don't give permission for my account to open_basedir area. (Since it is not an upgraded account) What can I do unless buy for premium version.



via Chebli Mohamed

dimanche 27 mars 2022

pass data to welcome message

my register has a welcome message (email), it generates a random number

    protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        't_matricula' => $data['t_matricula'],
        'number' => rand(100, 9200),
        'matricula' => $data['matricula'],
        'password' => Hash::make($data['password']),
    ]);

    Mail::to($user->email)->send(new WelcomeMail($user));
    
    return $user;
}

I need to pass that generated value (Number) to the welcome email, then print it in the email template



via Chebli Mohamed