stancl/tenancy Custom Database Name - laravel

I'm using laravel version 6 and also using stancl/tenancy package for dynamic database, but now I have some problem when I type this command "php artisan tenants:create -d my-domain1.com name=my-domain-1 plan=free email=foo#test.local othes=test" to create a new database I got database name like this "ecommerce_db_af0a82cc-aa3f-48dc-8ded-3b70c4ba833b" so I want to custom database name by my own I try to read the documentation of that package but can't understand you can see my database name that I have generated

Maybe that helps:
Change the config/tenancy.php with it:
'prefix' => 'tenant_',
Change your command file with it:
protected $signature = 'tenants:create {tenant?}';
and the handle:
if ($this->argument('tenant')) {
$this->info("Creating Tenant #{$this->argument('tenant')}#")
$tenant = Tenant::create([
'id' => "{$this->argument('tenant')}",
]);
}

Related

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.

Laravel 6 unique return Undefined table or Database [] not configured

Hi I added validation to form:
$rules = [
'name' => 'required|max:255|unique:testcrm.permissions,name'
];
I using PostgreSQL. I have table permissions with column unique 'name'.
I have this same error which is in this theme.
When I have:
unique:permissions,name'
Laravel return error:
Undefined table:...
When I change to:
unique:testcrm.permissions,name
I have error:
Database [testcrm] not configured.
I have:
database name: testid,
schema name: testcrm,
table name: permissions.
'name' => 'required|unique:testcrm.permissions,name|max:255'
This validation should work.
In your .env file though you should have something like this:
DB_CONNECTION=pgsql // define that you are using the pgsql connection
Laravel connection is Mysql by default so make sure you change that
The rest are your credentials. After that run php artisan config:cache so your changes are being cached and loaded in the project.

How to add relations to notifications? And get the related data in json

I am trying to create relations, and when notifications are fetched via $user->unreadNotifications I want to control which fields are shown, and fetch the relations. I cannot figure out where to do this.
I did the following:
php artisan notifications:table
php artisan make:migration add_relations_to_notifications_table
In this new migration I added requester_id.
$table->integer('requester_id')->unsigned()->nullable();
$table->foreign('requester_id')->references('id')->on('users')->onDelete('cascade');
php migrate
php artisan make:notification AnInviteWasRequested
Then in AnInviteWasRequested I removed the toArray and replaced it with toDatabase:
public function toDatabase($notifiable)
{
return [
'requester_id' => Auth::guard('api')->user()->id
];
}
However this does not set the requester_id field, it just put json into the data column that looks like this: {"requester_id":1}.
Is there anyway to get this to update the requester_id field instead of updating data?
And also is it possible somewhere, like a Model file (not in vendor dir) to control which fields are displayed when $user->unreadNotifications is done?
Actually to define which field to show/save, and then you need it to display, you only need to modify the toDatabase method. Example:
public function toDatabase($notifiable)
{
$user = Auth::guard('api')->user();
return [
'requester_id' => $user->id,
'requester_name' => $user->name,
// and more data that you need to show
];
}
So for relational data or any other data, just define it inside this method. Hope it helps. :)

How do I use a package language file?

I'm trying to use a Laravel 4 package language file, but I don't know how to do it.
I created a package with php artisan workbench vendor/package --resources. I then create file workbench/vendor/package/src/lang/en/routes.php.
In that routes file a I have this:
<?php
return [
'foo' => 'bar'
];
Now how do I access that? I tried with Lang::get('routes.foo') and Lang::get('vendor/package::routes.foo') but both fails and just gives me the parameter itself I entered. I'm calling it in my service providers boot method.
Same like you call view and config:
// for lang
Lang::get('package::routes.foo')
// or with shortcut func
trans('package::routes.foo')
// for view
View::make('package::view.name');
// for config
Config::get('package::group.option');
What you need to do is to remove vendor/ but leave package.
You can see more at the Laravel documentation on: package conventions.
====
UPDATE
in laravel 5 you can call view and config like this :
// for view (shorthand)
view('path_to_view', array('data' => 'somedata'));
// for config
config('config.name', 'default');

Resources