mysql


  • Use search terms in eloquent queries for Laravel

    Use search terms in eloquent queries for Laravel

    I needed to make an Eloquent query that could take search terms. The search terms are optional – no search term and the whole dataset is returned. In our particular data model the Users have one or many Associations. So the search term should check for user name, phone, email and association name. In the…

  • (PHP) Add column to table in Laravel

    Create migration in console: [php] php artisan make:migration add_mycolumn_to_mytable [/php] Use Schema::table() to access existing table (instead of Schema::create() for creating new tables) [php] public function up() { Schema::table(‘mytable’, function($table) { $table->text(‘mycolumn’); }); } public function down() { Schema::table(‘mytable’, function($table) { $table->dropColumn(‘mycolumn’); }); } [/php] Then run migrations: [php] php artisan migrate [/php]

  • (MySQL) MySQL doodles

    [sql] — Replace a word or string in a column UPDATE my_table SET my_column = REPLACE(my_column, ‘Old string’, ‘New string’); — [/sql]

  • Convert CSV to SQL

    This tool is fantastic! It saved me many hours of work, when I had complex CSV files I needed to turn into SQL tables http://www.convertcsv.com/csv-to-sql.htm

  • (PHP) Eloquent doodles for Laravel

    [php] // Get model by primary key $myModel = MyModel::find($id); //Where can use short syntax for equal comparison. $myModels = MyModel::where(‘someAttribute’, ‘=’, ‘someValue’)->get(); $myModels = MyModel::where(‘someAttribute’, ‘someValue’)->get(); // Delete models $affectedRows = MyModel::where(‘someAttribute’, ‘someValue’)->delete(); // Select distinct $distincts = MyModel::distinct()->select(‘someAttribute’)->where(‘someAttribute’, ‘someValue’)->get(); // Select with Limit and offset $myModels = MyModel::limit(30)->get(); $myModels = MyModel::limit(30)->offset(30)->get(); [/php] Different…

  • (PHP) Simple pagination

    Here’s a simple way to paginate when getting large amounts of posts from the database.

  • Generate dummy data

    Here’s a great tool for generating dummy data for your database etc. www.generatedata.com

  • Convert MySQL timestamp to PHP date

    If you save your date in the format of timestamp in your MySQL database, and want to output it like this: [php] $result = mysql_query("SELECT * FROM articles"); $row = mysql_fetch_array($result); $date = $row[‘timestamp’]; echo $date; [/php] Your date come out looking something like this: 2011-01-30 20:54:12 So it’s formated like YYYY-MM-DD HH:MM:SS But what…

  • (PHP) How to make a simple gallery

    This is just a VERY basic gallery that you can modify to suit your needs. For example you could add columns in the database and store more info about the images (date, size, tags etc.). You could very easily make the output use Lightbox or similar images viewers. And you could of course do a…