Photo by Sai Kiran Anagani on Unsplash

Add a folder in app directory

app/Services

<?php
namespace App\Services;
class CSVExport{ protected $data; public function __construct($columns){
$this->data = ‘“‘ . implode(‘“,”’, $columns) . ‘“‘ . “\n”;
}
public function addRow($row){
$this->data .= ‘“‘ . implode(‘“,”’, $row) . ‘“‘ . “\n”;
}
public function export($filename){
header(‘Content-type: application/csv’);
header(‘Content-Disposition: attachment; filename=”’ . $filename . ‘.csv”’);
echo $this->data;
die();
}
public function __toString()
{
return $this->data;
}
}

How to use

<?php...use App\Services\CSVExport;...class SomeController extends Controller{    public function download(){        $users = User::all();        $csv = new CSVExport([            ‘Name’,            ‘Email’,            ...
...
]); foreach($users as $user){ $csv->addRow([ $user->name, $user->email,
...
...
]); } // export csv as a download $filename = ‘Users’; $csv->export($filename); }}

--

--

When you use a resource controller route, it automatically generates names for each individual route that it creates.

Example:

Route::resource('post', 'PostController');
It automatically generate routes with their name for us

POST | posts | posts.store
GET|HEAD | posts | posts.index
GET|HEAD | posts/create | posts.create
...

How we can modify automatically generated route names?

To modify name pass names

Route::resource('posts', 'PostController', [
'names' => [
'index' => 'all-posts',
'store' => 'post.new',
// etc...
]
]);

or

Specify the as option to define a prefix for every route name.

Route::resource('posts', 'PostController', [
'as' => 'prefix'
]);

This will give you routes such as prefix.posts.index, prefix.posts.store, etc.

--

--

Fillable is a protected property of a Model class and with the help of fillable property we can use short way to insert data into database like

User::create($request->all());

Or

$arrayOfData = ['name' => 'Doe', 'email' => 'test@test.com', ...];
User::create($arrayOfData);

Or

$user = new User;
$user->fill($arrayOfData);

Without fillable we need to write more code like

$user = new User;
$user->name = $request->input('name');
$user->email = $request->input('email');
...
...
$user->save();

Remember: Mass assignment vulnerability can occurs when we use fillable. To avoid use proper validation.

--

--

To hide default search box, simply pass ‘dom’:’lrtip’. This will hide default search box but searching feature still working so we can add any other custom searching.

$(‘#myTable’).DataTable({   “dom”:”lrtip”});

If you want to hide default search box and disable searching you can pass searching: false. In this we can not add any custom searching.

$(‘#myTable’).DataTable({    searching: false});

--

--

Important points to remember

  1. You can access data, computed properties, and methods inside life cycle hooks.
  2. You should not use an arrow function to define a lifecycle method.
  3. Mounting hooks are often the most-used hooks. They allow you to access your component immediately before and after the first render.

Lifecycle hooks(vue 2.x)

  1. beforeCreate
  2. created
  3. beforeMount()
  4. mounted
  5. beforeUpdate
  6. updated
  7. activated
  8. deactivated
  9. beforeDestroy
  10. destroyed
  11. errorCaptured

When to use lifecycle hooks?

When you need to know when your component is created, added to the DOM, updated, or destroyed.

--

--