Use Illuminate database - php

I want to use Illuminate database(https://github.com/illuminate/database). Not with Laravel, use only in my php file.
I do
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'database',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);
But it seems not working, and don't show any error message. Do I need to require any file? The illuminate directory is in the same directory with my php file.
EDIT:
I can use query now. Like this
$users = Capsule::table('users')->where('votes', '>', 100)->get();
I don't know how to use model.
User.php
class User extends Illuminate\Database\Eloquent\Model {
}
My php file
require 'vendor/autoload.php';
require 'User.php';
$users = User::where('status', '=', 1)->get();
Got error
Fatal error: Call to a member function connection() on a non-object in /Users/someone/repos/test/vendor/illuminate/database/Illuminate/Database/Eloquent/Model.php on line 2472
SOLVED:
Everything works fine. Use #majid8911 example https://github.com/mattstauffer/IlluminateNonLaravel
Thank you everyone.

take a look at here I successfully did the same with this tutorial:
https://github.com/mattstauffer/IlluminateNonLaravel

Related

Laravel: Jasper reports in laravel lumen5.6

I already implemented jasper reports with laravel and it works fine for me. Now i shifted to laravel lumen for api building so i try to integrate jasper as same as i integrate in my laravel projects but in laravel lumen it throws some error as below:-
Call to undefined method Laravel\Lumen\Application::booting()
below is my connection code
class_alias(JasperPHP\JasperPHPServiceProvider::class,'JasperPHP');
$app->withFacades(); $app->withEloquent();
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
$app->register(JasperPHP\JasperPHPServiceProvider::class);
Please share your thoughts, Thanks in advance
I finally come up with a solution and it works fine with laravel lumen 5.6 below are the steps:-
1)Install JasperReports 6 library by below command
composer require cossou/jasperphp
In bootstrap/app.php uncomment this line $app->withFacades(); and add below code
$app->singleton('jasperphp', function ($app) {
return new JasperPHP;
});
$app->alias('JasperPHP\JasperPHPServiceProvider\JasperPHP', 'JasperPHP');
Controller part Changes
namespace App\Http\Controllers;
use JasperPHP\JasperPHP as JasperPHP;
use Illuminate\Http\Request;
//dd(__DIR__ . '/../../vendor/cossou/jasperphp/examples/hello_world.jasper');
class ReportController extends Controller {
public function generateReport() {
//JasperPHP::compile(base_path('/vendor/cossou/jasperphp/examples/hello_world.jrxml'))->execute();
$jasper = new JasperPHP;
$filename = 'gau';
$output = base_path('//public/reports/' . $filename);
$jasper->process(
base_path('/vendor/cossou/jasperphp/examples/LaravelIreporTest.jasper'),
$output,
array("pdf"),
array("test" => "Tax Invoice"),
array(
'driver' => 'mysql',
'username' => 'username',
'password' => 'password',
'host' => 'localhost',
'database' => 'database name',
'port' => '3306',
)
)->execute();
}
}

Fatal error: Uncaught Error: Class 'maimana\App' not found -- slim3

i was following alex's how to build a shopping cart lesson and everything's working fine. but then i dont know what im doing wrong so i get this error :
Fatal error: Uncaught Error: Class 'maimana\App' not found in /Applications/MAMP/htdocs/maimana/bootstrap/app.php:13 Stack trace: #0 /Applications/MAMP/htdocs/maimana/public/index.php(3): require() #1 {main} thrown in /Applications/MAMP/htdocs/maimana/bootstrap/app.php on line 13
bootsrap/app.php :
<?php
use Respect\Validation\Validator as v;
use maimana\App as MyApp;
use Slim\Views\Twig;
use Illuminate\Database\Capsule\Manager as Capsule;
session_start();
require __DIR__ . '/../vendor/autoload.php';
$app = new MyApp;
$container = $app->getContainer();
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'maimana',
'username' => 'rdp46',
'password' => 'littlelion4696',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
require __DIR__ . '/../app/routes.php';
Myapp/App.php :
namespace maimana;
use DI\ContainerBuilder;
use DI\Bridge\Slim\App as DiBridge;
class App extends DiBridge{
protected function configureContainer(ContainerBuilder $builder)
{
$builder->addDefinitions([
'settings.displayErrorDetails' => true,
]);
$builder->addDefinitions(__DIR__ . '/container.php');
}
}
anyone know what's going on?
Rename the Myapp directory to maimana (note case) and then update your composer.json to autoload the maimana namespace.
i.e. ensure that your composer.json has:
"autoload": {
"psr-4": {
"maimana\\": "maimana/"
}
}
This assumes that the maimana directoryis in the root of your project where the composer.json file is. Once you have changed composer.json, you need to run composer dumpautoload for the changes to take effect.
This is required because there is a one to one mapping between directory name that the PHP file is in and the namespace for the class in that PHP file. As the namespace in your App.php is maimana, the directory needs to be maimana.

