Seeders in this scenario - laravel

I have an email configured in the services.admin.key that has the email of the admin of the app.
The app only has one admin. Do you know the best approach so that someone who has access to the code from Git could run a command to configure the user? For example, if its a table with three columns:
name: admin
email: the email configured in`services.admin.key`
password: sometestpass
How can I allow someone who has access to the project to run some command to create one user with these details? Do you know what the best approach for this is? I doubt how to approach this properly if it should be with a seeder or another approach?
For example, I have this method on the user model that I use in some views to check if the current user is an admin.
public function isAdministrator ()
{
if ($this->email == config('services.admin.key'))
{
return true;
}
return false;
}

If you talk about fresh project on local database, it would be nice to get ready seeder for admin user.(not remote database). let's say I got access to git repo I could clone project, and run composer install for dependencies and than PHP command php artisan migrate --seed it would be good to have seeder ready for creating admin. To make seeder you need just php artisan make:seed AdminSeeder
and after write in AdminSeeder file something like:
User::create(['name' => 'admin', 'email' => config('services.admin.key')),'password' => bcrypt('sometestpass'),]);
than in Database/seeders/DatabaseSeeder.php write $this->call(AdminSeeder::class);
it's all. now when someone get fresh project it does 3 commands to make everything ready.
composer install
php artisan migrate --seed
php artisan key:generate
hope I understood right way what you needed.

Related

Is it possibile using Artisan command on web server?

I would like to ask if is possible using following command on webserver.
public function addCache() {
\Artisan::call('config:cache');
}
Yes it is possible, connect to your web server with terminal, most of the time you will need to ssh into it, and then navigate to the project root folder, then run the php artisan command as what you typically do
You can create a route with artisan call
Route::get('/clear', function () {
Artisan::call('config:clear');
});
If you don't have terminal on your host or server, you can define a route and define this artisan call inside that.
Route::get('cache-config',function(){
\Artisan::call('config:cache');
return back();
});
Or you can use this package for this artisan commands and some other useful commands.

Why Will Voyager Not Show my Admin Dashboard When Live? | Forge, Voyager & Laravel

I am creating a Laravel eCommerce application. I am following this tutorial on youtube: https://www.youtube.com/watch?v=L3EbWJmmyjo&list=PLEhEHUEU3x5oPTli631ZX9cxl6cU_sDaR&index=18
For the Admin section of the website, I am using Voyager. On my localhost, everything seems to be working perfectly:
The landing page (http://localhost:8000):
When I then enter the link: http://localhost:8000/admin it takes me to the http://localhost:8000/admin/login:
I then enter these credentials (images show database as well). As you can see the credentials match and when I press login I'm taken to the dashboard:
All the screenshots so far are on my localhost. When I am trying to install Voyager on my live website it stops working! This is my live web address: https://janinevalerieaesthetics.com
I am hosting using Forge and Envoyer. And they are linking using my GitLab account: https://gitlab.com/rossi99/salonwebsite
I connect to my Forge server using ssh through my terminal using this link: ssh forge#janinevalerieaesthetics.com
And then I cd all the way into the current directory (the one that hold all my folders including the 'vendor' file and 'artisan') and then I use the php artisan commands to install the voyager package (https://voyager-docs.devdojo.com/getting-started/installation) :
Changing the .env file:
Installation Steps:
APP_URL=http://localhost
My .env:
APP_URL=https://janinevalerieaesthetics.com
Installation Steps:
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
My .env:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=forge
DB_USERNAME=forge
DB_PASSWORD="**************"
I thne run this command from my terminal forge#salonWebsite:~/janinevalerieaesthetics.com/current$ :
php artisan voyager:install
I then make a user from the same terminal using this command :
php artisan voyager:admin your#email.com --create
Here is my live database tables:
And here is the roles table ( you can see that user with ID 1 is the admin):
But when I navigate to https://janinevalerieaesthetics.com/admin/login it shows the login screen:
But when I log in with the correct credentials, instead of login me in and showing the admin dashboard, it logs me in and takes me to the landing page and when I try to manually get to the log in it just redirects me to the landing page:
I found the answer for anyone else having this issue. When I inspected my 'seeds' directory, I did not have a 'DatabaseSeeder.php' file. I then created this by simply right-clicking and then creating DatabaseSeeder.php in mt seeds directory, I then added this code into it:
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$this->call(VoyagerDatabaseSeeder::class);
}
}
I then had to back into my terminal and run the following commands:
ssh forge#janinevalerieaesthetics.com
cd janinevalerieaesthetics.com
(this will be your web address, to find this run command "ll -la" after ssh command)
cd current
cd database
cd seeds
vim DatabaseSeeder.php
(this will allow you to see that the most up to date seed it being used, ':q' to exit)
cd ../
(to go back to 'database' directory)
cd ../
(to go back to 'current' directory)
php artisan db:seed
(this will popuate all the roles and tables that voyager needs)
(you might see a message like this, type yes)
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command? (yes/no) [no]:
> yes
This will then populate the database of your live site. I then tried to log in with an existing user and it didn't work. I had to create a new admin user using this command in the '/current$' directory:
php artisan voyager:admin admin#admin.com --create
<replace admin#admin.com with your own user email>
You will then be prompted for a username and password for your admin and once these are entered you can then navigate to: https://yourURL.com/admin/login and enter the details of your user and you will be navigated to you the dashboard!

