Eloquent 5.5 paginate() not working outside laravel - laravel

I am using Illuminate/database package outside Laravel with my CodeIgniter setup. The initialization is done using Capsule class like this
use Illuminate\Database\Capsule\Manager as CapsuleManager;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
class Capsule extends CapsuleManager
{
public function __construct()
{
parent::__construct();
require_once __DIR__.'/../config/database.php';
$db = (object) $db['default'];
$this->addConnection(array(
'driver' => 'mysql',
'host' => $db->hostname,
'database' => $db->database,
'username' => $db->username,
'password' => $db->password,
'charset' => $db->char_set,
'collation' => $db->dbcollat,
'prefix' => $db->dbprefix,
));
$this->setEventDispatcher(new Dispatcher(new Container));
// Make this Capsule instance available globally via static methods... (optional)
$this->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
$this->bootEloquent();
}
}
I had it running on illuminate/database 5.2. Recently I updated it to illuminate/database 5.5. My Eloquent paginate() has stopped working. The links() method on eloquent collection gives following error.
Call to a member function make() on null
With a stack trace to
return new HtmlString(static::viewFactory()->make($view ?: static::$defaultView, array_merge($data, [
in vendor\illuminate\pagination\LengthAwarePaginator.php on Line 90
Any help in this regard will be appreciated.

Related

BlueFeatherGroup eloquent-filemaker connection problem

I'm using https://github.com/BlueFeatherGroup/eloquent-filemaker in a Laravel Project to get data from a FileMaker database.
I use a MySQL as default connection in my project, but I need a second connection to a FileMaker Server.
I created in database.php config file a new conn:
'filemaker' => [
'driver' => 'filemaker',
'host' => 'myfmhost',
'database' => 'FMdatabase',
'username' => 'FMUser',
'password' => 'MFPass',
'prefix' => '',
'version' => 'vLatest',
'protocol' => 'https',
],
I created a model "tabla" as follows:
use BlueFeather\EloquentFileMaker\Database\Eloquent\FMModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class tabla extends FMModel
{
use HasFactory;
protected $connection= 'filemaker';
protected $fillable = [ ];
protected $layout = 'tabla';
}
In my Controller:
$data = tabla::all();
dd($data);
and I get this error:
Argument 1 passed to
BlueFeather\EloquentFileMaker\Database\Query\FMBaseBuilder::__construct()
must be an instance of
BlueFeather\EloquentFileMaker\Services\FileMakerConnection, instance
of Illuminate\Database\MySqlConnection given
But if I do this in my controller (Query Builder aproach):
$data = FM::connection('filemaker')->layout('tabla')->get();
dd($data);
It works ok!
What am I doing wrong? Is it possible to use filemaker conn when using Eloquent if it's not the default project connection ?
Thanks in advance!!

Issue with laravel 5.8 and mongodb 4.2 connection

My system has 2 xampp's one with PHP 5.6 and the other with PHP 7.3. I have correctly included both the PHP versions in environment variables. I am trying to connect my laravel v5.8 application with mongodb v4.2.
I have used jenssegers/mongodb package in my application. Also I have added MongodbServiceProvider in app.php. I am using Robo 3T for mongodb GUI. Now whenever I try to post data to mongodb collection then I get a authentication failed error.N= Below are the codes that I have used in my application.
Database.php(Config folder)
'mongodb' => [
'driver' => 'mongodb',
'host' => env('MONGO_DB_HOST', 'localhost'),
'port' => env('MONGO_DB_PORT', 27017),
'database' => env('MONGO_DB_DATABASE'),
'username' => env('MONGO_DB_USERNAME'),
'password' => env('MONGO_DB_PASSWORD'),
'options' => []
],
Todo.php(Model)
namespace App;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class Todo extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'todo';
protected $fillable = [
'title', 'desc'
];
}
.env file
MONGO_DB_HOST=127.0.0.1
MONGO_DB_PORT=27017
MONGO_DB_DATABASE=mongocrud
MONGO_DB_USERNAME=
MONGO_DB_PASSWORD=
Overview of Error message:
(1/1) AuthenticationException
Authentication failed.
in Find.php line 299
at Server->executeQuery('mongocrud.todo', object(Query), array('readPreference' => object(ReadPreference)))
in Find.php line 299
at Find->execute(object(Server))
in Collection.php line 624
at Collection->find(array(), array('typeMap' => array('root' => 'array', 'document' => 'array'), 'readPreference' => object(ReadPreference), 'readConcern' => object(ReadConcern)))
First delete vendor folder & replace below 2 line in composer.json:
"jenssegers/mongodb": "^3.5",
"laravel/framework": "5.8.*",
to
"jenssegers/mongodb": "^3.5",
"laravel/framework": "5.7.*",
& update composer
after update the composer clear the config cache.
Make sure you have the MongoDB PHP driver installed. You can find installation instructions at http://php.net/manual/en/mongodb.installation.php

Symfony\Component\Debug\Exception\FatalThrowableError

i'm trying to seed data to database using laravel.
this is my factory model
$factory->define(App\product::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'description' => $faker->description,
'price' =>$faker->price,
'image' => $faker->image,
'value' => $faker->value,
'category_id' => $faker->category_id,
'is_voucher' => $faker->is_voucher,
'voucher_id' => $faker->voucher_id,
];
});
in the producttableseed
public function run()
{
$product = factory(product::class, 10)->create();}
and when i run php artisan db:seed
i get this error
Symfony\Component\Debug\Exception\FatalThrowableError : Argument 1 passed to Illuminate\Database\Eloquent\Factory::{closure}() must be an instance of Faker\Generator\Generator, instance of Faker\Generator given
Do you have the line
use Faker\Generator as Faker;
before you define your factory? This, plus changing first line to this
$factory->define(App\product::class, function (Faker $faker) {
works for me.
Also, be careful with
$product = factory(product::class, 10)->create();}
You are seeding 10 database entries here. Your variable name being singular, looks like you might be up for a surprise when you try working with an array that has multiple entries.

Elequent / Illuminate Laravel Database LIKE operation not working

I'm using the Illuminate database manager from Laravel, which works pretty good except using the LIKE operation for now.
I have tried those options but got nothing:
function initConnection()
{
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => $this->config['host'],
'database' => $this->config['database-name'],
'username' => $this->config['username'],
'password' => $this->config['password'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
]);
$capsule->setAsGlobal();
}
And after initializing I tried:
function searchByName($word)
{
return Capsule::table($table)
->get()
->where('name', 'LIKE', '%'.$word.'%')
->first();
}
echo searchByName('John');
I also tried this option:
Capsule::table('table_name')
->select('SELECT * FROM table_name WHERE table_name.name LIKE "%John%"');
this also failed.
I can't find a documentation for using all the operations in Laravel.
you don't need to define the table in eloquent like this. your model will do that by itself if you follow the name convention or u can set table name in the model by :
class YourModelName extends Model
{
protected $table = 'your_table_name';
}
now query like this :
function searchByName($word) {
return Capsule::where('name', 'LIKE', '%'.$word.'%')->get();
}
this will return multiple value which will match the string.

Transaction doesn't work when instantiating eloquant from a class

I try to use transactions OUTSIDE laravel. it works when I include the db instantiation right in my index file as is:
use Illuminate\Database\Capsule\Manager as Db;
$db = new Db;
$db->addConnection( [
'driver' => Settings::DATABASE_DRIVER,
'host' => Settings::DATABASE_HOST,
'database' => Settings::DATABASE_NAME,
'username' => Settings::DATABASE_USERNAME,
'password' => Settings::DATABASE_PASSWORD,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
] );
# Make this Capsule instance available globally via static methods... (optional)
$db->setAsGlobal();
I can then elsewhere use code like this:
Db::connection()->beginTransaction();
# Create blog post in the database and return its id
$blogPostRecordId = ( new BlogDbModel() )->create( $_POST );
Db::connection()-> rollBack();
it will correctly work and rollback: no row is created in db.
However, If I get an instance of the db from a class, it won't work:
class DbSql
{
/**
* Use the eloquent query builder and orm. Bypass PDO interface.
* #return Capsule
*/
public function db()
{
$capsule = new Capsule;
$capsule->addConnection( [
'driver' => Settings::DATABASE_DRIVER,
'host' => Settings::DATABASE_HOST,
'database' => Settings::DATABASE_NAME,
'username' => Settings::DATABASE_USERNAME,
'password' => Settings::DATABASE_PASSWORD,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => ''
] );
# Make this Capsule instance available globally via static methods... (optional)
$capsule->setAsGlobal();
// Setup the Eloquent ORM... (optional; unless you've used setEventDispatcher())
//$capsule->bootEloquent();
return $capsule;
}
}
and then use
( new DbSql() )->db()->getConnection()->beginTransaction();
# Create blog post in the database and return its id
$blogPostRecordId = ( new BlogDbModel() )->create( $_POST );
( new DbSql() )->db()->getConnection()->rollBack();
it simply won't work and the transaction is ignored. Why does getting the db instance from a class instantiation make the process fail ? I would prefer to use an instance as needed.
Solution: the db instance must be exactly the same everywhere the transaction is needed:
$dbInstance = ( new DbSql() )->db();
Then use $dbInstance wherever the transaction is to be followed:
$dbInstance->connection()->beginTransaction();
as well as with any db eloquent operation. So pass the $dbInstance as needed. The good thing is that it can be anywhere in you code: in your models, controllers....
Then finish:
$dbInstance->connection()->commit();
If the system is not able to reach this last line, nothing will be committed to the db.

Resources