Fast CRUD With Laravel 5

Source: https://gilacoding.com/read/membuat-crud-laravel-hanya-dengan-5-menit

  1. Create a new laravel project named ‘latihan-crud’
    Type this command: composer create-project –prefer-dist laravel/laravel latihan-crud

     
  2. Modify .env file in /laravel/latihan-crud/ directory  to set the database setting:
  3. Create the new database ‘latihan-crud’ on phpmyadmin
  4. Install new package ‘crud-generator’ from ‘appzcoder’: composer require appzcoder/crud-generator
    DONT FORGET TO GO INTO ‘latihan-crud’ DIRECTORY FIRST!

    Look up ‘appzcoder’ directory in /latihan-crud/vendor/
  5. Add the new package ‘Appzcoder\CrudGenerator\CrudGeneratorServiceProvider::class’ in /config/app.php in ‘providers’

     
  6. Install another package ‘laravelcollective/html’

    Then do the same with #5 to add the new package ‘Collective\Html\HtmlServiceProvider::class,’ in /config/app.php in ‘providers’:

     
  7. In the same file (app.php), add the new facades ‘Form’ and ‘HTML’ (????) in ‘aliases’ section:
  8. Do ‘composer update’ to update the dependencies
  9. Then publish our files and setting
  10. Create a new class ‘Posts’ for our crud generator with two fields ‘title’ and
    <OLD>

    BUT I GOT THIS ERROR:

    </OLD
    <NEW>
    From this https://packagist.org/packages/appzcoder/crud-generator, I found the source of the error, I supposed to use ‘;’ (“title#string; body#text”) instead of ‘,’ (“title#string, body#text”) between the fields like this (DONT USE THIS COMMAND. THIS IS ONLY JUST A CORRECT COMMAND)

    USE THIS COMMAND INSTEAD TO MAKE IT CONSISTENT WITH THE ORIGINAL TUTORIAL:

    </NEW>
    The script for the table migration would be exist in /database/migrations/ directory (IF YOU FIND MORE THAN ONE SCRIPT WITH SAME TABLE ‘Posts’ LIKE ‘2016_10_22_081014_create_posts_table.php’ AND ‘2016_10_22_074719_create_posts_table.php’, PLS REMOVE THE OLD ONE FIRST! OR WE’D GET THIS ERROR: “Cannot declare class CreatePostsTable, because the name is already in use”)
  11. Push the table script to our database with ‘php artisan migrate’

    Check our database to make sure the new table ‘posts’ is already created! SOMEHOW THE OTHERS TABLES ‘migrations’, ‘users’ AND ‘password_resets’ ARE CREATED ALSO!
  12. Test on the browser. Start the server: ‘php artisan serve’ then open on our browser : http://localhost:8000/ –> It shows the laravel welcome page!
    Remember, A new route ‘posts’ is already created automatically from previous step, so to see the CRUD in action, pls go to http://localhost:8000/postsposts
  13. Add new data.
    Click the ‘+’ blue icon create-newthen fill the form with the fields ‘Title’ and ‘Body’. Then press ‘Create’ Buttonnew-postBut I got this error:

    I found the solution! In ‘Post’ class in /app/Post.php, at ‘$fillable’ variable, It’s only has ‘title’ field But there is no ‘body’ field!

    So add ‘body’ field like this:

    Then repeat the creation of new data! Here is the expected resultpostsMembuat Login Laravel 5 2 dengan username dan Hak akses
  14. At the right top of the page, there are two links ‘login’ and ‘register’ but none of them are working. To make them work, we need to create the authentication to create users like this: php artisan make:auth

    Dont forget to stop the server first by clicking CTRL+C!
    The above command would create some views for login, register, password, etc. Please check them in /resources/views/auth/ directory!
    This also create automatically a few new routes in /routes/web.php
  15. Now we can try them on our browser
    Remember, we dont need to create/migrate the user table because we already did this at the previous step (#11) when we did the first migration!
    Now the links ‘login’ and ‘register’ are working!
    Here is the register page: http://localhost:8000/registerregisterI use password: satria. Successful registration would be redirected to http://localhost:8000/homelogged-inThe links ‘login’ and ‘register’ also be showed up on the initial page http://localhost:8000/
  16. Change/set default route ‘/’
    In /routes/web.php, the default route for http://localhost:8000/ would go to the welcome page in /resources/views/welcome.blade.php. Here is the default route

    We want to change it to go to our login page! We have the login page in /resources/views/auth/login.blade.php. So here is the modified route

    Now each our homepage http://localhost:8000/ is opened, it’d open the login page!
    If we want to set the default route to the post page, please modify /routes/web.php like this:

    Now, if we open http://localhost:8000/, it’d show the list of the post page!
    LEARN ALSO THIS:
    Laravel Admin Panel
    An admin panel for managing users, roles, permissions & crud.

OSClass First Plugin Tutorial

OSCLASS PLUGIN TUTORIAL
1. Create a new directory in any directory. name it ‘my_firstplugin’.
2. Create a new file in the directory. name it ‘index.php’.
3. open the file and add these lines in it :
/*
Plugin Name: My First Plugin
Short Name: my_firstplugin
Plugin URI: –
Description: A first osclass plugin for tutorial purpopse.
Version: 1.00
Author: Satria
Author URI: –
*/
above is ‘plugin info’
4. to make the plugin installable, add this function : osc_register_plugin. so

/**
* (hook: install) Make installation operations
*         It creates the database schema and sets some preferences.
* @returns void.
*/
function my_firstplugin_install() {

}
osc_register_plugin(osc_plugin_path(__FILE__), ‘my_firstplugin_install’);

then add two hooks to initialize (load/init) the plugin and to uninstall the plugin, respectively :

/**
* (hook: init) Registers scripts and styles.
* @returns void.
*/
function my_firstplugin_load() {

}
osc_add_hook(‘init’, ‘my_firstplugin_load’);

/**
* (hook: uninstall) Make un-installation operations
*         It destroys the database schema and unsets some preferences.
* @returns void.
*/
function my_firstplugin_uninstall() {

}
osc_add_hook(osc_plugin_path(__FILE__) . ‘_uninstall’, ‘my_firstplugin_uninstall’);

5. those are the minimum requirement (install, uninstall and load) to create an osclass plugin. Zip the directory (so it’ll be my_firstplugin.zip) then open the oc-admin page and add the plugin from the zip file.
if there is an error “There was a problem adding the plugin” when installing the plugin then just copy and paste the plugin directory in oc-content/plugins/ directory

6. From admin page, we can install/uninstall and enable/disable our plugin but this plugin dont have any functionality yet. we will add some scripts so we can display the links/menu in admin page to configure the plugin. In the index.php file, add this hook ‘admin_menu’ so it’ll be :
/**
* (hook: admin_menu) Display the admin menu
* @returns html.
*/
function my_firstplugin_admin_menu() {
echo ‘<h3><a href=”#”>My First Plugin</a></h3>
<ul>
<li><a href=”‘ . osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . ‘/admin/link1.php’) . ‘”>&raquo; ‘ . __(‘Link 1’, ‘my_firstplugin’) . ‘</a><li>
<li><a href=”‘ . osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . ‘/admin/link2.php’) . ‘”>&raquo; ‘ . __(‘Link 2’, ‘my_firstplugin’) . ‘</a></li>
</ul>’;
}
osc_add_hook(‘admin_menu’, ‘my_firstplugin_admin_menu’);