How to migrate tables on a running site for laravel?

Using laravel how to migrate table(if there are changes) on a running site?
We use CMD if working offline. My question is simple if I have a site running fine already (online) then how to migrate...we can't run CMD there, right?
one way is using Artisan command in routes like this:
Route::get('/migrate', function(){
\Artisan::call('migrate');
dd('migrated!');
});
and then call this route.
remember to REMOVE this route after MIGRATING...!
if your server is linux you can use ssh to access command environment.
If your personal system is Windows you can install putty to open ssh.
how it works is in this link :
https://mediatemple.net/community/products/dv/204404604/using-ssh-in-putty-(windows)
how to access ssh in mac system is in this link
https://www.servermania.com/kb/articles/ssh-mac/
after access ssh you most first install composer then locate your project folder and now you can call all php artisan command like
php artisan migrate
import Artisan at web.php
use Illuminate\Support\Facades\Artisan;
you can write migrate route like this
Route::get('migrate',function(){
Artisan::call('migrate');
});
you can write migrate:rollback route like this
Route::get('rollback',function(){
Artisan::call('migrate:rollback');
});
you can write routes & view clearing route like this
Route::get('vr-clear',function(){
Artisan::call('route:clear');
Artisan::call('view:clear');
});
Basically i will suggest you too use like this reboot route for clearing cache and route clear after deploying the Laravel project to live.
Because sometimes it can be show some error message.
Route::get('reboot',function(){
Artisan::call('view:clear');
Artisan::call('route:clear');
Artisan::call('config:clear');
Artisan::call('cache:clear');
Artisan::call('key:generate');
});
The answer is also simple. You're still going to use CLI or CMD while your site is online. If you use cPanel, go to Advanced tab and select terminal. Inside your terminal use cd to change drive. Default drive might probably be public_html. cd public_html, cd <your_project_folder>php artisan migrate.
This will process migration while your application is running.

How to run Laravel artisan on a live server without shell_exec?

My new project is based on Laravel framework, and developers need to run php artisan commands to do their requirements. On the other hand, server configuration disabled shell_exec for security purposes.
What should I do for this case? Is there any secure way for developers considering server security issues?
There are three ways to run php artisan command.Below i have mention all of them :
1) Use Terminal which is on server.Through that you can execute the php artisan command.
2) Use "putty" software to access the server.In that you can connect putty with server.For connection to the server you require "SSH credential" which you can create on server.
3) Using route you can execute artisan command.But for that every time you need to add/modify route and execute that in the browser so that artisan command will execute.Below i have given an example of it.You just have to put it in the routes file:
Route::get('command', function () {
/* php artisan migrate */
\Artisan::call('migrate');
dd("Done");
});
The above route you need to call through browser.This route will execute command of "php artisan migrate".

How to automatically run the migrations in a Laravel package?

I just installed Cartalyst's Sentry 2 in a Laravel 4 application but I found out that I have to run that package's migrations separately by specifying --package=cartalyst/sentry, which makes automatic deployment impossible.
Is there a way to run php artisan migrate and have it run Sentry's migrations as well?
After Laravel 5 there is a better way to solve this:
Create your migrations in package's
/database/migrations folder
.
After that, in package's service provider create a boot method, referencing the migrations folder
public function boot()
{
$this->loadMigrationsFrom(__DIR__ .
'/database/migrations');
}
Now, on the top level folder (the main app, which required this package), you can run the default migrate command ( php artisan migrate ) and it will automatically find the packages migrations througth the loadMigrationsFrom method
ANSWER COPIED FROM:
http://voerro.com/en/tutorials/r/developing-and-distributing-laravel-5-packages/3
What I usually do in a scenario like this is publish the package migrations though the command:
php artisan migrate:publish vendor/package
This copies the migration files from any given package to your migrations folder.
I created a composer script to replace php artisan migrate ...... The script runs my migrations and the vendor's migrations everything at once.
My composer scripts is
"scripts": {
"migrate": [
"php artisan migrate --env=$LARAVEL_ENV",
"php artisan migrate --package=\"cartalyst/sentry\" --env=$LARAVEL_ENV",
"php artisan migrate --package=\"mrjuliuss/syntara\" --env=$LARAVEL_ENV",
"php artisan migrate --package=\"filmoteca/static-pages\" --env=$LARAVEL_ENV"
]
}
Then you can run the migration with LARAVEL_ENV=prod composer run-script migrate
To pass parameter to the script I use environment variables. In the previous example I set the environment variable LARAVEL_ENV to prod so the migration use the production database connection.
You can always create a alias in your local machine to short the command. For example alias migrate="LARAVEL_ENV=local composer run-script migrate"
I think this approach is good because when you are going to add a new package to your composer.json and this package has a migration you add the package and the package's migration in the same file. So, you do not forget add/remove the migration of a package.
This is a complete composer.json with the script
The 3rd party package must declare a Service Provider, that declare migrations.
As you can see at https://github.com/laravel/passport/blob/9.x/src/PassportServiceProvider.php#L80:
protected function registerMigrations()
{
if (Passport::$runsMigrations && ! config('passport.client_uuids')) {
return $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}
}

Resources