Laravel Routes creating clashes with artisan migrate - laravel

Scenario
I have setup Laravel application routes in such a way that, where
Route::group(["domain" => getSubDomain()->sub_domain,"middleware"=>
["guest"]], function () {
//member routes
});
have one helper function getSubDomain()->sub_domain, which is checking sub_domain allowed in companies table. here is the helper function code
$domain = \App\Model\Company::where('sub_domain', request()->getHost())->whereIn("company_type_id", (array)$company_type_id)->first();
return !empty($domain) ? $domain : new \App\Model\Company(
[
"sub_domain" => !in_array(4, $company_type_id) ? env("sub_domain") : "",
"welcome_message" => env("welcome_message"),
"domain_flag" => "assets/images/dev-logo.png"
]);
In short this helper function will check in database about the sub domain is what? and based on valid sub domains routes are created.
Issue
This implementation is having requirement of sub_domain column from table and which is yet run using migration,
So when I am running migration using php artisan migrate it's showing me invalid column sub_domain.
This is creating conflict, that To run the migration routes are creating problem, and to prepare proper routes migration needs to be run.
Earlier the same implementation was using .env variables but that was bit tedious as the support of new domains is required.
Can anybody have solution to run migration without interfering Routes ?

Related

Best way share data across all view in Laravel

I have some data that I want to share across all views. I am using AppServiceProvider's boot method to share data.it is working fine with MySQL however with pgsql when I run composer or PHP artisan commands I am getting the following error,(i do fresh and seed database very often)
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "news" does not exist
it took me a while to understand where is the real issue. I m still unable to understand if it is pgsql related error or something else, bellow is code in AppServiceProvider. if I comment this code everything works fine except where I m using this.
public function boot()
{
$activeClubs = (new TournamentService())->getAllActiveClubs();
$activeNews = (new TournamentService())->getActiveNews();
$activeTournaments = (new TournamentService())->getActiveTournament();
View::share(['activeClubs' => $activeClubs, 'activeTournaments' => $activeTournaments, 'activeNews' => $activeNews]);
}
can you please help me that how can i share data across all views that i don't get this error in future.
you can determine if the app is running from console or not and prevent loading data in console
if ( !app()->runningInConsole() ){
// it's not console.
}
you mentioned you want to load this data in all views but sometimes, its one view that loaded in all of your views e.g. the layout.app view.
if its the case I recommend using view composers instead of View::share() as it will pass the data to that view before render and it will only run if that view is included in the page (so basically it will solve your problem even without app()->runningInConsole() condition required )
view composers doc
simple example:
View::composer('view-name', function ($view) {
$view->with('key', 'value');
});

Laravel call method on any artisan command

Is it possible to set a method that changes a value in my database automatically when I run a php artisan command? What I'm trying to accomplish is change the value of the first row in my "domains" table to suit the url from my .env file automatically whenever I push codes to my live/staging environment. Are there any ways to do this automatically without me manually going into my DB and changing it.
You could setup a Listener for the native event CommandFinished and check if the command is the config:cache.
Event::listen('Illuminate\Console\Events\CommandFinished', function ($event) {
if ($event->command == 'config:cache') {
// Change domains table data using Eloquent or Query Builder
}
});
To learn more about Events, see: https://laravel.com/docs/5.7/events#generating-events-and-listeners

Laravel about Requests

i am new in programing laravel. so here I will not ask about my problems but I am here just want to ask how to use laravel. here I would like to ask how to use:
Determining If an Input Value Is Present in laravel. and what it does?
thanks ^^
Laravel is really a good MVC Framework.
I can suggest you some source from where you can get better understanding of Laravel Framework.
https://laravel.com/docs/5.2
https://laracasts.com/skills/laravel
For simple example - How to use Laravel after installation
Go to path using terminal ex. /var/www/laravel/project
Go to - /var/www/laravel/project/resources/views
Create a directory test and create a file index.php
Create a controller - php artisan make:controller TestController
Create a function testGet() and return view - return view('test.index'); //test is directory and index is file
Create a function testPost(Request $request) and to get data use $data = $request->all(); and then print this data.
Create a model with migration file - php artisan make:model Test --migration
Go to route file - /var/www/laravel/project/app/Http/routes.php
Add line Route::get('/test', 'TestController#testGet');
Add line Route::post('/test', 'TestController#testPost');
Now check GET request to your project http://project/test it will call testGet function.
POST request http://project/test?name=Adam will call testPost function and will print your name.
as said in the comments you better check laracasts! its the laravel school
anyway to anwser your question, its simple
View
<input type="text" name="fname">
Controller
public function doingstuff (Illuminate\Http\Request $request) {
if ($request->has('fname')) {
// a man gotta do what a man gotta do here
}
}
smooth huh? :3

Route is not defined after updating laravel 5.1.8 to 5.1.24

I updated my laravel installation with the composer update and i get this error.
Route [categoryid] not defined
The strange thing is that, before the update, it worked just fine.
My routes.php file looks like this:
Route::group(['prefix'=>'category'], function () {
Route::get('mobilephones', [
'as'=>'mobilephones',
'uses'=>'PhoneController#getShow'
]);
Route::get('{categoryid}', [
'as'=>'categoryid',
'uses'=>'CategoryController#one'
]);
Route::get('{categoryid}', [
'as'=>'computerscategoryid',
'uses'=>'CategoryController#one'
]);
});
and i am calling the route with this html code
<li>Argument</li>
Everything used to work so is anyone aware of a change in the Group route files after 5.1.8?
As a general rule, always run php artisan route:list to see the compiled list of your routes.
You have two routes that do the exact same thing:
Route::get('{categoryid}', [
'as'=>'categoryid',
'uses'=>'CategoryController#one'
]);
Route::get('{categoryid}', [
'as'=>'computerscategoryid',
'uses'=>'CategoryController#one'
]);
They accept the same argument. They get sent to the same controller action. The only difference is that they have different route names. One of them (the second one) is most likely overriding the other. I would suggest removing the second one - computerscategoryid - because I can't see a purpose in having both of them.

Laravel 4.2 use Sentinel with Jenssegers MongoDB

Is it possible to use Cartalyst/Sentinel with Jenssegers/MongoDB on Laravel 4.2?
I'm currently using Sentry, but I want to try Sentinel with new features.
After installation, I tried this:
$user = Sentinel::register([
'email' => $email,
'password' => $password,
]);
But I've got the following error:
Argument 2 passed to Illuminate\Database\Query\Builder::__construct()
must be an instance of Illuminate\Database\Query\Grammars\Grammar,
null given, called in
/home/vagrant/shared/muzza/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
on line 1853 and defined
Sentinel doesn't have integration with Jenssegers MongoDB lib.
Dan Syme commented on 2 Jul:
You would have to write the implementations for the different models
used throughout Sentinel.
Then switch out the models for the different entities on the config
file with your own implementations, Sentinel should take care of
everything else.
Maxime-BHA commented on 3 Jul:
As #jenssegers already did for Sentry package, you have to create your
own Models extending the originals models from Sentinel and bind the
attributes defined in those models for relationships/persistances to
the new models etc.. like static $usersModel =
'Cartalyst\Sentinel\Users\EloquentUser'; and also change the config
file.
See exemple for Sentry there :
https://github.com/jenssegers/laravel-mongodb-sentry
I think it's the exact same procedure.
More information you can find here.

Resources