EDIT : if we have osclass version higher than 320 then we better to edit the above function like this :
/**
* (hook: admin_menu) Display the admin menu
* @returns html.
*/
function my_firstplugin_admin_menu() {
if(osc_version()<320) {
echo ‘<h3><a href=”#”>My First Plugin</a></h3>
<ul>
<li><a href=”‘ . osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . ‘/admin/link1.php’) . ‘”>&raquo; ‘ . __(‘Link 1’, ‘my_firstplugin’) . ‘</a><li>
<li><a href=”‘ . osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . ‘/admin/link2.php’) . ‘”>&raquo; ‘ . __(‘Link 2’, ‘my_firstplugin’) . ‘</a></li>
</ul>’;
}else{
osc_add_admin_submenu_divider(‘plugins’, __(‘My First Plugin’, ‘my_firstplugin’), ‘my_firstplugin_divider’, ‘administrator’);
osc_add_admin_submenu_page(‘plugins’, ‘&raquo; ‘.__(‘Link 1’, ‘my_firstplugin’), osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . ‘/admin/link1.php’), ‘my_firstplugin_link1’, ‘administrator’);
osc_add_admin_submenu_page(‘plugins’, ‘&raquo; ‘.__(‘Link 2’, ‘my_firstplugin’), osc_admin_render_plugin_url(osc_plugin_folder(__FILE__) . ‘/admin/link2.php’), ‘my_firstplugin_link2’, ‘administrator’);
}
}
//osc_add_hook(‘admin_menu’, ‘my_firstplugin_admin_menu’);
if(osc_version()<320) {
osc_add_hook(‘admin_menu’, ‘my_firstplugin_admin_menu’);
} else {
osc_add_hook(‘admin_menu_init’, ‘my_firstplugin_admin_menu’);
}

