Thursday, September 5, 2013

Migrating From Laravel 3 to Laravel 4

I apologize that this page is not well organized. It mainly exists to serve as notes for myself. If you can make sense of this, great!

Changes I had to make to my code when updating from Laravel 3 to Laravel 4:

General
Auth::login(int) => Auth::attempt(array()) or Auth::login(User)
Request::current() => Request::path()
URI::segment(int) => Request::segment(int)
URL::to_route("X") => URL::route("X")
Request::route()->is("X") => Request::is("X")
Form::token() => included by default when calling Form::open() but still accessible
Form (method = POST) => default method when calling Form::open()
Form::open_for_files(...) => Form::open(array('files' => true, ...))
path(...) => storage_path() || public_path || app_path() || base_path()
DS (constant) => DIRECTORY_SEPARATOR
with_errors($x) => withErrors($x)
with_input($x) => withInput($x)
Response::error() => App::abort()
Request::ip() => Request::getClientIp()
DB::query => DB::select, DB::insert, DB::update, DB::delete, DB::statement

Blade
@layout(...) => @extends(...)
@render(...) => @include(...)
@forelse => GONE, use @if(count(...))  and @foreach()  [ this was nice while it lasted :( ]

Eloquent
belongs_to => belongsTo
has_many => hasMany
belongs_to_many => belongsToMany
has_one => hasOne
order_by => orderBy
group_by => groupBy
has_many_and_belongs_to => hasManyAndBelongsTo

Models
set_fieldx($val) {$this->field = $val;} => setFieldxAttribute($val) { $this->attributes['field'] = $val; }
get_fieldx(){return $this->field; } => getFieldxAttribute($val) { return $val; }
where_val() => whereVal()
or_where() => orWhere
where_in => whereIn
where_between => whereBetween (Note: 2nd argument should be array of min, max)
or_where_between => orWhereBetween (Note: 2nd argument should be array of min, max)
or_where_in => orWhereIn
where_not_in => whereNotIn
left_join => leftJoin
::hydrate() => newFromBuilder (read more)

Libraries
You can add app/libraries to the composer autoload json or
create package(s) using workbench and place your code in there.

No comments: