Custom Variable in AppServiceProvider Laravel 5.5? - laravel

I want use a variable from my database to set a folder for custom views.
"class AppServiceProvider extends ServiceProvider"
$ActiveProject = ThemeConfig::where('module_type',"project")->where('active',"1")->first()->file;
After this, I get an active project name (like Nshop), and I want to set it in:
public function register()
{
$this->app['view']->addNamespace('Projects', base_path() . '/Projects/'.$ActiveProject.'/Views');
}
But I get an error.
How can I accomplish this task?

Using ORM Models in the AppServiceProvider doesn't work. This file is part of the boot process of Laravel where your models are not loaded yet. But you can rely on functions that are part of the Laravel core concept.
$ActiveProject = ThemeConfig::where('module_type',"project")->where('active',"1")->first()->file;
Becomes
$ActiveProject = \DB::table('theme_configs')->where('module_type',"project")->where('active',"1")->first()->file;

Related

Laravel adding Gate with vendor:publish

So I am developing a composer package, that adds several of my reusable code to a fresh Laravel project. So far I've managed to add core translation files and some models, routes in my service provider with $this->publishes() and $this->loadRoutesFrom() in my boot() method.
Now I want to add Gates to that package, but I'm stuck. Somehow I should register these in the project's AuthServiceProvider on run. Would be very nice if anyone could give me some advice how to perform this task.
If you want to register policies, there is no need to use the AuthServiceProvider, you can simply use Illuminate\Support\Facades\Gate::policy($key, $value).
You can do that in your own ServiceProvider of your package. If you want to define abilities, you can add a boot method like this:
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
// ...
public function boot(GateContract $gate)
{
$gate->define('update-post', function ($user, $post) {
return $user->id === $post->user_id;
});
}
This will resolve the gate instance for you and allow you to define abilities. It's important to use the boot method, since this way you can be sure every service is already registered.

Laravel 5 Can't Use User Class

I'm new to Laravel. I just installed Laravel 5 and have been going through some video tutorials in order to learn how it works.
I want to use the User class as it seems like it is very useful. In my web.php (routes) file, I have:
use app\Models\User;
Route::get('/users', function (){
$users = User::all(); //select * from users
return $users;
});
I get the following error upon loading that route in my browser:
I have a User.php file placed in my app/Models/ directory, and also changed in my auth.php file for it to reference this.
There are countless questions like this online and I try the fixes but I can't seem to get it to work. Any ideas?
Thanks!
Remove Models from use statement. It's not directory... Model is in namespace App so use:
use App\User;
Namespace and Directory is completely different things...
Or if you want to use in directory app/Models go to User.php model file and change namespace from App to App\Models

Lumen: Generate Model validation rules

Artisan generator seems over-sophisticated, It generates a class extended from Model class!!!
Is there any way to generate model validation rules in a lumen model automatically (based on column definition of a mysql table)?
What about column names?
I am the author of lumen-generators, A collection of generators for Lumen and Laravel 5.
This package contains a Model Generator which supports generating validation rules.
Installation
Add the generators package to your composer.json by running the command:
composer require wn/lumen-generators
Then add the service provider in the file app/Providers/AppServiceProvider.php like the following:
public function register()
{
if ($this->app->environment() == 'local') {
$this->app->register('Wn\Generators\CommandsServiceProvider');
}
}
Don't forget to include the application service provider on your bootstrap/app.php and to enable Eloquent and Facades if you are using Lumen
If you run the command php artisan list you will see the list of added commands:
wn:controller Generates RESTful controller using the RESTActions trait
wn:controller:rest-actions Generates REST actions trait to use into controllers
wn:migration Generates a migration to create a table with schema
wn:model Generates a model class for a RESTfull resource
wn:pivot-table Generates creation migration for a pivot table
wn:resource Generates a model, migration, controller and routes for RESTful resource
wn:resources Generates multiple resources from a file
wn:route Generates RESTful routes.
Generating a model with validation rules
Runing the following command:
php artisan wn:model TestingModel --rules="name=required age=integer|min:13 email=email|unique:users,email_address"
Will generate a model containing the following rules:
public static $rules = [
"name" => "required",
"age" => "integer|min:13",
"email" => "email|unique:users,email_address",
];
Please refer to the Full README for more details.
Hope this helps :)
There is no such command built into laravel or lumen.
I found a package (on a site called google) that provides such a command: https://github.com/jijoel/validation-rule-generator
It's locked to illuminate/support 4.0.x, so won't work with current versions of laravel. If you have lots of models it might be worth to fork, bump the version in composer.json and see if it works.

How to generate a core controller in Laravel?

I have deleted the main controller Laravel by path: App\Http\Controllers\Controller.php;
All new controllers extent this controller:
class MainController extends Controller
{
}
How to generate a core controller in Laravel again?
I think you can't regenerate core Controller class via command neither artisan nor composer . You have to copy this from github or another project.
I don't think either you are able to regenerate it. But you can get a copy of the content here

location of helper function in Laravel 5

I am using Laravel 5 and new in this Platform. I studied about URL:asset In the earlier version of PHP, we could find the helper function in Auto Load or config files.
Do the helper function list exists somewhere in the Framework directory where if we want to change something in the helper function according to our need.
The helper functions are present in
/vendor/laravel/framework/source/illuminate/Foundation/helpers.php
You can find your helper function inside app.php as facades are registered on app.php and service container would let you resolve the class anywhere you want by use shorthandname or registered name;

Resources