How to migrate tables on a running site for laravel? - 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.

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.

Symbolic link not working in Laravel and image not loading

My problem is that when I upload the archim with my site to the hosting and then unpack it there, I lose the symbolic link and the images stop working. How can this be fixed? Everything is fine on the local computer, and I use php artisan storage:link, but there is no terminal on the hosting.
If you have no ssh/terminal access, then create symbolic link with Route, run it once & delete it.
route/web.php :
Route::get('/sym', function () {
Artisan::call('storage:link');
});
Hope this helps
Well, if you have ssh access to the hosting, execute the command.
If you don't have ssh access, like basic hosting with cpanel, you need to configure a cronjob to execute the command. Example:
* * * * * /path/to/php /home/path/to/project/php artisan storage:link

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".

Xampp localhost not working

I am running laravel on xampp and I have problem with accessing pages,
http://localhost/laravel/public/ I get a login page which is good
however when I go for example http://localhost/laravel/public/smokeyard
I get 404 error with The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
Route:
Route::get('smokeyard', 'GuzzleController#smokeyard');
Controller:
function smokeyard(){
return view('smokeyard');
}
All my views are located in resources folder.
if you want to access laravel project without running artisan serve, you need to change few settings,
copy the .htaccess file from the public folder and paste it in the root folder of your application
rename the server.php in the root directory to index.php
now go to localhost/your_project_name/smokeyard for the url you want to check
hope this helps
Run php artisan serve and access your server at http://localhost:8000. Your routes should work fine then.
You can't access your laravel application routes straight away through the folder structure with xampp. You need to setup the web root to point to the public folder and access localhost or run the laravel server and use it. This is because the url rewriting would fail when you access the routes in laravel the way you do.
I tried this in MX Linux 21.xx. My xampp got messed up after installing larvel on it.
check MySQL status with command
mysql service status
stop the service using the command
service mysql stop
you will be prompted to provide your password, Enter password.
start xampp using command
sudo /opt/lampp/lampp start
All should be back to normal

Lumen not working out of the box

Just installed Lumen framework.
hit the link http://localhost/lumen/public/ in my browser and got this following error, anyone got any idea about it?
Traced it back to the app.php file in bootstrap folder.
if You want to access lumen project without "php artisan serve"
$app->run(); replace with
$request = Illuminate\Http\Request::capture();
$app->run($request);
from this path yourlumenproject/public/index.php
Open your terminal in the root folder run the following command php artisan serve.
Lumen development server started on http://localhost:8000/
if you want to serve your app in local development you can do this :
php -S localhost:8000 -t public/
and it will serve in localhost in port 8000. hope this help.
Note : I'm using Laravel Framework version Lumen (5.2.4) (Laravel Components 5.2.*)
At the moment Lumen only runs in the domain root.
(I've submitted a PR that fixes this but it has yet to be merged)
You have to create a Virtual Host on your local webserver and point the document root of that to the public directory. After that you can access your app with something like: http://lumen.dev.
Guide for Virtual Hosts with nginx
Guide for Virtual Hosts with Apache
A simple alternative to setting this up manually is Laravel Homestead. It is an official Vagrant box made for Laravel, that allows you to easily get your development environment up and running.

Resources