We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
you can insert data this way :
Item::create($insert);
instead of
DB::table('items')->insert($insert);
try this timestamps will work
Thanks friend it`s really help-full me
Now it works for the timestamps and creates a record, but the values of $insert are not inserting ...
did you write field name on $fillable in app/Item.php Model like :
public $fillable = ['title','description'];
Yes I did, but my timestamps are protected ...
can you comment $dates variable and check again for just testing, that way we can check it is working or not ?
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.
Hmm, It's strange, Can you try with protected into public fillable variable in Model ?
Like : public $fillable = ['title','description'];
Also doesn't work ...
I wil just add a column for created_at in my sheet for now ...
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);
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();
}
in laravel 5.3?
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
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 ?
Yes, right you have to add field name in $fillable variable of Model.
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();
}
Yes, right this way
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 .
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 ...
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
Thanks for your good response, yes i can see your project, if you want.
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.
bro good jobs
Works like a charm! thank you for helping us deal with our mountain bike event results :)
how to validation in empty field of excel fiel
How to validation on empty cell of excel file ??
you can make your own or you can follow bellow like :
https://laravel.com/docs/5....
Thank u
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
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();
I will recommend to All @@@
Use $request-> instead of Input:: otherwise good stuff.
I want to use the $data in another function ... Any solution ?
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.
https://uploads.disquscdn.c...
I am new to laravel, can you please tell me why there are these errors after uploading csv ?
Did you add route ?
yes i did
https://uploads.disquscdn.c...
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
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.
Excellent !
Hey could you maybe help me? I can't get the data from my table.
https://uploads.disquscdn.c...
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
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');
}
Worked perfectly! Thanx man.
Thanks to you for good response :)
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.
i have same error i can't fix please help me.
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
Can you tell me what error you found or anything ?
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?