make model , controller ,seeder,factory,migration in custom folder - php

Is there any way to create model with all other classes in custom folder
when I try
php artisan make:model locations/City --all
I expect to create
controllers/locations/CitiesController
models/locations/City
etc.
only create a model in a custom folder (locations)
but the controller and others in the main folder

This feature exists in Laravel 8 and Laravel 9. If you're using any of the earlier versions, it won't work.

Related

Laravel5.6 - override a vendor view

I try to make my own version of a vendor blade template.
I dont want to extends the controller with the reference of the view.
So in my AppServiceProvider I add this line:
// Custom views for passport
$this->loadViewsFrom(__DIR__.'/../../resources/views/oauth/passport', 'passport');
I created a file named authorize.blade.php in /resources/views/oauth/passport
In the vendor controller method we can see this:
return $this->response->view('passport::authorize');
The problem is when I call the vendor controller method it loads his version of authorize.blade.php. I would like mine to be loaded and I expected the new line I added to AppServiceProvider to do that.
Passport comes with VUE components and views you need to publish first to override them. From the Laravel Passport page:
"If you would like to customize the authorization approval screen, you may publish Passport's views using the vendor:publish Artisan command."
All you need to do is run php artisan vendor:publish --tag=passport-views and the vendor views will be place in resources/views/vendor/passport, where you can edit them.
Use can use php artisan vendor:publish --tag=passport-views this will copy the views to your views folder for you to change.
So in my AppServiceProvider I add this line:
// Custom views for passport
$this->loadViewsFrom(DIR.'/../../resources/views/oauth/passport',
'passport');
You can use this option only by placing it in registry () instead of boot(). And then you can use your Views regardless of whether they were published in Vendor or not

According to this tutorial where have I to create this class in my Laravel project (a class implementing the UserProviderInterface interface)

I am abslolutly new in PHP and moreover in Laravel framework (I came from Java).
I am following this tutorial to create a custom authentication driver:
http://laravel-recipes.com/recipes/115/using-your-own-authentication-driver
I have a very newbye doubt: at the beginning of this tutorial it show that I have to create a class that implements UserProviderInterface.
It only show the code but not where this class have to be put into my Laravel project. The only clue about its positioning is the namespace:
namespace MyApp\Extensions;
But exactly where have I to put it?
I have the following structure:
It say to put it into MyApp\Extensions but I have not MyApp and Extension folder in my project? (or maybe the nampespaces name doesn't reflect a directory structure into the project tree?)
So where have I to create this class?
if you want it to go inside MyApp\Extensions, consider MyApp as the app folder.
Then only thing to do is create a folder named Extensions inside app folder and create your UserProviderInterface.php there.
But If I were you, I'd create it under app\Auth\Providers\UserProviderInterface.php
I believe what the page author meant was to create a folder named Extensions and create the provider file under app/Extensions folder. MyApp is just a custom namespace that the author chose, that in default laravel app, it should be App.
Which means, if you create a folder Extensions under app folder, the DummyAuthProvider should then be in the namespace of namespace App\Extensions;

Laravel: Edit file in the vendor directory

I am working with laravel and I have installed a package using composer by running this command composer require mailchimp/mailchimp=~2.0.
After that I got a folder 'mailchimp' in the vendor directory. In there, there is a file named Mailchimp.php that I have to modify, but based on some old posts here, if I modify the file, any time I run the command composer update, I will loose my changes in the file, just because it is located in the vendor directory. So is there any option for me to solve this problem ?
I tried using the command php artisan vendor:publish but I do not get the expected results.
You can create a custom class which will extend the Mailchimp class and override the function you want. Then use the custom class in your code.
use DrewM\MailChimp\MailChimp;
class CustomMailChimp extends MailChimp {
...
// The function you would like to override
}
Then use it new CustomMailChimp(..)

Laravel 5 - Moved User model to App/Models causing autoload issues

Working on a inherited Laravel Spark project that contained two User models.
One is the standard Spark model inside the App directory but the other is inside App/Models. I have combined the two models and updated the auth.php to reference the User model inside the Models directory but composer dump-autoload is saying it cannot find the App/User model.
How can I tell the autoloader that the User model is not there any more but
instead in the Models directory?
Edit:
I have changed the namespace to App/Models but still receive the error:
class_parents(): Class App\User does not exist and could not be loaded
In my terminal when running dump-autload
Second Edit:
Fixed, didn't realise the namespace was referenced so much. Did a find and replace on App\User and sorted the issue.
A standard Laravel installation will work by simply changing the namespace as others have mentioned; however Laravel Spark references the User and Team models, therefore a namespace change alone will not work.
You shouldn't edit any files inside the vendor/laravel/spark-aurelius (aurelius codename will vary depending on your version) as these changes are not tracked.
With Spark, you should add the following lines into your app/Providers/SparkServiceProvider.php:
public function register()
{
Spark::useUserModel('App\Models\User');
Spark::useTeamModel('App\Models\Team');
}
You can set your own custom App\Models directory, rather than using the above example.
Finally you will need to update any references you make to your models, e.g. update controllers from use App\User to use App\Models\User.
Source: Laravel Spark 6.0 Customization
5th Jan 2020 Update: Remember to also update STRIPE_MODEL and BRAINTREE_MODEL values in your .env to your new namespace.
Laravel Spark 9.0 removes Braintree support, therefore you only need to update CASHIER_MODEL in 9.0.
21st Dec 2020 Update: Laravel 8.x now keeps all models in the app\Models directory by default. Even if you're on an older version of Laravel (e.g. 6.x) but are using Laravel Spark 11, then you do not need to do any of the above. Laravel Spark 11 will assume your models live in app\Models.
You need to change User model namespace:
namespace App\Models;
Try renaming the namespace. :)
namespace App\Models;
change namespace, and use like this
namespace App\Models;

Create model in a custom path in laravel

I want to create a model in a custom path. If i write the php artisan command like the following it will create model inside app.
php artisan make:model core
But i want to create the model file inside the custom made Models folder. How can i do it ?
Just define the path where you want to create model.
php artisan make:model <directory_name>/<model_name>
e.g.
php artisan make:model Models/core
You can do it easily with custom directory of your Models Folder (I assume Models Folder is sub folder of App).
Just use this command php artisan make:model Models/core then you will get your desire result.
Thanks.

Categories