Laravel Recursive Relationships

This is how you can use recursive relations: public function childrenAccounts() { return $this->hasMany(‘Account’, ‘act_parent’, ‘act_id’); } public function allChildrenAccounts() { return $this->childrenAccounts()->with(‘allChildrenAccounts’); } Then: $account = Account::with(‘allChildrenAccounts’)->first(); $account->allChildrenAccounts; // collection of recursively loaded children // each of them having the same collection of children: $account->allChildrenAccounts->first()->allChildrenAccounts; // .. and so on This way you save … Read more

Order By before Group By using Eloquent (Laravel)

I just needed to do something similar with a messages model. What worked for me was applying the unique method on the returned eloquent collection. Model::where(‘toId’, $id) ->orderBy(‘createdAt’, ‘desc’) ->get() ->unique(‘fromId’); The query will return all messages ordered by createdAt and the unique method will reduce it down to one message for each fromId. This … Read more

Laravel – Database, Table and Column Naming Conventions?

Laravel has its own naming convention. For example, if your model name is User.php then Laravel expects class ‘User’ to be inside that file. It also expects users table for User model. However, you can override this convention by defining a table property on your model like, class User extends Eloquent implements UserInterface, RemindableInterface { … Read more

Laravel Eloquent Relations: ->latest()

latest() is a function defined in Illuminate\Database\Query\Builder Class. It’s job is very simple. This is how it is defined. public function latest($column = ‘created_at’) { return $this->orderBy($column, ‘desc’); } So, It will just orderBy with the column you provide in descending order with the default column will be created_at.

Laravel $q->where() between dates

You can chain your wheres directly, without function(q). There’s also a nice date handling package in laravel, called Carbon. So you could do something like: $projects = Project::where(‘recur_at’, ‘>’, Carbon::now()) ->where(‘recur_at’, ‘<‘, Carbon::now()->addWeek()) ->where(‘status’, ‘<‘, 5) ->where(‘recur_cancelled’, ‘=’, 0) ->get(); Just make sure you require Carbon in composer and you’re using Carbon namespace (use Carbon\Carbon;) … Read more

Laravel 5 hasMany relationship on two columns

I don’t think it’s possible to do exactly what you are asking. I think you should treat them as separate relationships and then create a new method on the model to retrieve a collection of both. public function userRelations() { return $this->hasMany(‘App\UserRelation’); } public function relatedUserRelations() { return $this->hasMany(‘App\UserRelation’, ‘related_user_id’); } public function allUserRelations() { … Read more

Laravel Migration Change to Make a Column Nullable

Laravel 5 now supports changing a column; here’s an example from the offical documentation: Schema::table(‘users’, function($table) { $table->string(‘name’, 50)->nullable()->change(); }); Source: http://laravel.com/docs/5.0/schema#changing-columns Laravel 4 does not support modifying columns, so you’ll need use another technique such as writing a raw SQL command. For example: // getting Laravel App Instance $app = app(); // getting laravel … Read more