dont forget to add a new directory ‘admin’ in the plugin. then add two new files in it : link1.php and link2.php. the files will display the html content. you can add any html file in its.

7. Ok. great. then how to add any content on the front page by our plugin? Without any plugin we can add any content in oc-content/themes/your-theme/main.php. ‘your-theme’ is curent activated theme, it could be ‘bender’ (default theme). we can insert any html/php code in main.php file like <p>TEST</p> or <?php echo “Great”; ?>. If we want to make it better, we can include any php/html file and add this code <?php osc_current_web_theme_path(‘your_file.php’) ; ?> dont forget to add the new file your_file.php and fill it with any html/php code.

8. Ok. now we want to connect to database and pull some data to be displayed in the admin page.
in index.php file below the plugin info, add a line :
require_once ‘model/myfirstplugin_model.php’;
then add a new directory ‘model’ and a file ‘myfirstplugin_model.php’ in it.
we want to display all city in a table t_city on link1.php file. so add these lines in myfirstplugin_model.php file :
<?php

/**
* Data Access Object (DAO) for messages.
*    Performs operations on database.
* @author Satria
* @package my_firstplugin
* @subpackage Model
* @since 1.00
*/
class MyFirstPluginModel extends DAO {

function __construct() {
parent::__construct();
}

/**
* Singleton.
*/
private static $instance;

/**
* Singleton constructor.
* @return an MadhouseMessengerDAO object.
*/
public static function newInstance() {
if(!self::$instance instanceof self) {
self::$instance = new self;
}
return self::$instance;
}

/**
* Return table name city
* @return string
*/
public function getTable_City()
{
return DB_TABLE_PREFIX.’t_city’;
}

/**
* Get all city
*
* @return array
*/
public function getAllCity()
{
$this->dao->select();
$this->dao->from( $this->getTable_City() ) ;
$this->dao->orderBy(‘s_name’, ‘ASC’) ;

$results = $this->dao->get();
if( !$results ) {
return array() ;
}

return $results->result();
}
}

then edit link1.php file so it’ll be :

<div id=”settings_form” style=”border: 1px solid #ccc; background: #eee; “>
<div style=”padding: 20px;”>
<div>
<fieldset>
<legend><?php _e(‘My First Plugin Link 1 : All City Name’); ?></legend>
<!–p>
<?php _e(‘Congratulation. This is a link 1 content’, ‘my_firstplugin’); ?>
</p–>
<?php
$city = MyFirstPluginModel::newInstance()->getAllCity();
foreach($city as $c){
echo $c[‘s_name’].'<br>’;
}
?>
<p>
<?php _e(‘Created by Satria Faestha. Osclass version : ‘.osc_version(), ‘my_firstplugin ‘); ?>
</p>
</fieldset>
</div>
</div>
</div>

then open link1.php on admin page. All city name will be displayed on the page.
You can play with the query like just show 5 top city, etc.

9. Now we want to add our own table so when we install the plugin, our table will be created and ready to be used. otherwise if we want to uninstall the plugin, the tables will be deleted.
Add these two sql file in sql directory (create a new directory ‘sql’). they are myfirstplugin.sql and add_data.sql
in myfirstplugin.sql :
CREATE TABLE /*TABLE_PREFIX*/t_my_firstplugin (
pk_i_id INT(2) UNSIGNED NOT NULL AUTO_INCREMENT,
s_name VARCHAR(255),

PRIMARY KEY (pk_i_id)
) ENGINE=InnoDB DEFAULT CHARACTER SET ‘UTF8’ COLLATE ‘UTF8_GENERAL_CI’;

in add_data.sql :
INSERT INTO /*TABLE_PREFIX*/t_my_firstplugin (pk_i_id, s_name) VALUES (1,’Satria Faestha’);
INSERT INTO /*TABLE_PREFIX*/t_my_firstplugin (pk_i_id, s_name) VALUES (2,’Ermillya Roza’);
INSERT INTO /*TABLE_PREFIX*/t_my_firstplugin (pk_i_id, s_name) VALUES (3,’Sarah Almahyrah’);
INSERT INTO /*TABLE_PREFIX*/t_my_firstplugin (pk_i_id, s_name) VALUES (4,’Alim Alhikmah’);

