Ir al contenido principal

PHP / Laravel III (Model)

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>
or
    • 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

Entradas populares de este blog

C# Using tabs

To use tabs in C# use the TabContainer element from AjaxControlToolkit Include AjaxControlToolkit  Include in the Web.config file, inside the tag <system.web> the following code  <pages>       <controls>         <add tagPrefix="ajaxCTK" namespace="AjaxControlToolkit" assembly="AjaxControlToolkit"/>       </controls>     </pages>   Include TabContainer element First  include TabContainer element that is the section where all the tabs will be displayed. <ajaxCTK:TabContainer ID="TabContainerUpdate" runat="server"                 Height="800"                 CssClass="ajax__tab_style"> </ajaxCTK:TabContainer> Second per each tab include the following code corresponding to each ...

Rails - Basic Steps III

pValidations Validations are a type of ActiveRecord Validations are defined in our models Implement Validations Go to   root_app/app/models Open files  *.rb for each model Mandatory field validates_presence_of   :field Ex:   validates_presence_of    :title Classes The basic syntax is class MyClass        @global_variable                def my_method              @method_variable        end end Create an instance myInstance = MyClass.new Invoke a mehod mc.my_method class() method returns the type of the object In Ruby, last character of method define the behavior If ends with a question -> return a boolean value If ends with an exclamation -> change the state of the object Getter / Setter method def global_variable       return @global_variable end ...

Python create package

Create a root folder Create a sub-folder "example_pkg" that contains the funtionallity packaging_tutorial/ example_pkg/ __init__.py In the root folder create the following structure  packaging_tutorial/ example_pkg/ __init__.py tests/ setup.py LICENSE README.md in the setup.py contains the configuration of the packages your package is found by find_packages() import setuptools with open ( "README.md" , "r" ) as fh : long_description = fh . read () setuptools . setup ( name = "example-pkg-YOUR-USERNAME-HERE" , # Replace with your own username version = "0.0.1" , author = "Example Author" , author_email = "author@example.com" , description = "A small example package" , long_description = long_description , long_description_content_type = "text/markdown" , url = "https://github.com/pypa/sam...