Laravel 5 tricks (rough for me)

I need these few thing when I am working on Laravel 5. You can add more by comment..

 

How to install packages by composer
————————————————–
adding a package: composer require vendor_name/package_name
removing a package: composer remove vendor_name/package_name

adding form and html package:
—————————–
composer require illuminate/html

for pdf package (reporting: generating pdf):
——————————————-
composer require barryvdh/laravel-snappy
composer require barryvdh/laravel-dompdf

How to desable default dates (created_at and published_at) on form in Laravel:
————————————————-
You can disable Localy
1. set public $timestamps = false; in YourModel.php file

Or you can disable globaly
2
your_project_dirvendorlaravelframeworksrcIlluminateDatabaseEloquentModel.php
at line no. 65
Or
in Sublime press Ctrl+P and type model.php and u can see the above file link and select the file Model.php
and update the $timestamps variable to false;

/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
// public $timestamps = true;
public $timestamps = false;

Yahoo.. you are done.

chnaging the storage path in Laravel:
—————————————-
there are two methods to uoload files
1. Normal
Input::file(‘file’)->move(__DIR__.’/storage/upload/company/’,Input::file(‘file’)->getClientOriginalName());

2. Using Storage API
first add these line above ControllerName
// — for file system
use IlluminateSupportFacadesStorage;
use IlluminateSupportFacadesFile;

and then..
$file = Request::file(‘filefield’);
$extension = $file->getClientOriginalExtension(); // get the file’s extension
Storage::disk(‘local’)->put($file->getFilename().’.’.$extension,  File::get($file)); // uplaoding file to your local directory
$entry->mime = $file->getClientMimeType(); // get the file’s MIME type
$entry->original_filename = $file->getClientOriginalName(); // get the file name
MySQL Query Functions:
—————————
creat($inputs) : for inserting data to a table
all(): for getting all data.
lists(‘id’, ‘name’) : for getting list from a table. argument/s needed.

print last MySQL query in Laravel for debuging
——————————————————
enable log
DB::enableQueryLog();
then
dd(DB::getQueryLog());

Last Inserted Row/ ID:
———————
ModelName::lastInsertId();

Path in Laravel
—————
App Path:
app_path() : get tihe full path of the app directory
base_path(): get the path to the root directory of the application
public_path(): get the full path of the publich directory
storage_path(): get the storage (app/storage) directory path

Migration

To create the migration, via the command line, in the root folder of your application, simply type:
php artisan migrate:make create_users_table ––create=users

This will automatically create a migrations file inside of yourapp/database/migrations folder. Let’s take a look at the newly created file.

To run the migration andcreate our user table, use the command line again and run:
php artisan migrate
Just like that, the command will use the up() function and bam! We have our users table with all the columns we wanted.
Reverting Migrations: Now if you wanted to rollback migrations, we could use php artisan migrate:rollback or php artisan migrate:reset.

Adding CSS Files:
{{ HTML::style(path-to-css-dir/file-name.css’)}}
{{ HTML::style(‘css/main.css’)}}

Working With Session:
@if(Session::has(‘message’))
<p class=”alert”>{{ Session::get(‘message’) }}</p>
@endif

Errors:
FatalErrorException in EloquentUserProvider.php line 122: Class ‘AppUser’ not found
Ans =>
Need to update config/auth.php file. Change
‘model’ => ‘AppUser’
to
‘model’ => ‘AppModelsUser’

clean cache: How to clean cache in laravel?
—————–
php artisan cache:clear

 

How to delete file in laravel 5?
—————————————
File::delete(‘path-to-the-dir/file.ext’);

 

How to delete directory/folder in Laravel 5?
———————————————-
File::deleteDirectory(‘path-to-dir’);

Advertisements