Laravel Excel I do not understand what is being filled

Asked

Viewed 72 times

0

I do not understand what is happening in this code that fills a cell B8 in excel:

$sheet->cell('BQ'.$i, function($cell) use($report) {
    $cell->setValue(!empty($report->PastaStatusHistorical->where('status_historical.status','Concluído')->first()) ?
    $report->PastaStatusHistorical->where('status_historical.status','Concluído')->first()->created_at->format('d/m/Y') : '');
});

A date is being passed from the database or application?

How do I find this table and column in the bank?

That one where-> indicates that there is a select?

created_at-> is a method?

What’s going on with PastaStatusHistorical? I go in the template and do not find these status fields 'completed'.

1 answer

0


First you need to understand that this type of query is done using Eloquent, it is an Activerecord deployment to work with the database, the complete documentation is available here,

I will start by formatting the code without the use of the Ternary operator, for larger code snippets I prefer to use the if / else for the sake of readability.

$sheet->cell('BQ'.$i, function($cell) use($report) {

    if(!empty($report->PastaStatusHistorical->where('status_historical.status','Concluído')->first()) {

        $valor = $report->PastaStatusHistorical->where('status_historical.status','Concluído')->first()->created_at->format('d/m/Y');

    } else {

        $valor = '';

    }

    $cell->setValue($valor);

});

The columns created_at and updated_at are columns automatically managed by Eloquent, they do not need to be referenced in the template, basically when a record is created, the current server time will be registered in the column created_at, and when an update happens on that line the field updated_at will be updated.

Yes, the where indicates that a query is being made, and first() defines that only the first line found should be returned.

The PastaStatusHistorical is a relationship of tables defined in the model, hasMany is a relationship 1 : N, if you understand English I suggest reading the official documentation, if I did not find this article in Portuguese.

  • Thank you very much, now in the model ta essa jossa here : public Function Pastastatushistorical(){ Return $this->hasMany('App Models Statushistoricalfolder', 'pasta_id'); } // I couldn’t leave it as a code ... can you explain to me what’s going on ?? Do I still have to go after this Historical Folder status, which is another model to understand what’s going on ? Hell, this hellhole has come to make someone’s life easier ??

  • @Victorhugo, I edited the answer with an article in Portuguese about the relationship between tables, and about Laravel help, I think it helps a lot, once you understand the Eloquent ORM, you will hardly need to write some SQL code, and making the right relationships, you will never again do INNER JOIN in their consultations.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.