- Laravel Eloquent where between two dates from Database
- Subscribe to RSS
- Database: Query Builder
- Subscribe to RSS
- Database: Migrations
Laravel Eloquent where between two dates from DatabaseMigrations are like version control for your database, allowing your team to easily modify and share the application's database schema. Migrations are typically paired with Laravel's schema builder to easily build your application's database schema. If you have ever had to tell a teammate to manually add a column to their local database schema, you've faced the problem that database migrations solve. The Laravel Schema facade provides database agnostic support for creating and manipulating tables across all of Laravel's supported database systems. To create a migration, use the make:migration Artisan command :. Each migration file name contains a timestamp which allows Laravel to determine the order of the migrations. The --table and --create options may also be used to indicate the name of the table and whether the migration will be creating a new table. These options pre-fill the generated migration stub file with the specified table:. If you would like to specify a custom output path for the generated migration, you may use the --path option when executing the make:migration command. The given path should be relative to your application's base path. A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should reverse the operations performed by the up method. Within both of these methods you may use the Laravel schema builder to expressively create and modify tables. To learn about all of the methods available on the Schema builder, check out its documentation. For example, this migration example creates a flights table:. Some migration operations are destructive, which means they may cause you to lose data. In order to protect you from running these commands against your production database, you will be prompted for confirmation before the commands are executed. To force the commands to run without a prompt, use the --force flag:. To rollback the latest migration operation, you may use the rollback command. This command rolls back the last "batch" of migrations, which may include multiple migration files:. You may rollback a limited number of migrations by providing the step option to the rollback command. For example, the following command will rollback the last five migrations:. The migrate:refresh command will roll back all of your migrations and then execute the migrate command. This command effectively re-creates your entire database:. The migrate:fresh command will drop all tables from the database and then execute the migrate command:. To create a new database table, use the create method on the Schema facade. The create method accepts two arguments. The first is the name of the table, while the second is a Closure which receives a Blueprint object that may be used to define the new table:. When creating the table, you may use any of the schema builder's column methods to define the table's columns. You may easily check for the existence of a table or column using the hasTable and hasColumn methods:. If you want to perform a schema operation on a database connection that is not your default connection, use the connection method:. To drop an existing table, you may use the drop or dropIfExists methods:. Before renaming a table, you should verify that any foreign key constraints on the table have an explicit name in your migration files instead of letting Laravel assign a convention based name. Otherwise, the foreign key constraint name will refer to the old table name. The table method on the Schema facade may be used to update existing tables. Like the create method, the table method accepts two arguments: the name of the table and a Closure that receives a Blueprint instance you may use to add columns to the table:. The schema builder contains a variety of column types that you may specify when building your tables:. In addition to the column types listed above, there are several column "modifiers" you may use while adding a column to a database table. For example, to make the column "nullable", you may use the nullable method:. Below is a list of all the available column modifiers.
Subscribe to RSS
Database: Query Builder
Laravel makes interacting with databases extremely simple across a variety of database backends using either raw SQL, the fluent query builderand the Eloquent ORM. Currently, Laravel supports four databases:. In this file you may define all of your database connections, as well as specify which connection should be used by default. Examples for most of the supported database systems are provided in this file. By default, Laravel's sample environment configuration is ready to use with Laravel Homesteadwhich is a convenient virtual machine for doing Laravel development on your local machine. You are free to modify this configuration as needed for your local database. Laravel makes this a breeze, and the proper connections will always be used whether you are using raw queries, the query builder, or the Eloquent ORM. Note that three keys have been added to the configuration array: readwrite and sticky. The read and write keys have array values containing a single key: host. The rest of the database options for the read and write connections will be merged from the main mysql array. You only need to place items in the read and write arrays if you wish to override the values from the main array. So, in this case, The database credentials, prefix, character set, and all other options in the main mysql array will be shared across both connections. The sticky option is an optional value that can be used to allow the immediate reading of records that have been written to the database during the current request cycle. If the sticky option is enabled and a "write" operation has been performed against the database during the current request cycle, any further "read" operations will use the "write" connection. This ensures that any data written during the request cycle can be immediately read back from the database during that same request. It is up to you to decide if this is the desired behavior for your application. When using multiple connections, you may access each connection via the connection method on the DB facade. You may also access the raw, underlying PDO instance using the getPdo method on a connection instance:. Once you have configured your database connection, you may run queries using the DB facade. The DB facade provides methods for each type of query: selectupdateinsertdeleteand statement.
Subscribe to RSS
Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems. There is no need to clean strings being passed as bindings. You may use the table method on the DB facade to begin a query. The table method returns a fluent query builder instance for the given table, allowing you to chain more constraints onto the query and then finally get the results using the get method:. You may access each column's value by accessing the column as a property of the object:. If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single stdClass object:. If you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:. If you would like to retrieve a Collection containing the values of a single column, you may use the pluck method. In this example, we'll retrieve a Collection of role titles:. If you need to work with thousands of database records, consider using the chunk method. This method retrieves a small chunk of the results at a time and feeds each chunk into a Closure for processing. This method is very useful for writing Artisan commands that process thousands of records. For example, let's work with the entire users table in chunks of records at a time:. You may stop further chunks from being processed by returning false from the Closure :. The query builder also provides a variety of aggregate methods such as countmaxminavgand sum. You may call any of these methods after constructing your query:. Instead of using the count method to determine if any records exist that match your query's constraints, you may use the exists and doesntExist methods:. Of course, you may not always want to select all columns from a database table. Using the select method, you can specify a custom select clause for the query:. If you already have a query builder instance and you wish to add a column to its existing select clause, you may use the addSelect method:. Sometimes you may need to use a raw expression in a query. To create a raw expression, you may use the DB::raw method:. Instead of using DB::rawyou may also use the following methods to insert a raw expression into various parts of your query. The selectRaw method can be used in place of select DB::raw This method accepts an optional array of bindings as its second argument:. The whereRaw and orWhereRaw methods can be used to inject a raw where clause into your query. These methods accept an optional array of bindings as their second argument:. The havingRaw and orHavingRaw methods may be used to set a raw string as the value of the having clause. The orderByRaw method may be used to set a raw string as the value of the order by clause:. The query builder may also be used to write join statements. To perform a basic "inner join", you may use the join method on a query builder instance.