I just setting up Maatwebsite/Laravel-Excel plugin from here https://github.com/Maatwebsite/Laravel-Excel/. I want to read excel file and display data in Html table in a blade In laravel. I got datas by using following:
$content=Excel::load($file, function($reader) {
})->get();
when I echo $content; I have following ::
SheetCollection {#762 ▼
#title: ""
#items: array:3 [▼
0 => RowCollection {#623 ▼
#title: "Sheet1"
#items: array:4 [▼
0 => CellCollection {#693 ▼
#title: null
#items: array:2 [▼
"name" => "Abc"
"city" => "City1"
]
}
1 => CellCollection {#692 ▼
#title: null
#items: array:2 [▼
"name" => "Xyz"
"city" => "city2"
]
}
2 => CellCollection {#710 ▼
#title: null
#items: array:2 [▼
"name" => "Name2"
"city" => "city3"
]
}
3 => CellCollection {#709 ▼
#title: null
#items: array:2 [▼
"name" => "Name2"
"city" => "city3"
]
}
]
}
1 => RowCollection {#728 ▼
#title: "Sheet2"
#items: []
}
2 => RowCollection {#697 ▼
#title: "Sheet3"
#items: []
}
]
}
Those are data on my xlsx file . Now I want to render it on view blade . any Idea???? thanks
You can use the Laravel Collections methods to get data. Explore the methods but I think you can try something like this.
return $content->all();
or iterate each row and store them formated in your data to render it in a blade view using each.
$content->each(function($row){
// process here each row
});
I hope it can help you.
Related
How can I sort this Collection by the values? It is needed also to add another index before each item because this way it is not possible since two items will have the same index, in our case: "User1".
I need this Collection:
Collection {#254 ▼
#items: array:2 [▼
"User1" => Collection {#253 ▼
#items: array:1 [▼
3 => 12.0
]
}
"User2" => Collection {#255 ▼
#items: array:2 [▼
3 => 11.0
1 => 13.0
]
}
]
}
to be sorted by the last values, and be separated in other "items":
Collection {#254 ▼
#items: array:3 [▼
"User2" => Collection {#255 ▼
#items: array:2 [▼
1 => 13.0
]
}
"User1" => Collection {#253 ▼
#items: array:1 [▼
3 => 12.0
]
}
"User2" => Collection {#255 ▼
#items: array:2 [▼
3 => 11.0
]
}
]
}
I can convert it in an array and make an iteration, but maybe Laravel has a simpler way.
Just use the sortBy() method. And specify wich value you want to sort
Here you can find all information you need about the collection methods:
https://laravel.com/docs/5.7/collections#method-sort
i have a problem with laravel session :
Route::get("/set",function(){
session(["s1"=>'foo']);
Session::save();
dump(Session::all());
dump(session("s1"));
exit;
});
the first dump result is like this :
array:4 [▼
"_token" => "YbH1A9frXuUsk6O01IdshL1rXmih2GtpiRXdvZoC"
"s1" => "foo"
"_sf2_meta" => array:3 [▶]
"_flash" => array:2 [▶]
]
and the second:
"foo"
ok .. everything is ok , but when i go to second route like bellow problem occurred :
Route::get("/read",function(){
dump(session("s1"));
dump(Session::all());
exit;
});
the result is like this , first dump :
null
and second dump :
array:1 [▼
"_token" => "LVH9mnoWVRfiAuFkIbEgEWr56hgLpityuHCJTBj3"
]
please help me ...
I have the next collection:
Collection {#356 ▼
#items: array:31 [▼
0 => {#359 ▼
+"id": 17
+"zone_id": 2
+"name_de": "Österreich"
+"name_en": "Austria"
+"name_iso": "AUSTRIA"
+"tld": "at"
+"iso3166": "AT"
+"phone": 43
+"vat_regex": "/^U[0-9]{8}$/"
+"shop_id": 17
+"country_id": 165
}
1 => {#360 ▼
+"id": 2
+"zone_id": 2
+"name_de": "Belgien"
+"name_en": "Belgium"
+"name_iso": "BELGIUM"
+"tld": "be"
+"iso3166": "BE"
+"phone": 32
+"vat_regex": "/^[01][0-9]{9}$/"
+"shop_id": 17
+"country_id": 25
}]
}
And I want to get the next result as associative array:
[
"AT" => "Austria",
"BE" => "Belgium"
]
I'm trying to do it using:
$keyed = $countries->map(function ($item) {
return [$item->iso3166 => $item->name_en];
});
But I'm getting:
Collection {#357 ▼
#items: array:31 [▼
0 => array:1 [▼
"AT" => "Austria"
]
1 => array:1 [▼
"BE" => "Belgium"
]
]
}
What I'm doing wrong or how can I achieve the associative array?
Note: I'm using Laravel 5.2 so mapWithKeys() Collection method is not implemented.
You want to use function ->pluck('name_en', 'iso3166').
I make the following query
$group = Group::with('user.campaign')->where('groupName', 'TeamA')->get();
This returns something like the following
Collection {#429 ▼
#items: array:1 [▼
0 => Group {#398 ▼
#table: "user_groups"
#guarded: []
#attributes: array:5 [▶]
#original: array:5 [▶]
#relations: array:1 [▼
"User" => Collection {#402 ▼
#items: array:5 [▼
0 => User {#409 ▶}
1 => User {#410 ▶}
2 => User {#411 ▶}
3 => User {#412 ▶}
4 => User {#413 ▶}
]
}
]
#hidden: []
#visible: []
#appends: []
#fillable: []
}
]
}
So I can see User is within the relations. However, I am having issues accessing the users which are apart of the Group TeamA.
If I try
$group->user
I get an error. Unfortunately, my error page at the moment only seems to say something went wrong, without any details.
How would I go about accessing the users of this group?
Thanks
i think you have some mistake on that , User field is a collection ...
that mean , User field is not instance of User class and is collection of users
"User" => Collection {#402 ▼
#items: array:5 [▼
0 => User {#409 ▶}
1 => User {#410 ▶}
2 => User {#411 ▶}
3 => User {#412 ▶}
4 => User {#413 ▶}
]
}
i suggest you to rename User field in Team class to Users.
after that it's good to know, property name should be camel case ...
Can some please tell me how to display the results of this array in a view in Laravel 5?
RowCollection {#464 ▼
#title: "Worksheet"
#items: array:3 [▼
0 => CellCollection {#472 ▼
#title: null
#items: array:2 [▼
"name" => "Owen Kelley"
"age" => 29.0
]
}
1 => CellCollection {#473 ▼
#title: null
#items: array:2 [▼
"name" => "Jim Jackson"
"age" => 50.0
]
}
2 => CellCollection {#474 ▼
#title: null
#items: array:2 [▼
"name" => "Sally Anne"
"age" => 35.0
]
}
]
}
I have been struggling with this for ages!
Many thanks.
You should use foreach, example:
In controller/routes.php:
return view('yourviewname')->with('elephants', 'elephants')
In blade view:
#foreach($elephants as $elephant)
{{ $elephant->name }}
#endforeach