We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Neeltje Chanterie • 3 years ago

Finally I found something that works.
I'm new to laravel so this helps alot!
But the only problem I have is with the timestamps ... because you don't insert the created_at timestamp I get an error and can't see my view ...
Any help/solution?

Hardik Savani • 3 years ago

you can insert data this way :
Item::create($insert);
instead of
DB::table('items')->insert($insert);
try this timestamps will work

Dodiya Nilesh • 2 years ago

Thanks friend it`s really help-full me

Neeltje Chanterie • 3 years ago

Now it works for the timestamps and creates a record, but the values of $insert are not inserting ...

Hardik Savani • 3 years ago

did you write field name on $fillable in app/Item.php Model like :
public $fillable = ['title','description'];

Neeltje Chanterie • 3 years ago

Yes I did, but my timestamps are protected ...

Hardik Savani • 3 years ago

can you comment $dates variable and check again for just testing, that way we can check it is working or not ?

Neeltje Chanterie • 3 years ago

Same results as before,
If I add DB::table('products')->insert($insert); it only inserts the info of the product
and if I add Product::create($insert); it inserts only the timestamps.

Hardik Savani • 3 years ago

Hmm, It's strange, Can you try with protected into public fillable variable in Model ?
Like : public $fillable = ['title','description'];

Neeltje Chanterie • 3 years ago

Also doesn't work ...
I wil just add a column for created_at in my sheet for now ...

Hardik Savani • 3 years ago

Ok, now you have to insert using DB this way :
$insert['created_at'] = date('Y-d-m H:i:s');
DB::table('items')->insert($insert);

Neeltje Chanterie • 3 years ago

I found the solution!
you can just use this function instead, it's shorter and more up-to-date,
it wil insert automatically the timestamps, by using "create()".
public function importExcel()
{
if(Input::hasFile('import_file')){
$path = Input::file('import_file')->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
Item::create([
'title' => $value->title,
'description' => $value->description
]);
}
}
return back();
}

Welton L. Santos • 3 years ago

in laravel 5.3?

Moockie • 3 years ago

Nice solution, but I wonder how you can check whether the data was added in order to echo a success/fail message. Any ideas? Hardik Savani

Chanon Saelee • 3 years ago

Neeltje Chanterie I'm new to Laravel too and that you mean use
Item::create() instead $insert[] right ?
and I must write field name of my timestamps on $fillable in app/Item.php Model ?

Hardik Savani • 3 years ago

Yes, right you have to add field name in $fillable variable of Model.

Chanon Saelee • 3 years ago

Okay. and if I want to put some of that data to other table I must create new model and use them in function importExcel() like this ?

use Input;
use App\Item;
use App\Role;
use App\OtherModel;
use DB;
use Excel;

...
...
public function importExcel()
{
...
...
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
Item::create([
'title' => $value->title,
'description' => $value->description
]);
Role::create([
'role' => $value->role,
'level' => $value->level
]);
OtherModel::create([
'aaaa' => $value->aaaa,
'bbbb' => $value->bbbb
]);
}
}
...
...
return back();
}

Hardik Savani • 3 years ago

Yes, right this way

Chanon Saelee • 3 years ago

I just try that code step by step and found some problem

FatalErrorException in MaatwebsiteDemoController.php line 35:
Cannot access protected property Maatwebsite\Excel\Collections\RowCollection::$title

I don't know how to fix it .

Neeltje Chanterie • 3 years ago

This is my whole controller:

namespace App\Http\Controllers;

use App\Product;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
//use Illuminate\Support\Facades\Request;

use App\Http\Requests;
use Illuminate\Support\Facades\Storage;

use Input;
use Validator;
use Redirect;
use Session;
use Excel;
use Illuminate\Support\Facades\DB;
use Kyslik\ColumnSortable;
use Carbon\Carbon;

class ProductController extends Controller
{
public function importExcel()
{
if(Input::hasFile('import_file')){
$path = Input::file('import_file')->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
Product::create([
'product_code' => $value->product_code,
'name' => $value->name,
'brand' => $value->brand,
'brand_number' => $value->brand_number,
'serial_number' => $value->serial_number,
'production_date' => $value->production_date,
'description' => $value->description,
'tags' => $value->tags,
'QR_code' => $value->QR_code,
'purchase_price' => $value->purchase_price,
'purchase_date' => $value->purchase_date,
'CanBeLentByTeacher' => $value->CanBeLentByTeacher,
'CanBeLentByStudent' => $value->CanBeLentByStudent,
'type' => $value->type,
'active' => $value->active,
]);
}
}
else{
dd("fout in het bestand, kijk na of de titels juist staan, download het voorbeeld.");
}
}
return redirect('products');
}
}

I hope this helps for now ... I'm on vacation right now so this is the best I can give ... My project must be on private for school on bitbucket, but if you want I can add you to the repository?
I had the same error and my teacher just unstalled and installed the maatwebsite package and them it worked fine ...

Chanon Saelee • 3 years ago

Thank you so much.
Now it work fine. That problem come from my excel file it have many empty sheet .

Sorry for my slow response, I'm in weekend. :D

Hardik Savani • 3 years ago

Thanks for your good response, yes i can see your project, if you want.

Neeltje Chanterie • 3 years ago

Sorry, bc of the php tag he excluded my namespaces ... I eddited it and deleted some functions ...

Dou you have a bitbucket acc? If so, give me your username so I can add you.

Habtamu Fissha • 2 years ago

bro good jobs

Ngwenya Glass • 2 years ago

Works like a charm! thank you for helping us deal with our mountain bike event results :)

Gaurangmaheta • 3 years ago

how to validation in empty field of excel fiel

Gaurangmaheta • 3 years ago

How to validation on empty cell of excel file ??

Hardik Savani • 3 years ago

you can make your own or you can follow bellow like :
https://laravel.com/docs/5....

Gaurangmaheta • 3 years ago

Thank u

ishadi • 3 years ago

hi, can you explicitly explain how to export data that has been filtered? as i can see your post just explain more about importing data using maatwebsite. great post btw thanks

Hardik Savani • 3 years ago

Thank you
i added downloadExcel() function in controller then for export data,
In that function you can change query, i write like this:
$data = Item::get()->toArray();

Vimal Kashiyani • 3 years ago

I will recommend to All @@@

eric muga • 2 years ago

Use $request-> instead of Input:: otherwise good stuff.

ARNAB MUNSHI • 2 years ago

I want to use the $data in another function ... Any solution ?

Donald • 2 years ago

Hi Just located this tutorial, great work and great comments below. How do you handle spaces in the title row in a csv file when using this method? for each field in this line
$insert[] = ['title' => $value->title, 'description' => $value->description];

If it reads
(1)
$insert[] = ['title' => $value->My Title, 'description' => $value->My Description]; I get errors.

FatalErrorException
syntax error, unexpected 'Title' (T_STRING), expecting ']'
in ImportDataController.php (line 33)

I have tried editing to...
(2)
$insert[] = ['title' => $value->'My Title', 'description' => $value->'My Description'];
#2 error
FatalErrorException
syntax error, unexpected ''My Title'' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
in ImportDataController.php (line 33)

and edit (3)
$insert[] = ['title' => $value->"My Title", 'description' => $value->"My Description"];

FatalErrorException
syntax error, unexpected '"My Title"' (T_CONSTANT_ENCAPSED_STRING), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$'
in ImportDataController.php (line 33)

Any work around other than changing the column string in the csv to remove spaces.

Thanks Greatly.

Sheikh Muhammad Talha • 2 years ago

https://uploads.disquscdn.c...
I am new to laravel, can you please tell me why there are these errors after uploading csv ?

Hardik Savani • 2 years ago

Did you add route ?

Sheikh Muhammad Talha • 2 years ago
Sheikh Muhammad Talha • 2 years ago

https://uploads.disquscdn.c...
I am new to laravel, and am getting this error can not figure out why it is saying missing controller, as it is already there and have exact same code as yours

mayur vadher • 2 years ago

hi...
it working with csv export.. but returns error when trying to excel export like below.

This site can’t be reached

filter%5Bdegree_id%5D%5Bvalue%5D=28&filter%5Bdegree_id%5D%5Btype%5D=1&filter%5Bsemester_id%5D%5Bvalue%5D=19&filter%5Bsemester_id%5D%5Btype%5D=1&filter%5Bsubject_id%5D%5Bvalue%5D=1&filter%5Bsubject_id%5D%5Btype%5D=1&filter%5Bexam_id%5D%5Bvalue%5D=23&filter%5Bexam_id%5D%5Btype%5D=1&download=Download+CSV might be temporarily down or it may have moved permanently to a new web address.

Kayalvizhi • 2 years ago

Excellent !

Enes Adıgüzel • 2 years ago

Hey could you maybe help me? I can't get the data from my table.
https://uploads.disquscdn.c...

https://uploads.disquscdn.c...

Siti Rahmah • 2 years ago

after opened file:

excel cannot the file 'users'.xlsx , because the file format or file extension is not valid, bla3x :(
help meee ...

this code ->

$data = User::get()->toArray();
return Excel::create('users', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data)
{
$sheet->fromArray($data);
});
})->download('xlsx');

I Used Laravel 5.2

guptas89 • 2 years ago

Hi,

I am using the same code to download the excel file. Can you please help me what should be the structure of $data in array format.
I am not getting data from tables. I have to create my own data array in acceptable format.
Here is my code:

public function downloadExcel()
{
$data[] = array('Red','Blue','Green', 'Black');
return Excel::create('itsolutionstuff_example', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data)
{
$sheet->fromArray($data);
});
})->download('xlsx');
}

ishan shah • 2 years ago

Worked perfectly! Thanx man.

Hardik Savani • 2 years ago

Thanks to you for good response :)

Roshan • 2 years ago

I have installed maatwebsite/excel package in laravel 5.3. But when i try to simply upload CSV and get their data then it gives me this error
"FatalErrorException in Excel2007.php line 94: Class 'ZipArchive' not found"

Please help me.
Thanks.

Roth Phearom • 2 years ago

i have same error i can't fix please help me.

https://uploads.disquscdn.c...

Aceros Hexagon • 2 years ago

I have install a fresh laravel 5.2 and try to follow this tutorial, but i found something like this went i import a file

Hardik Savani • 2 years ago

Can you tell me what error you found or anything ?