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!!
Related
I'm trying to implement a full access log in a Laravel 5.8 project and that's what I've come up so far: a table named access_logs in a secondary database so than non-relevant log data won't make the db bulky during backup, and custom primary key with no AUTO_INCREMET so can be purged old data without worrying about resetting incremental id while new data is being created. the model is following:
class AccessLog extends Model
{
public $timestamps = true;
public $incrementing = false; // custom id, purgeable
protected $connection= 'mysql2';
protected $fillable = [
'id', // custom id, purgeable
'auth_guard',
'auth_id',
'url',
'method',
'referer',
'request',
'session',
'additional_data',
'ip',
];
}
and the DB writing part in the middleware is following:
$logData = [
'id' => join('_', [
time(),
Auth::user()->id ?? 0,
rand(100000, 999999),
]),
'ip' => json_encode(request()->ips() ?? ''),
'auth_guard' => auth()->getDefaultDriver() ?? null,
'auth_id' => auth()->user()->id ?? null,
'url' => url()->current(),
'method' => request()->method(),
'referer' => url()->previous(),
'request' => json_encode([
$this->removeKeys(request()->all(), [
'password',
]),
$_FILES ?? [],
request()->headers->all(),
]),
'session' => json_encode(session()->all()),
];
AccessLog::create($logData);
the middleware is registered in the kernel file under protected $middlewareGroups['web'].
now my question is I'm trying to log form validation data (ErrorBag?) as well as flash messages, but it seems those do not exists or saved with the session data (session()->all()), how to include/save those with the log?
In my Laravel application, I am trying to send mail notification based on the company_id of the logged in user:
I have this:
$mail=DB::table('mail_settings')->first();
$config = array(
'driver' => $mail->driver,
'host' => $mail->host,
'port' => $mail->port,
'from' => array('address' => $mail->from_address, 'name' => $mail->from_name),
'encryption' => $mail->encryption,
'username' => $mail->username,
'password' => $mail->password,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false
);
Config::set('mail',$config);
Models
class Company extends Model
{
protected $table = 'companies';
protected $fillable = [
'id',
'organization_name'
];
}
class User extends Authenticatable
{
protected $fillable = [
'name',
'company_id',
'email',
];
}
Is there any way to override default mail configuration (in app/config/mail.php) on-the-fly (e.g. configuration is stored in database) before mailer transport is created?
Thanks
Is there any way to recreate laravel swiftmailer transport so it can pick up updated config values?
The Mailer class is created in the Illuminate\Mail\MailManager class's resolve() method. If you want to dynamically create a mailer, you need to adapt this function in your Controller to use your $config array and return a Mailer from which you could chain the usual methods.
protected function resolve($name)
{
$config = $this->getConfig($name);
if (is_null($config)) {
throw new InvalidArgumentException("Mailer [{$name}] is not defined.");
}
// Once we have created the mailer instance we will set a container instance
// on the mailer. This allows us to resolve mailer classes via containers
// for maximum testability on said classes instead of passing Closures.
$mailer = new Mailer(
$name,
$this->app['view'],
$this->createSwiftMailer($config),
$this->app['events']
);
if ($this->app->bound('queue')) {
$mailer->setQueue($this->app['queue']);
}
// Next we will set all of the global addresses on this mailer, which allows
// for easy unification of all "from" addresses as well as easy debugging
// of sent messages since these will be sent to a single email address.
foreach (['from', 'reply_to', 'to', 'return_path'] as $type) {
$this->setGlobalAddress($mailer, $config, $type);
}
return $mailer;
}
I am getting error on send Location Data To Database Using Laravel GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31'),
Error:
Object of class Torann\GeoIP\Location could not be converted to string
This Is My Auth LoginController. How to Insert GeoIP Loacation data into database. Please Help me
If i remove this code 'current_location' => GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31'), i am no longer getting this error, every data inserted into database but i add this code i am getting this error
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Jenssegers\Agent\Agent;
use Carbon\Carbon;
use App\User;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Closure;
use GeoIP;
use Location;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
function authenticated(Request $request, $user)
{
// Chrome, IE, Safari, Firefox, ...
$agent = new Agent();
$browser = $agent->browser();
// Ubuntu, Windows, OS X, ...
$platform = $agent->platform();
$user->update([
'last_signin' => Carbon::now()->toDateTimeString(),
'ip_address' => $request->getClientIp(),
'browser_login' => $agent->browser(),
'browser_version' => $agent->version($browser),
'device_login' => $agent->platform(),
'device_version' => $agent->version($platform),
'current_location' => GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31'),
'language' => $agent->languages(),
'root' => $agent->robot(),
'https' => $request->server('HTTP_USER_AGENT'),
]);
}
/**
* Where to redirect users after login.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest',['except'=>['logout', 'userLogout', 'profile']]);
}
public function userLogout()
{
Auth::guard('web')->logout();
return redirect('/');
}
}
Auth Route :
//User Auth Route Function
Auth::routes();
This is happending because GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31') returns an instance of Torann\GeoIP\Location and you are trying to save it as a String.
Checking the documentation of this object it has this shape:
\Torann\GeoIP\Location {
#attributes:array [
'ip' => '232.223.11.11',
'iso_code' => 'US',
'country' => 'United States',
'city' => 'New Haven',
'state' => 'CT',
'state_name' => 'Connecticut',
'postal_code' => '06510',
'lat' => 41.28,
'lon' => -72.88,
'timezone' => 'America/New_York',
'continent' => 'NA',
'currency' => 'USD',
'default' => false,
]
}
You have to choose a way to represent this location as a String, a possible way can be to save the latitude and the longitude separately.
If you need to use only one column at the DB, you can check some GeoHashing implementations skthon/geogash.
You Might be trying to use getLocation method from wrong instance.
1.) Try as below way :
"use Torann\GeoIP\GeoIPFacade as GeoIP"
$location = GeoIP::getLocation();
2.) Or try as Geoip package documentation suggest here (http://lyften.com/projects/laravel-geoip/doc/methods.html)
from this instance \Torann\GeoIP\GeoIP and then use geoip()->getLocation('27.974.399.65');
This seems to be an issue in the current_location field and how it is typed in your database. From what I read, I guess your field is defined a string, and when trying to save your record to the database, it fails since the data you're trying to save is an Location object.
I would recommend changing your current_location column in your database to make it a json type.
Then you'd be able to insert your data as:
$user->update([
'last_signin' => Carbon::now()->toDateTimeString(),
'ip_address' => $request->getClientIp(),
'browser_login' => $agent->browser(),
'browser_version' => $agent->version($browser),
'device_login' => $agent->platform(),
'device_version' => $agent->version($platform),
'current_location' => json_encode(GeoIP::getLocation('2405:204:970a:d9b3:10a3:5280:9064:3f31')->toArray()),
'language' => $agent->languages(),
'root' => $agent->robot(),
'https' => $request->server('HTTP_USER_AGENT'),
]);
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.
I've included Solarium as required on composer.json, did composer install and everything went fine.
However, when I try to create a client stance, I get an error:
Class 'Solarium\Client' not found
The code I'm using goes like this:
public function __construct()
{
$config = array(
'endpoint' => array(
'localhost' => array(
'host' => '127.0.0.1',
'port' => 8983,
'path' => '/solr/my_solr_instance',
)
)
);
$this->client = new \Solarium\Client($config);
}
Any ideas on how to fix this?
After installing solarium in laravel ..
create a file solr.php in config
return [
'endpoint' => [
'Collection' => [
'host' => '192.168.0.1',
'port' => '8983',
'path' => '/solr',
'core' => 'collection1'
],
]
];
IN your controller construction initiate client object
// create a client instance
$this->client = new \Solarium\Client();
$this->endpoint = $this->client->createEndpoint(Config::get('solr.endpoint.Collection'));
In search function
$query = $this->client->createSelect();
$query->setQuery("*:*");
$resultset = $this->client->select($query, $this->endpoint);
I have a new approach for this initiate client from the controller.
I have found this solution here as below it is not good to post complete code.
https://universaldetails.com/details/how-to-use-php-solarium-in-a-laravel-project
I have tried it and tested also so will help you.