Datatable server side php class for laravel - laravel

I want my datatables to process the data on the server side, I am referencing this example.
Server Side Example
However, the server side php class "ssp.class.php" given in this example uses core php with raw sql, I can not use it directly for laravel projects. Does anyone has reference to laravel way doing datatables. I don't want to use any packages at the moment though.

UPDATED 31/01/2023
You can use syamsoul/laravel-datatable-two-ssp package..
Link here
Open CLI and enter your app directory
Run this command composer require syamsoul/laravel-datatable-two-ssp
In your controller, need to add use SoulDoit\DataTableTwo\SSP;, and you can follow the code below:
private function dtColumns()
{
return [
['label'=>'ID', 'db'=>'id', 'formatter'=>function($value){
return str_pad($value, 8, '0', STR_PAD_LEFT);
}],
['label'=>'Email', 'db'=>'email' ],
['label'=>'Username', 'db'=>'username' ],
['label'=>'Created At', 'db'=>'created_at' ],
];
}
private function dtQuery($selected_columns)
{
return \App\Models\User::select($selected_columns);
}
This is working perfectly on Laravel 8 and above.
.
.
Below is my old answer.. (21 March 2019)
You can use syamsoul/laravel-datatable-ssp package..
Link here
Open CLI and enter your app directory
Run this command composer require syamsoul/laravel-datatable-ssp
In your controller, need to add use SoulDoit\DataTable\SSP;, and you can follow the code below:
public function get(){
$dt = [
['db'=>'id', 'dt'=>0, 'formatter'=>function($value, $model){ return str_pad($value, 8, '0', STR_PAD_LEFT); }],
['db'=>'email', 'dt'=>1],
['db'=>'first_name', 'dt'=>2, 'formatter'=>function($value, $model){ return $value . ' ' . $model->last_name; }],
['db'=>'created_at', 'dt'=>3],
['db'=>'email_verified_at'],
['db'=>'last_name'], // must include this because need to re-use in 'first_name' formatter
];
$dt_obj = new SSP('\App\User', $dt);
$dt_arr = $dt_obj->getDtArr();
return response()->json($dt_arr);
}
This is working perfectly on Laravel 5.8 ... I'm not sure about the other versions.

Related

Laravel Nova Card Options Missing Function

I was recently given a Laravel app that uses Nova and Vue in it - all three of which I've never worked with before, so apologies in advance if I ask something that should be obvious. Inside the app there is a custom card that was already setup by the previous developer that works fine and I'm just trying to add an extra property for the Vue side to read. The structure of the card is pretty basic:
public $width = 'full';
public function currentUser()
{
return $this->withMeta(['currentUser' => Auth()->user()->id]);
}
public function is_active()
{
return $this->withMeta(['is_active' => Auth()->user()->is_active]);
}
public function information()
{
$information_id = Information::where('user_id', Auth()->user()->id)->first()->id;
return $this->withMeta(['information' => $information_id]);
}
public function component()
{
return 'overview';
}
This card shows up on the dashboard and using the Network tab in debug tools, I can see where the JSON response that goes to Vue when the card's API component is hit. The problem is, the one property I'm trying to add (information) doesn't show up. The full response is:
{
"label": "Dashboard",
"cards": [
{
"width": "full",
"component": "overview",
"prefixComponent": false,
"onlyOnDetail": false,
"currentUser": 780,
"is_active": 1
}
]
}
I've tried running npm install, npm dev and php artisan nova:publish - all are successful, but that JSON isn't picking up the new functions I added to the card's PHP file. For the sake of just trying, I even added these two:
public function testing()
{
return 'test';
}
/**
* Indicates that the analytics should show current visitors.
*
* #return $this
*/
public function currentVisitors()
{
return $this->withMeta(['currentVisitors' => true]);
}
Despite recompiling everything, those functions also don't seem to do anything - the JSON returned stays the same. I'm positive I'm doing something very basic wrong, but for the life of me can't quite figure it out.
Any suggestions?
I personally did create 2 components for Laravel Nova 3.x (one of them using VueJS), and what I did was:
Go inside the component root folder and run npm run prod (or dev ir you are testing)
Then, you must have your component registered as a composer package, so when you load the Component's ServiceProvider, you should have:
public function boot(): void
{
$this->loadViewsFrom(__DIR__.'/../resources/views', 'component-view-name');
Nova::serving(function (ServingNova $event) {
Nova::script('component-view-name', __DIR__.'/../dist/js/field.js');
});
}
That Nova::serving is the one "attaching" or sharing your desired *.js compiled file.
I am not sure about cards. My component was just a new field, not a card, but should be exactly the same.

Laravel Scout (Meilisearch) - Says imported data, but doesn't

I've installed and configured meilisearch + Laravel Scout package.
My Model:
class Post extends Model
{
use Searchable;
}
When I run php artisan scout:import 'App\Models\Post' it returns:
Imported [App\Models\Post] models up to ID: 5
All [App\Models\Post] records have been imported.
But when I check the index, it's empty. Why?
The index is being created, but the data doesn't get imported.
The same configuration of meilisearch and Scout package, works for some other models.
I've just run into this issue myself and came across your question. I don't suppose you're specifying what should be stored in the index are you?
I.e. in your model, have you created a toSearchableArray method like the below...
public function toSearchableArray(): array
{
return [
'name' => $this->name,
];
}
If you have, it turns out that your toSearchableArray method must also return the primary key within the array, otherwise the record does not get indexed.
public function toSearchableArray(): array
{
return [
'id' => $this->getKey(), // this *must* be defined
'name' => $this->name,
];
}
You can try to set:
SCOUT_QUEUE=false
To check that there is no issue with your queues and run the import again.
For index you can try:
php artisan scout:index posts
There is no other issue with your queues and run the import again.
If you have SCOUT_QUEUE=true then please, start your queue using php artisan queue:work --daemon and your data will be start importing.

