Laravel Import Data from CSV to Table


Laravel Import Data from CSV to Table – The Laravel Import Data from CSV to Table is used to import file to CSV to table.


Laravel Import Data from CSV to Table | with full Example.

Let us understand how to use Laravel Import Data from CSV to Table.

Full example of Import Data from CSV to Table.

Now here i am going to explain how to Import Data from CSV to Table.

First we have to create controller page and save as [ImportController.php].

Let’s look at a simple example.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\ServiceProvider;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use App\TestModel;
//use App\Excel;
use Excel;

class ImportController extends Controller 
{
	public function upload()
	{
		return view('Import');
	}
   public function ImportClient()
	{
		$file = Input::file('file');
		$file_name = $file->getClientOriginalName();
		$file->move('files', $file_name);
		$result = Excel::load('files/'.$file_name, function($reader)
		{
			$reader->all();
		})->get();
		return view('Import');
	}
}

Then create a view page and save as [Import.blade.php]

Let’s look at a simple example.

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Import example</title>
    <link rel="stylesheet" href="http://getbootstrap.com/dist/css/bootstrap.css">
</head>
<body>
<form action="import1" method="post" enctype="multipart/form-data">
<br><br><br><br>
<table align="center">
<tr><td><b>File Import</b></td>
<td><input type="file" name="file"></td></tr>
<tr><td>
<input type = "hidden" name = "_token" value = "<?php echo csrf_token() ?>"></td></tr>
<tr>
<td></td>
<td>
<input type="submit" name="upload" value="upload"></td>

</tr>
</table>
</form>
</body>
</html>

Route Path:-

Route::get('/Import','ImportController@upload');
Route::post('/import1','ImportController@ImportClient');
Laravel Import Data from CSV to Table
Laravel Import Data from CSV to Table


Advertisements

Add Comment

📖 Read More