Input is Array Helper in laravel 4

Input is Array Helper

It is a very important idea in laravel 4 … for me, if you understand it you will find out that Laravel is a very easy to understand and customizable framework. Recently, I am doing some research on how laravel 4 working in validation. It is the most simple and common validation code that we always use.

Continue reading

Advertisements

Tips of validation in Laravel 4

Laravel provide a very helpful feature on validation. If you are still writing somethings like this…

if(Input::get("password") != Input::get("password_confirmation")){
     return Redirect::back()->with("error","password confirm is not matched");
}

I think it is time for you to understand how powerful Laravel is.

Confirmation

Mail, password confirmation can be done easily by validation rules.

$rules = array(
		"password" 				=> "required|confirmed",
		"password_confirmation"	=> "required",
	);

$validator = Validator::make(Input::all(), $rules);
<form action="post">
    <input type="password" name="password">
    <input type="password" name="password_confirmation">
</form>

For mail confirmation, replace password_confirmation to email_confirmation.

Unique value

Let’s say, “name_col” must be unique and there is no duplicate.

+----+-------------+
| id | name_col    |
+----+-------------+
|  1 | Steve       |
|  2 | John        |
+----+-------------+
$rules = array(
		"name_col" => required|unique:mytable",
	);

$validator = Validator::make(Input::all(), $rules);

Regular Expression

Another powerful feature in Laravel 4 validation is regular expression. Following sample show the Katagana regular expression.

$rules = array(
		'katagana'      => 'regex:/^[ァ-ヶー]+$/',
	);

$validator = Validator::make(Input::all(), $rules);

Custom Error Message

You would like to have your own error message, open app/lang/en/validation.php and configure. Let’s say, you want to modify “confirmation” error message.

"confirmed" => "Hey! :attribute is not matched", 

Quick guide on installation Laravel Homestead in Windows 8

Updated: 2014-12-5 Lastest version of Homestead is not working in this article. But it is still possible to install Homestead into your windows environment by checkout to old version of Homestead. I will show you how to do it in this article.


Latest version of Vagrant 1.6.3 and VirtualBox 4.3.14 seems to get working in Windows environment. In this article, I want to tell you that you are able to install Laravel Homestead in Windows within 20 minutes (15 minutes for downloading Vagrant box and time for toilet). Well, hope you are able to get every work in 20 minutes.

1. Download the required software

Download all the software on below, we required it when installing Homestead and Laravel

putty and puttygen both softwares are needed. You are able to download on same website, same page. After downloading, install VirtualBox and Vagrant.

2. Adding Homestead Vagrant box

After Vagrant is installed, open the command prompt and execute following commands to adding Homestead Vagrant Box. It take quite a long time to download. For me, I take almost 15 minutes to download.

Continue reading

Handle Table Relationships by Eloquent in Laravel 4

I summarize the things written in Laravel Docs so it almost the same things.

# One to One

In table below, every user has one phone. Column phone_id represent the data in phones and all phone_id stored in users table is unique and no repeat.

スクリーンショット 2014-08-21 午後7.48.58

In app/models/User.php, it should looks like this. In default, there is no User.php and you have to create by yourself.

class User extends Eloquent {

    public function phone()
    {
        // 1st parameter is eloquent class name
        // 2nd parameter is foreign key
        // 3rd parameter is local key in "users" table
        return $this->hasOne('User','user_id','id');
    }

}

For phones table

class Phone extends Eloquent {

    public function user()
    {
    	// 1st parameter is eloquent class name
        // 2nd parameter is foreign key
        // 3rd parameter is local key in "users" table
        return $this->belongsTo('User','user_id','id');
    }

}

In migrations file, it should looks like this.

Continue reading

Advance using of eloquent in Laravel 4

This article is going to show you some advance using of eloquent that didn’t show in laravel documentation.

“SELECT” Query

SELECT DISTINCT category FROM phones LIMIT 20

Phone::select(array("category"))->distinct()->take(20)->get();

SELECT id,name FROM phones ORDER BY created_at DESC

Phone::orderBy("created_at","desc")->get(array("id,name"));

SELECT * FROM phones WHERE status=”publish” AND name LIKE “John%”

Phone::select("*")->where("status","=","publish")->where("name","like","John%")->get()

SELECT * FROM phones WHERE (id BETWEEN 1 AND 100) OR (image not like ‘%iPhone%’);

Phone::whereBetween("id",array(1,10))->orWhere("image","not like", "%iPhone%")->get();

Quick guide on installation laravel homestead in OS X

A very quick guide on installation Laravel in your Vagrant within 5 minutes. I will be explained in very roughly, if your are looking for more details please checking laravel home page.

1. Installing VirtualBox & Vagrant

Download and install both VirtualBox and Vagrant if you are not installed yet.Both of it is required in running homestead.

2. Adding Homestead Vagrant box

Open terminal and execute following commands to adding Homestead Vagrant Box. It take quite a long time to download. For me, I take almost 20 minutes to download.

vagrant box add laravel/homestead

3. Clone The Homestead Repository

Clone the git repository by following commands.

git clone https://github.com/laravel/homestead.git Homestead

Continue reading