Help
- php artisan help
Creating classes (Model)
Using artisan
- php artisan make:model <class_name>
- The class will be created in app
- Add the namespace use App\<class>
Standart
By default once you create the Model in SINGULAR, laravel will consider that you table will be in PLURAL
In case you want to maintain the singular add to you model:
- protected $table = '<class name>';
Fillable
- In the class created add
- protected $fillable = ['note'];
DB Configuration
- In the file .env
- Type the user and password
- Create the database in mysql
Migration
Create the tables in the DB
- Type in artisan: php artisan: php artisan make:migration <migration_name> --create=<table_name>
- php artisan mak:mig <migration_name> --create=<table_name>
- The migration will appears on the folder database/migrations
Migration commands
- $table->string('<field_name>');
- $table->integer('<field_name>');
- $table->timestamps();
- Creates 2 fields:
- Current time in tinker: Carbon\Carbon::now()
Create DB
- Type: php artisan migrate
Update
- Type: php artisan migrate:refresh
Delete DB
- Type: php artisan migrate:rollback
In case you have added a migration among the previous one
- First rollback all your migrations using
- php artisan migrate:reset
- Type: composer dump-autoload
Commands
- Get registers
- $<var> = <Class>:all();
Routes
- In the folder app/Http/ there is a file named routes.php
- Route::get('URL', function () {
- return view('<view_name>');
- });
- In the routes can do the queries
Pass arrays
- compact('notes');
- Can be pass in the return of each view.
$notes = Note::all();
return view ( 'notes', compact('notes') );
Views
- In the folder resources/views are stored all the views
- The arrays are displayed as
- @foreach($notes as $note)
Tinker
Console to interact with the DB
- php artisan tinker
The instructions are based on the classes
- \AppNote::truncate();
Commands
- DB::table('<table_name>')->insert({ '<field>' => '<value>'});
- DB::table('<table_name>')->get();
- DB::table('<table_name>')->where( '<field>' , '<value>');
- DB::table('<table_name>')->where( '<field>' , '<value>')->first();
- DB::table('<table_name>')->where( '<field>' , '<value>')->delete()
Foreign Keys
After create the main table add in the second table the foreign key
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
Comentarios
Publicar un comentario