PDO Exception:SQLSTATE[HY000] [2006] MySQL server has gone away with xampp

I'm trying to connect to the database and fetching some records but I'm getting error:
Error i'm getting
I'm using Slim PHP framework and slim-twig to render views so my code for the connection file is:
<?php
use Payment\App;
use Illuminate\Database\Capsule\Manager as Capsule;
session_start();
require __DIR__ . '/../vendor/autoload.php';
$app = new App;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost:8080',
'database' => 'payment',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
require __DIR__ . '/../app/routes.php';
?>
I had also configured my php.ini file properties like max_execution_time & max_allowed_packets but it didn't work and i had also checked that no loop is causing this problem because it takes approx 3 min to show me this error,in this 3 min the it loads.so please anyone can tell me where i'm doing in my code?
Probably caused by your packets. The following settings should help:
Run this in your MySQL terminal.
set global max_allowed_packet=104857600

Defining configuration values under specific namespaces?

I'm writing a framework, and have just added namespaces. I've had no trouble converting everything, save for configuration values.
I can define constants easily, so single values are not a problem...but what about an array of values?
Take this array, for instance, which is the configuration array to add a connection to Capsule...
[
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'DB_NAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => ''
]
I have been using a file that looks like this for database configuration...
<?php
namespace BareBones;
use Illuminate\Database\Capsule\Manager as Capsule;
$BareBonesCapsule = new Capsule;
$BareBonesCapsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'DB_NAME',
'username' => 'USERNAME',
'password' => 'PASSWORD',
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => ''
]);
$BareBonesCapsule->setAsGlobal();
$BareBonesCapsule->bootEloquent();
use Illuminate\Database\Schema\Blueprint as Blueprint;
use Illuminate\Database\Eloquent\Model as Eloquent;
I'm going to start booting Eloquent from my main App class. This will prevent a global variable from doing it. While it is unlikely that $BareBonesCapsule is going to be used, I would still like to keep my framework clean and keep everything in it's namespace.
I could declare a bunch of constants in the configurations file...
<?php
namesapce BareBones;
define("driver", "mysql");
define("host", "localhost");
define("database", "DB_NAME");
/* etc... */
This doesn't seem very clean, and I'm assuming there is a better way to do this. How do other frameworks handle this issue, and what alternative means of configuration do I have while maintaining a separation from the global namespace?
You can use .ini files and break them into sections like so:
; This is a sample configuration file
; Comments start with ';', as in php.ini
[first_section]
one = 1
five = 5
animal = BIRD
[second_section]
path = "/usr/local/bin"
URL = "http://www.example.com/~username"
[third_section]
phpversion[] = "5.0"
phpversion[] = "5.1"
phpversion[] = "5.2"
phpversion[] = "5.3"
urls[svn] = "http://svn.php.net"
urls[git] = "http://git.php.net"
From: http://php.net/manual/en/function.parse-ini-file.php
Symfony supports XML, PHP and YML and allows you to write your own config decoder.

php undefined property of an object

I'm sure this is more simple than I'm making it.
The error that I get...
Notice (8): Undefined property: Cake\Database\Connection::$config [... line 73]
The code on line 73...
Line 72: debug($conn);
Line 73: debug($conn->config);
Here's what $conn equals
$conn = object(Cake\Database\Connection) {
'config' => [
'password' => '*****',
'username' => '*****',
'host' => '*****',
'database' => '*****',
'driver' => 'Cake\Database\Driver\Postgres',
'persistent' => false,
'encoding' => 'utf8',
],
}
Anyone know why $conn->config isn't the right syntax?
There is no public property named $config only a protected $_config in the database connection class in Cake (http://api.cakephp.org/3.2/source-class-Cake.Database.Connection.html).
With the the database connection in Cake you must use the public function config() to get the configuration array.
$config = $conn->config();
I don't know what debug() does, but it's not returning a detailed representation of the properties of the object as would var_dump or as you could get from reflection. Don't rely on it to learn the interface of an object/class.

Categories