in myfirstplugin_model.php add these lines :
/**
* Import sql file
* @param type $file
*/
public function import($file)
{
$path = osc_plugin_resource($file) ;
$sql = file_get_contents($path);

if(! $this->dao->importSQL($sql) ){
throw new Exception( “Error importSQL::MyFirstPluginModel<br>”.$file ) ;
}
}
/**
* Remove data and tables related to the plugin.
*/
public function uninstall()
{
$this->dao->query(sprintf(‘DROP TABLE ‘.DB_TABLE_PREFIX.’t_my_firstplugin’) ) ;
}

then in index.php file edit these functions :
function my_firstplugin_install() {
MyFirstPluginModel::newInstance()->import(‘my_firstplugin/sql/myfirstplugin.sql’);
MyFirstPluginModel::newInstance()->import(‘my_firstplugin/sql/add_data.sql’);
}
osc_register_plugin(osc_plugin_path(__FILE__), ‘my_firstplugin_install’);

and

function my_firstplugin_uninstall() {
MyFirstPluginModel::newInstance()->uninstall();
}
osc_add_hook(osc_plugin_path(__FILE__) . ‘_uninstall’, ‘my_firstplugin_uninstall’);

then do uninstall (if our plugin still installed) and install the plugin. you can see the new table t_my_firstplugin in our database. you can add new function in the model file to pull the content :
public function getMyFirstPluginData()
{
$this->dao->select();
$this->dao->from( $this->getTable_MyFirstPlugin() ) ;
//$this->dao->orderBy(‘s_name’, ‘ASC’) ;
$this->dao->orderBy(‘pk_i_id’, ‘ASC’) ;

$results = $this->dao->get();
if( !$results ) {
return array() ;
}

return $results->result();
}

then in link2.php file, edit the content so it’ll be :
<div id=”settings_form” style=”border: 1px solid #ccc; background: #eee; “>
<div style=”padding: 20px;”>
<div>
<fieldset>
<legend><?php _e(‘My First Plugin Link 2’); ?></legend>
<p>
<?php
$data = MyFirstPluginModel::newInstance()->getMyFirstPluginData();
foreach($data as $d){
echo $d[‘s_name’].'<br>’;
}
?>
</p>
</fieldset>
</div>
</div>
</div>

10. We also want the data in our table can be added, edited and/or deleted.

Mac OSX and IOS

MAC OSX AND IOS TUTORIAL LINKS
1. http://www.raywenderlich.com/17811/how-to-make-a-simple-mac-app-on-os-x-10-7-tutorial-part-13

How to Make a Simple Mac App on OS X 10.7 Tutorial: Part 2/3

How to Make a Simple Mac App on OS X 10.7 Tutorial: Part 3/3

2. https://developer.apple.com/library/mac/referencelibrary/GettingStarted/RoadMapOSX/books/RM_YourFirstApp_Mac/Articles/Introduction.html

Macbook Tips and Tricks

MACBOOK PRO TIPS & TRICKS
1. Q: Where is the ‘Page Up’, ‘Page Down’, ‘Move Top’ and ‘Move End’ Key?
A: For ‘Page Up’, Press ‘fn’ and ‘Up Arrow’
For ‘Page Down’, Press ‘fn’ and ‘Down Arrow’
For ‘Move Top’, Press ‘command’ and ‘Up Arrow’
For ‘Move End’, Press ‘command’ and ‘Down Arrow’
ref:https://discussions.apple.com/thread/4588271?start=0&tstart=0ß

Q: How To Select From Start/Certain Point To The End?

A: Press ‘command’, ‘shift’ and ‘Down Arrow’
2. Q: How to duplicate row on ‘Atom’?
A: Press ‘command’ ‘shift’ and ‘D’
3. Q: How to use ‘backspace’?
A: Press ‘fn’ and ‘Delete’
ref:http://www.apple.com/shop/question/answers/readonly/are-apple-keyboards-with-only-a-delete-key-less-efficient-in-making-deletions-than-conventional-keyboards-with-both-a-backspace-and-a-delete-key/QA9JYDAJHJHYPYUJF?tagName=mac
4. Q: How to rename?
A: Rename a file or folder by selecting it and hitting the ‘return’ key. Just click on the icon of the file/folder from the OS X Finder, and then hit the return key, then type in the new name. This is quick and simple, and likely the most traditional method of renaming on the Macs
5. Q: How to delete (keyboard shortcut)?
A: By now you’ve probably figured out that you can’t just delete a file in the Mac Finder using the delete key. The solution: Select the files and/or folders you want to delete in the Finder, then. Press the [Command][Delete] keyboard keys at the same time. Continue reading “Macbook Tips and Tricks”

