How do you extend a view from a package in Laravel? - laravel

So i integrated this package to my application, https://github.com/thekordy/ticketit and this package has its own view and i want to modify the views like the create.blade.php,.. how would i do this appropriately?
because my current solutions is just to copy the view from the package change the return view('create'); in my controller?

You will notice many packages include in its instalation proccess this command:
php artisan vendor:publish
What it does behind the scenes is it looks for all package's service providers instructions in order to figure out it anything should be "published" (meaning copying from vendor folder to config/, views/ etc)
I looked at your package's service provider:
https://github.com/thekordy/ticketit/blob/0.2/src/TicketitServiceProvider.php and from line 179 to 182, the package seems to have the correct "publish" instructions.
Which means probably that the documentation skiped this part.
So, you should just basically hit the command php artisan vendor:publish and it will copy views, translations, public and migrations folder to your own apps folders.
Then you will see inside your resources/views a vendor folder, which will now have the ticketit views inside it.
Laravel figure it out when you say "view('ticketit.form.index')" and it will first look inside your own resources folder, if it don't find the content, it will try to look inside the package's folder.
For more, read the docs:https://laravel.com/docs/5.4/packages#views

Just to add one more thing, you can choose which type of resources to publish by using the tags for the publish command
php artisan vendor:publish --provider="Kordy\Ticketit\TicketitServiceProvider" --tag="views"
Publish only ticketit views (destination: base_path/resources/views/vendor/ticketit)
If for any reason, found extending views is not enough and want to extend functionalities or controllers themselves, ticketit allows using of custom routes file, you can use that to point to your own custom controllers.
Other supported vendor publish tags:
php artisan vendor:publish --provider="Kordy\Ticketit\TicketitServiceProvider" --tag="lang"
Publish only ticketit translation files (destination: base_path/resources/lang/vendor/ticketit)
php artisan vendor:publish --provider="Kordy\Ticketit\TicketitServiceProvider" --tag="db"
Publish only ticketit migration files (destination: base_path/database/migrations)
php artisan vendor:publish --provider="Kordy\Ticketit\TicketitServiceProvider" --tag="public"
Publish only ticketit web resources (js, css, ..) files (destination: public_path/vendor/ticketit)

For anyone facing this issue the solution is Laravel default convention, as shown in documentation: https://laravel.com/docs/5.4/packages#views (publishing views topic)
In short, you need to use the resource path as:
'__DIR__.'/path/to/views' => resource_path('views/vendor/view_namespace)'
where view_namespace is the second parameter inside your loadViewsFrom method.
So given the file: https://github.com/thekordy/ticketit/blob/0.2/src/TicketitServiceProvider.php
If line 102 is $this->loadViewsFrom($viewsDirectory, 'ticketit');
Line 103 should be:
$this->publishes([$viewsDirectory => base_path('views/vendor/ticketit')], 'views');

Related

Change default directory for creating migration in Laravel

I want to change the default directory for creating migration i.e. If I do php artisan make:migration create_users_table, it should create users table in /database/migrations/child_database. I do not want to use --path every time I create a migration for child_database. I want to change the default directory for php artisan make:migration.
you can create your own artisan command to achieve this:
create your a new artisan command and use it to make migration in your custom directory using this article. hope help you ^_^
try this package github repo or use laracast link
You can use loadMigrationsFrom() in your AppServiceProvider.
Inside the boot() function, run:
/**
* Register Custom Migration Paths
*/
$this->loadMigrationsFrom([
database_path().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR.'folder1',
database_path().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR.'folder2',
]);/
This question has been answered before, even though it's a little bit old it still has it's value.
Have a look at: https://stackoverflow.com/a/35895106
If you use this answer, the default directory is changed and you will have the solution you want.

How to create modular in laravel?

I want to create a modular to create a controller and a model.
Of course, the model is pre-created.
php artisan module:make-controller Admin\ReportController Report --model=Report
I see this message
The "--model" option does not exist.
I assume you used nWidart Modules, so the answer is you can not use --model option because nWidart Modules doesn't support that option/parameter. You need run another command to create a model, an example:
php artisan module:make-model Report Admin
Report is model name, and Admin is module name
check it out for more module commands

Laravel configuration usage for different servers in pure php file

I am new in Laravel and want to know such problem
I am running an web application on several server using Laravel.
But I have encountered with an issue.
When there is modification for the project, I need to sync with git on several servers.
But it has different settings for each server (eg: DB name, DB password...)
I have set it manually because I couldn't use .env or configuration file since the file is just pure php file.
The issue I want to solve is how can I get Laravel configuration data from pure PHP file(not controller or whatever).
It will be thankful if someone teach me solution.
You asked:
how can I get Laravel configuration data from pure PHP file
The answer would be this:
You just make some file in config/ folder of laravel app (or use the existing file like config/app.php)
You make an array of your key value pairs
<?php
return [
'some_key' => 'some_value',
...
and you simply call it with this code where ever you need it:
config('config_file.key');
for example
config('app.name');
would give you Laravel by default.

Laravel Cashier - publish migration results in "cannot declare class CreateCustomersColumns"

I have a fresh Laravel installation and I've added Cashier to my project.
Since the Users model on my app won't have a stripe connection, but rather an Accounts model, I need to alter their migration to add columns to Accounts instead of Users
The documentation says to run:
php artisan vendor:publish --tag="cashier-migrations"
which adds the two migration files to database/migrations
From there I can change users to accounts in the migration file.
When I try to run php artisan migrate, I get:
Whoops\Exception\ErrorException : Cannot declare class CreateCustomerColumns, because the name is already in use
This problem only goes away when I delete the migration files, but then the new columns are added to users.
The documentation states that you can disable their migration files by putting Cashier::ignoreMigrations(); in AppServiceProvider
I didn't realize that's what I wanted to do. I thought the publish command only published the two files I needed to edit, however, those are the only migration files that come with Cashier.
Be sure to add Cashier::ignoreMigrations(); in the register method.
And add use Laravel\Cashier\Cashier;
The file (and thus the class) must exist twice, probably in your database/migrations directory, most likely with 2 different datetime prefixes on the filenames.

How to run package in laravel 5?

How to run package in laravel 5?
I have downloaded one chat package from INTERNET. And, I have run command vendor:publish and it successfully published.
Now I don't know how to run that.
First you need to check whether the package ships with any service provider and facade. If so, add them in your config/app.php file. Package's service provider should go under providers array and facade should go under aliases array.
As soon as you specify providers under providers array, Laravel now know that you have called an external package, so it will load them. Thus, the package now will be available for your usage.
The easy way to run a package in laravel is:
add the package to composer.json file
then in terminal write composer dump-autoload

Resources