Reputation: 81
I want to ask a question. I'm new in laravel framework. I want to create a text file from the input form data user using the Laravel framework. while the same time, I want to save these input to my database.
I have success to save in my database. but I fail to create the .txt file from this.
I have tried to get data from database but error. and I just try to get data from input form is an error too.
I've tried...
<?php
$nama = $_POST['name'];
$email = $_POST['email'];
$dob = $_POST['date'];
$phone = $_POST['phone'];
$gender = $_POST['gender'];
$address = $_POST['addreess'];
$date = date('dmY');
$jam = data('his');
$data = "$nama,$email,$dob,$phone,$gender,$address";
$file = "$nama"-"$date$jam";
$namafile = "$file.txt";
$fh = fopen($namafile,"w");
fwrite($fh,$data);
fclose($file);
echo "<h2>Hasil Penyimpanan Data</h2>";
echo "<hr>";
echo "Hasil : <a href='$namafile'> $namafile </a>";
I use it in my index.php with the means that I can get data from the input form directly and then save as a .txt file.
Here's my code in the controller that's saving to my database:
public function store(Request $request)
{
$employee = new Employee();
$employee->nama = $request->get('name');
$employee->email = $request->get('email');
$employee->dob = $request->get('date');
$employee->phone = $request->get('phone');
$employee->gender = $request->get('gender');
$employee->addreess = $request->get('addreess');
$employee->save();
return redirect('employees')->with('success','Selamat, Data berhasil di tambahkan !');
}
How can I save the input data to the database and at the same time how can I save the data to a text file?
Thanks for your help. :)
Upvotes: 5
Views: 25992
Reputation: 3775
In Laravel, use put() of Storage facade.
use Illuminate\Support\Facades\Storage;
Storage::disk('local')->put('file.txt', 'Your content here');
// 'file.txt' // yo can use your file name here.
// 'Your content here' // you can specify your content here
This will be stored in storage/app/
so your controller will look like this,
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use App\Employee;
class HomeController extends Controller
{
public function store(Request $request)
{
Storage::disk('local')->put('file.txt', 'Your content here');
$employee = new Employee();
$employee->nama = $request->get('name');
$employee->email = $request->get('email');
$employee->dob = $request->get('date');
$employee->phone = $request->get('phone');
$employee->gender = $request->get('gender');
$employee->addreess = $request->get('addreess');
$employee->save();
return redirect('employees')->with('success','Selamat, Data berhasil di tambahkan !');
}
}
Upvotes: 8