Change The Background Color Of WordPress Visual Editor To Black

I want to change the background color of my wordpress visual editor from white to black. I like black color for my wordpress background because I think it is easier to read (for me), energy-saving and it don’t make my eye irritated if I am reading for a long time. I am using the twenty sixteen theme and I already changed the background color to black (the font is white, of course). My articles mostly about the technical IT like programming, new tecnology and tutorial. When I am dealing with some phrases that I need to be emphasized, I like to make them colorful so I can read easier and make it different with the others. The problem I encountered was the WordPress visual editor. The default color was white. So when I changed the color of some words on the white background visual editor, I felt different when I saw it on the black wordpress page. It was not easy to change it like the wordpress page did. I can’t mess style.css to do this job. From this stackoverflow link http://wordpress.stackexchange.com/questions/25355/how-do-you-change-the-visual-editors-background-color, I know how to accomplish my goal. Here are the steps:

  1. Edit function.php file in your active theme. I use child theme so I need to edit the file in my child theme directory (/wp-content/themes/2016-child-simple-clean-design). Add this single line at the bottom of the file:
  2. Add (or edit if exist) editor-style.css in the theme directory like this:

    The first four lines is enough to change the background of the visual editor to black. The last three lines is about the change the color on the crayon syntax highlighting plugin. We need to change the default color on the plugin from white to black (#1a1a1a) because the plugin use white background and we already change the font color to white also! at this case we would not see our code! crayon-white so change the font color to black. crayon-black OK. that is it! Enjoy.

Install Laravel 5 On Ubuntu 14.04

References:

1. https://www.howtoforge.com/tutorial/install-laravel-on-ubuntu-for-apache/

2. https://laravel.com/docs/5.2

How to install:

  1. Laravel uses composer much for installation. So first check the composer version (I already installed the composer) through the terminal.

    Here is the result for my system.

    check_composerMy composer version is 1.0-dev. It says my composer version is over 60 days old. Let update it.

    But the result shows complain about permission.

    composer_update_errorHere we need ‘sudo’ to update it.

    Here is the success result. composer_update_sudoOkay. Lets check it again. check_composer2My latest composer version is  1.2-dev.
  2. Ready to install Laravel. First I need to go in laravel directory in Documents/works/. Then there are two ways how to install it. They are ‘Laravel Installer’ and ‘composer create-project’. Somehow Laravel Doc mention the installer first. I dont know which one is the best but this QA from stackoverflow can give a perspective. First I used ‘composer create-project’ way to create a new project ‘mylaravelproject’. Here is the installation:

    Here is the files and directories are created. laravel_files_createdIt seems ‘composer create-project’ installation download all the component required to start developing laravel project at one shot! Please see all component in ‘vendor’ directory. Before I try to give a shot on this new installation, I need to set some permission on a few directories suggested on the laravel doc like ‘storage’ and ‘bootstrap/cache’.

    set_permissionNow, I am ready to try it on my web browser. Many article suggest to setup a virtual host but I want to test it right away without it. I can use PHP CLI to do that. Here is the command on my linux terminal.

    PHP CLI is running.

    php_cli_runningOpen it on the web browser with url:

    Here is the welcome screen.

    laravel welcome screen
    laravel welcome screen

    The another way is to use ‘Laravel Installer’. From this stackoverflow QA, this way seems to download just required ‘vendor’ component/bundle. It may be for the expert level that needed more thought about laravel (CMIIW). I hope I can learn it later.

  3. Setup the virtualhost. after the successful first try, I think it need to setup a virtualhost config so I dont need to run/stop the PHP CLI on my linux terminal everytime when I want to give it a try. First, I need to create a new apache configuration file. I name it ‘mylaravel.conf’.

    apache_conf_fileHere is the file content.

    Make sure the ‘Document root’ path is written correctly. the entry ‘Require all granted’ is required here or I would get an error like this: ‘Forbidden You don’t have permission to access / on this server’. Then I need apache to enable the site and reload it.

    Here is the process.

    ‘mylaravel’ on ‘sudo a2ensite mylaravel’ is the config file name without .conf extension. then I need to register the site so I can open it through localhost with this command ‘sudo gedit /etc/hosts’.

    then add this entry in the ‘hosts’ file.

    then restart the apache server ‘sudo service apache2 restart’. Now I can open it through my web browser with this url: http://mylaravelproject.dev/
  4. The next step is to learn more about laravel like Application Structure, Configuration, Router, etc. This tutorial Laravel Tutorial and Belajar Laravel Ala Indonesia are worth to be read. I hope I can write my experience on this blog someday.