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

If Laravel collection has more than 1000 items the hasMany relationship collections are dissapearing

I have a Laravel query:

$var = Model::with(['rel1','rel2.1','rel2.2','rel2.3','rel3','rel4'])
->whereBetween('datum', [$start, $end])
->get();

If $var has more than 1000 items the hasMany relationships return an empty collection. If the same query has less than 1000 items the relationships are fine.

I have tried to set the PHP ini settings:

ini_set('memory_limit', '512M');

ini_set('max_input_vars', 30000);

ini_set('max_execution_time', 380);

But nothing worked so far.

On localhost everything works as expected, this issue only apears on the web server. Laravel is 5.4, PHP version 7.2.24

I'd like to get the relationships even if the collection is largen than 1000.

Thanks for your help.



via Chebli Mohamed

mercredi 30 mars 2022

Trying to get data using forgein key Angular

This is laravel function in the controller

public function getcatproduct($id)
    {
        $categories = category::all();
        $products = product::where('category_id', "=", $id)->first();
        // $products = $prod->categories;
        $categories = category::findOrFail($id);
        return response()->json([
            'data' => $categories,
            'data' => $products,
            'message' => 200
        ]);
    }

Typescript code getCatByProduct is a service I'm using laravel as an API !

   ngOnInit(): void {
  
    this.id = this.route.snapshot.paramMap.get('id');
    this.getProductbyCategData();
   
  }

getProductbyCategData(){
    this.dataService.getCatByProduct(this.id).subscribe(res => {
     this.data = res;
     this.product = this.data;
   
    });   }


via Chebli Mohamed

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