I'm using PHPStorm and I'm learning Laravel.
For "key too long" error I'm following the fix here: https://laravel.com/docs/master/migrations#creating-indexes
But, PHPStorm complaints for
Method 'defaultStringLength' not found in \Illuminate\Support\Facades\Schema
Why and how can I Solve? This is my AppServiceProvider.php file
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
/**
* Fix for key too long.
* #see https://laravel.com/docs/master/migrations#creating-indexes
*/
Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
I faced the same problem once and followed the answer from the following link to solve my problem. https://stackoverflow.com/a/44859379/4437710
Basically, replace use Illuminate\Support\Facades\Schema; with use Schema; I don't know the reason for this strange behavior. I'm not sure if it will work on your case too. But you can give it a try.
Another trick taken from the internet (Not tested by me):
For Laravel 5.4, use \Illuminate\Database\Schema\Builder::defaultStringLength(191); for correct function reference path instead
Related
I am using Laravel 5.8 and attempting to set up a custom validation extension.
I have created a class GroupValidator containing a validate function.
I have created a ValidationServiceProvider with the following code:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;
use App\Classes\GroupValidator;
class ValidationExtensionServiceProvider extends ServiceProvider
{
/**
* Register services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
Validator::extend('valid_parent_id', 'GroupValidator#validate');
}
}
When my validation triggers I get a Class GroupValidator does not exist exception. However if I specify the full path to my class in the extend function call like so:
Validator::extend('valid_parent_id', 'App\Classes\GroupValidator#validate');
then everything works fine.
Is there some way I can set this up so that I don't have to include the full path to my class?
I've bound an interface to its implementation, like this:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\Mail\Contracts\Webhook;
use App\Services\Mail\Clients\MailgunWebhook;
class MailServiceProvider extends ServiceProvider {
protected $defer = true;
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot() {
//
}
/**
* Register the application services.
*
* #return void
*/
public function register() {
$this->app->bind(
Webhook::class,
MailgunWebhook::class
);
}
}
I ran all of these:
php artisan config:clear
php artisan clear-compiled
php artisan optimize
composer dumpautoload
Yet, I still got the "Target is not instantiable error", when trying to use the binding.
After I commented out the $defer property, the binding started to work.
Why can't I use $defer in this case?
As stated in the documentation, to which I linked myself and failed to read it in full, $defer needs the provides() method.
In my case, all I needed to add in my service provider class is this:
public function provides() {
return array(Webhook::class);
}
Thanks to James Fenwick for his comment.
where is the appropriate place to catch every time laravel is used even non http based action?
I want to catch everything even artisan commands, Queues or Task that running.
the only place I can think of is bootstrap\app.php
but its too hacky and with my experience with laravel I am sure there is some built in way of doing it
is there some one place to catch them all?
you can add your logger to your app/Providers/AppServiceProvider.php's boot() function.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
// Your logger goes here
error_log('log...');
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
I recently started using laravel so i'm a beginner, and right now i'm working on a small project which requires me use shortcodes(like the ones in wordpress).
So i searched for a little bit and found this package:
https://packagist.org/packages/webwizo/laravel-shortcodes
I ran the installation and usage the way it's written but i get the error : Class 'App\Providers\Shortcode' not found in the provider I have to make using the laravel make:provider command as specified in the package instructions, below is my exact usage and install code.
added this to the providers array :
/*
* shortcodes providers
*/
Webwizo\Shortcodes\ShortcodesServiceProvider::class,
App\Providers\ShortcodesServiceProvider::class,
Added this to aliases:
'Shortcode' => Webwizo\Shortcodes\Facades\Shortcode::class,
this is the content of my ShortcodesServiceProvider in app/providers:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Shortcodes\JobsForm;
class ShortcodesServiceProvider extends ServiceProvider
{
/**
Bootstrap the application services.
*
#return void
*/
public function boot()
{
//
}
/**
Register the application services.
*
#return void
*/
public function register()
{
Shortcode::register('jobs', JobsForm::class);
}
}
I use laravel 5.4 so that might be an issue.
The thing is the class obviously exists, it gives the Shortcodes class not found error because I think it searches for it in the app/providers/ShortcodesServiceProvider file, and obviously it's not there it's in the vendor file.
Is there something I'm missing i've checked and double checked, I can't seem to get this thing to work.
It shoould work considering it has an alias defined right ?
I used it in the view like this:
return view('quarx-frontend::pages.' . $page->template)->with('page', $page)->withShortcodes();
Thanks for taking the time to read this any help would be much appreciated.
If you need any more info I'll be glad to supply it.
p.s. sorry for bad english ,not a native speaker :P
It searches for Shortcode in the App\Providers; namespace and not in the root namespace where the Facade is defined.
You can fix this in App\Providers\ShortcodesServiceProvider.php by either doing:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Shortcodes\JobsForm;
use Shortcode;
class ShortcodesServiceProvider extends ServiceProvider
{
Or use \Shortcode
/**
* Register the application services.
*
* #return void
*/
public function register()
{
\Shortcode::register('jobs', JobsForm::class);
}
I would recommend the first option.
I am currently working on view composer:
The problem right now that i have, The route that im calling inside the ComposerServiceProvider.php says that it cant find the route to the ViewComposers/LespakketComposer.php
In the Config\app.php i did add the correct App\Providers\ComposerServiceProvider::class,
Here is my code in ComposerServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;
class ComposerServiceProvider extends ServiceProvider {
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
View::composer('*','App\Http\ViewComposer\LespakketComposer');
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
Here is my error:
ReflectionException in Container.php line 734:
Class App\Http\ViewComposer\LespakketComposer does not exist
The Routes in my folder structure
Does anyone has a solution for my problem?
( The file i am requesting is an Class Indeed )
You forgot a s:
App\Http\ViewComposers\LespakketComposer