Laravel excel export list in list object maatwebsite/Laravel-Excel - excel

Hi I want to export list of people with all theirs childrens in them to excel. I'm using maatwebsite/Laravel-Excel plugin and its quite easy to export users collection but I need to include users every children inside that row. Its something like [{Name, Age, Gender, Job, Children[{name, age, gender, etc...}], Birthday, etc...}]. And how do I change order of the data column displayed on excel like if I want to switch datas on Age and Gender. Thank you!

You can use WithMapping
https://docs.laravel-excel.com/3.1/exports/mapping.html
<?php
public function map($user): array
{
//create initial array with user data in order that you want columns to be
$data = [
$user->name
...
];
// the same way you should crate headers (if you have). But instead of values you should create column name
foreach($user->children as $child){
array_push($data, $child->name, $child->birthday, $child->...., ..., ...); //and so on
}
// returned data should be 1 level array as each item is a row in the table
return $data;
}

Related

How can i delete multiple row using laravel excel

I have a table with id,details.Field 'details' contain single family member details like name,age,dob etc.
table like,
|id|details|
|---|---|
|1|{'name':'john','age':'12','dob':'19-06-1987','address':'address','family_id':"1"}|
I want to delete multiple user details using excel file and data in details column (Id should not given).the excel file contain 3 column name,family_id (unique with each family),dob.i need to check those 3 field match with details column and delete the row.
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\WithValidation;
class ImportDeleteFile implements ToModel, WithValidation
{
use Importable;
public function rules(): array
{
return [
//what i do here
];
}

Android Studio Room query to get a random row of db and saving the rows 2nd column in variable

like the title mentions I want a Query that gets a random row of the existing database. After that I want to save the data which is in a specific column of that row in a variable for further purposes.
The query I have at the moment is as follows:
#Query("SELECT * FROM data_table ORDER BY RANDOM() LIMIT 1")
fun getRandomRow()
For now I am not sure if this query even works, but how would I go about writing my function to pass a specific column of that randomly selected row to a variable?
Ty for your advice, tips and/or solutions!
Your query is almost correct; however, you should specify a return type in the function signature. For example, if the records in the data_table table are mapped using a data class called DataEntry, then the query could read as shown below (note I've also added the suspend modifier so the query must be run using a coroutine):
#Query("SELECT * FROM data_table ORDER BY RANDOM() LIMIT 1")
suspend fun getRandomRow(): DataEntry?
If your application interacts with the database via a repository and view model (as described here: https://developer.android.com/topic/libraries/architecture/livedata) then the relevant methods would be along the lines of:
DataRepository
suspend fun findRandomEntry(): DataEntry? = dataEntryDao.getRandomRow()
DataViewModel
fun getRandomRecord() = viewModelScope.launch(Dispatchers.IO) {
val entry: DataEntry? = dataRepository.findRandomEntry()
entry?.let {
// You could assign a field of the DataEntry record to a variable here
// e.g. val name = entry.name
}
}
The above code uses the view model's coroutine scope to query the database via the repository and retrieve a random DataEntry record. Providing the returning DataEntry record is not null (i.e. your database contains data) then you could assign the fields of the DataEntry object to variables in the let block of the getRandomRecord() method.
As a final point, if it's only one field that you need, you could specify this in the database query. For example, imagine the DataEntry data class has a String field called name. You could retrieve this bit of information only and ignore the other fields by restructuring your query as follows:
#Query("SELECT name FROM data_table ORDER BY RANDOM() LIMIT 1")
suspend fun getRandomRow(): String?
If you go for the above option, remember to refactor your repository and view model to expect a String instead of a DataEntry object.

Exporting Laravel Model to Excel but with modification of created_at column to only date not datetime

i tried to modify a collection before returning it to be exported with maatwebsite Excel. The column i tried to modify is the created_date column (the default created_date of laravel) so that when exported to excel, i only get the date not datetime.
i tried:
namespace App\Exports;
use App\Invoice;
use Maatwebsite\Excel\Concerns\FromCollection;
class InvoicesExport implements FromCollection
{
public function collection()
{
$temp = Invoice::all();
$len = count($temp);
for($i=0;$i<$len;$i+=1){
$temp[$i]->created_at = $temp[$i]->created_at->format('m/d/Y')
}
return $temp;
}
}
in the controller:
public function export()
{
return Excel::download(new InvoicesExport, 'invoices.xlsx');
}
but, when exported, the result in the excel file is, for example: '07/26/2016T00:00:00.000000Z'
i noticed that the time become zero, and when i tried:
$temp[$i]->created_at = "some random string"
the laravel in webpage return error said that "some random string" cannot be parsed to datetime by Carbon Class constructor.
how can i make the exporting does not construct datetime with the string i give in the 'created_at' column, but just return the plain string instead? if let's say, i can't modify the database so i can't create extra column and extra column for this simple thing is too much, i think.
If you are using Maatwebsite\Excel 2.1 try column formatting by the setColumnFormat($array) method.
If Maatwebsite\Excel 3.1, try with the WithMapping interface.
created_at is marked as 'datetime' in the $cast property of your model by default so when you set it it's automatically cast as date (with Carbon::parse('the value you gave'))
If you want to export your collection with some values formatted you should not mutate the objects of the original objects but create new dedicated objects/arrays that will not interfere with model behavior nor corrupt your existing objects.

Select Editor editorParams populate values from array in textfile?

I am using Tablulator v4.1. I am creating an interface for our pharmacy organization so a member can change the meeting information without doing any coding. One field is the meeting room location which is a select editor.
Is it possible to populate the values of editorParams with a path to an array stored in a different text file? Something like:
{title:"Room", field:"room", editor:"select", editorParams:{"/textfiles/roomvalues.txt"}},
I can then create another table to add/edit the possible room choices.
This code is what I currently use.
{title:"Room", field:"room", editor:"select", editorParams:{
values:{
"A":"Room A",
"B":"Room B",
"C":"Room C"}
}},
Thank you.
You can pass a function into the values property that can then return an array of values that you can load from elsewhere
{title:"Room", field:"room", editor:"select", editorParams:{
values:function(cell){
//lookup values from data source
return values;
}}},

Selecting objects in Doctrine

$queryBuilder
->add('select', 'd.item')
->add('from', 'Entities:TypeDetail d')
->add('where', $queryBuilder->expr()->andx(
$queryBuilder->expr()->gt('d.dateValue', $dates['start']),
$queryBuilder->expr()->lt('d.dateValue', $dates['end']))
);
TypeDetail (the table) has the following fields:
id, dateValue, itemId
And the model in Symfony is:
id, dateValue, item (object)
What I want to do is get a result containing only the item objects. I don't want the item id or any of the date values (while I do need them to filter the query, I don't actually care about them coming back in the response).
Is that possible? Obviously d.item as the select is not working!
Cheers

Resources