Laravel Excel - MaatWebsite/Excel's Import Operation raising DB Connection not Configured error

I am using Laravel 6.2 and Maatwebsite/Excel 3.1 here I am trying to import a excel file and willing to get data present in it in an Array format and in the Controller I need to validate some data among them and along with some other fields i need to insert that excel fetched array data to the database. Laravel application has no direct database connection. I am calling an API to insert those fields to the database, but the import operation giving Database connection [] not configured. error. Help me to sort out actual issue from it.
Controller Code
public function importExcelData(Request $request) {
// dd($request->excelFile);
$data = Excel::import(new UsersImport, request()->file('your_file'));
dd($data);
}
UsersImport File
<?php
namespace App\Imports;
use Maatwebsite\Excel\Concerns\ToArray;
class UsersImport implements ToArray
{
public function Array(Array $tables)
{
return $tables;
}
}
?>
After following the error stack appeared in the browser console's network response I got that I can solve this error very easily.
as per Laravel Excel Documentation
enter link description here
we need to run this command in a command prompt
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
after executing this command 'excel.php' file got created under config folder of the application
in this file , we can found this code
'transactions' => [
'handler' => 'db',
],
we need to change it as
'transactions' => [
'handler' => 'null',
],
Now after dumping the import result using dd(), I am seeing those data present in that excel file.

Connection Firebase Firestore to laravel

I have installed laravel 5.8 and firebase project with firestore database.
"name": "laravel/framework",
"version": "v5.8.36",
Firestore database connected to android app. App fetches data good from Firestore. Than I want to create admin panel with laravel for android app and want to integrate laravel with that database.But can not do this.
What I did:
installed php7.2
installed laravel 5.8.*
added php extension gRPC*
Added gRPC as a Composer dependency composer require "grpc/grpc:^v1.1.0" in laravel project
installed composer require google/cloud-firestore
Generated Firebase Admin SDK json file and saved into storage folder in laravel
Added this variable GOOGLE_APPLICATION_CREDENTIALS=/storage/files/progressive-yooung-team-firebase-adminsdk-ax7wi-d2a85ecabc.json (json file which generated firebase admin SDK) in .env file of laravel
installed composer require kreait/firebase-php ^4.35
Created Controller 'VarController' code:
<?php
namespace App\Http\Controllers;
use Kreait\Firebase\Factory;
class VarController extends Controller
{
public function index()
{
print_r("Output: 1");
$factory = new Factory();
print_r("Output: 2");
$firestore = $factory->createFirestore();
print_r("Output: 3");
$database = $firestore->database();
$userRef = $database->collection('users');
$snapshot = $userRef->document('Hus')->snapshot();
if($snapshot->exists()) {
printf('Document data:' . PHP_EOL);
print_r($snapshot->data());
}
print_r("Output: 4");
}
}
Problem is, It does not fetch data from firestore document 'Hus' and its data exists:
users > Hus > name: "Husniddin"
I put print_r("Output: 1"), Output: 2, etc... in order to know where is problem. On screen I see only: Output: 1 Output: 2.
Also don't forget to import these .
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
use \Kreait\Firebase\Database;
use Google\Cloud\Firestore\FirestoreClient;
After that , call the ServiceAccount() inside your function.
$serviceAccount = ServiceAccount::fromJsonFile(__DIR__.'/Firebase.json');
$firebase = (new Factory)
->withServiceAccount($serviceAccount);
$firestore = new FirestoreClient([
'projectId' => 'Your project name',
]);
$collectionReference = $firestore->collection('users');
$documentReference = $collectionReference->document('Search element from document');
$snapshot = $documentReference->snapshot();
change
$snapshot = $userRef->document('Hus')->snapshot();
to
$snapshot = $userRef->document('Hus')->documents();

Laravel - Getting string from a flash

After processing form input, I redirect to a new route with some flash data:
return Redirect::route('work.index')
->with('flash', 'New work entry has been entered');
In the controller specified by work.index, I try to access the data
$flashed = Session:get('flash');
However, instead of a string, I end up with an array with two sub-arrays, old and new
Am I doing something wrong? Am I supposed to do this?
$flashed = Session::get('flash')['new'][0]
Store Data for next request
Session::flash('city', 'New work entry has been entered');
Retrieve Data from last request
$data = Session::get('city');
return Redirect::route('work.index')
->with('data', $data);
My Advice is to use Laracasts/Flash package that helps you to manage Flash messages in an easy way.
Here the GitHub repo: https://github.com/laracasts/flash
Installation
First, pull in the package through Composer.
"require": {
"laracasts/flash": "~1.0"
}
And then, if using Laravel, include the service provider within app/config/app.php.
'providers' => [
'Laracasts\Flash\FlashServiceProvider'
];
And, for convenience, add a facade alias to this same file at the bottom:
'aliases' => [
'Flash' => 'Laracasts\Flash\Flash'
];
And you can use it with:
Flash::info('Message')
Flash::success('Message')
Flash::error('Message')
Flash::warning('Message')
Flash::overlay('Modal Message', 'Modal Title')
Now in your theme you can easly integrate it with:
#include('flash::message')
NB:
Note that this package is optimized for use with Twitter Bootstrap.

Resources