A PHP Framework For Web Artisans Laravel - The PHP Framework For Web Artisans laravel - the php framework for web artisans.

How to get the current URL inside @if statement (blade) in Laravel 4?

I am using laravel 4. I would like to access to the current URL inside an @if condition in a view using the Laravel's Blade templating engine but I don't know how to do it.

I know that it can be done using something like <?php echoURL::current(); ?> but It's not possible inside an @if blade statement.

Any suggestion?

Source: (StackOverflow)

How to use order by for multiple columns in laravel 4?

I want to sort for multiple columns in Laravel 4 by use method orderBy() of Eloquent. The query will generate by Eloquent like this:

FROM mytable
  coloumn1 DESC, coloumn2 ASC

How can I do?

Source: (StackOverflow)

Best practices for custom helpers on Laravel 5

I would like to create some helpers (functions) to avoid repeating code between some views, in L5 style:


<p>Foo Formated text: {{ fooFormatText($text) }}</p>

They are basically text formatting functions. Where and how can I put a file with these functions?

Source: (StackOverflow)

What are the best practices and best places for laravel 4 helpers or basic functions?

So I'm trying to understand the best place to put a global function in laravel 4. For example: date formatting. I don't think making a facade is worth it, facades are too modular. I've read articles about creating a library folder and storing classes there, but that also seems like a lot for a simple function. Also, shouldn't a 'tool' like this be available in blade templates?

What are the best practices for something like this? And how do i make it available to the blade templates?

Source: (StackOverflow)

Laravel requires the Mcrypt PHP extension

I am trying to use the migrate function in Laravel 4 on OSX, however I am getting the error Laravel requires the Mcrypt PHP extension.

As far as I understand, it's already enabled (see the image below).

What is wrong, and how can I fix it?

enter image description here

Source: (StackOverflow)

Managing relationships in Laravel, adhering to the repository pattern

While creating an app in Laravel 4 after reading T. Otwell's book on good design patterns in Laravel I found myself creating repositories for every table on the application.

I ended up with the following table structure:

  • Students: id, name
  • Courses: id, name, teacher_id
  • Teachers: id, name
  • Assignments: id, name, course_id
  • Scores (acts as a pivot between students and assignments): student_id, assignment_id, scores

I have repository classes with find, create, update and delete methods for all of these tables. Each repository has an Eloquent model which interacts with the database. Relationships are defined in the model per Laravel's documentation:

When creating a new course, all I do is calling the create method on the Course Repository. That course has assignments, so when creating one, I also want to create an entry in the score's table for each student in the course. I do this through the Assignment Repository. This implies the assignment repository communicates with two Eloquent models, with the Assignment and Student model.

My question is: as this app will probably grow in size and more relationships will be introduced, is it good practice to communicate with different Eloquent models in repositories or should this be done using other repositories instead (I mean calling other repositories from the Assignment repository) or should it be done in the Eloquent models all together?

Also, is it good practice to use the scores table as a pivot between assignments and students or should it be done somewhere else?

Source: (StackOverflow)

Error in exception handler. - Laravel

It's a Laravel-install related question. I have a public-facing Unix server setup:

<VirtualHost *:80>
DocumentRoot "/var/www/mydomain"
ErrorLog "/var/log/"
CustomLog "/var/log/" common

I can serve documents fine out of /var/www/mydomain i.e. with test.php containing:

<?php echo 'test';

works fine.

In bash, with Laravel installed through Composer and looking at the files:

# ls /var/www/mydomain/my-laravel-project

.gitattributes artisan         composer.json   phpunit.xml       vendor
.gitignore      app             bootstrap       composer.lock   public          server.php

So when I browse to:

why does my application report:

Error in exception handler. 

in the browser - on a blank white screen? I'm expecting to see the Laravel splash screen.

Moreover, the log files don't reveal anything either.

Source: (StackOverflow)

Proper Repository Pattern Design in PHP?

Preface: I'm attemping to use the repository pattern in a MVC architecture with relational databases.

I've recently started learning TDD in PHP, and I'm realizing that my database is coupled much too closely with the rest of my application. I've read about repositories, and using an IoC container to "inject" it into my controllers. Very cool stuff. But now have some practical questions about repository design. Consider the follow example.


class DbUserRepository implements UserRepositoryInterface
    protected $db;

    public function __construct($db)
        $this->db = $db;

    public function findAll()

    public function findById($id)

    public function findByName($name)

    public function create($user)

    public function remove($user)

    public function update($user)

Issue #1: Too many fields

All of these find methods use a select all fields (SELECT *) approach. However, in my apps I'm always trying to limit the number of fields I get, as this often adds overhead and slows things down. For those using this pattern, how do you deal with this?

Issue #2: Too many methods

While this class looks nice right now, I know that in a real world app I need a lot more methods. For example:

  • findAllByNameAndStatus
  • findAllInCountry
  • findAllWithEmailAddressSet
  • findAllByAgeAndGender
  • findAllByAgeAndGenderOrderByAge
  • Etc.

As you can see, there could be very, very long list of possible methods. And then if you add in the field selection issue above, the problem worsens. In the past I'd normally just put all this logic right in my controller:


class MyController
    public function users()
        $users = User::select('name, email, status')->byCountry('Canada')->orderBy('name')->rows()

        return View::make('users', array('users' => $users))


With my repository approach, I don't want to end up with this:


class MyController
    public function users()
        $users = $this->repo->get_first_name_last_name_email_username_status_by_country_order_by_name('Canada');

        return View::make('users', array('users' => $users))


Issue #3: Impossible to match an interface

I see the benefit in using interfaces for repositories, so I can swap out my implementation (for testing purposes or other). My understanding of interfaces is that they define a contract that an implementation must follow. This is great until you start adding additional methods to your repositories like findAllInCountry(). Now I need to update my interface to also have this method, otherwise other implementations may not have it, and that could break my application. By this feels insane...a case of the tail wagging the dog.

Specification Pattern?

This leads me to believe that repository should only have a fixed number of methods (like save(), remove(), find(), findAll(), etc). But then how do I run specific lookups? I've heard of the Specification Pattern, but it seems to me that this only reduces an entire set of records (via IsSatisfiedBy()), which clearly has major performance issues if you're pulling from a database.


Clearly I need to rethink things a little when working with repositories. Can anyone enlighten on how this is best handled?

Source: (StackOverflow)

Add new methods to a resource controller in Laravel 4

I want to know if it is possible to add new methods to a resource controller in Laravel 4 and how you do it.

I know that this methods are the default ( index, create, store, edit, update, destroy ).

I am confused, any ideas, examples ?

Source: (StackOverflow)

Is there a way to "limit" the result with ELOQUENT ORM of Laravel?

Is there a way to "limit" the result with ELOQUENT ORM of Laravel?

 SELECT * FROM  `games` LIMIT 30 , 30 

And with Eloquent ?

Source: (StackOverflow)

Safely remove migration In Laravel 4

In Laravel 4, there appears to be a command for creating a migration, but not removing.

Create migration command:

php artisan migrate:make create_users_table

If I want to delete the migration, can I just safely delete the corresponding migrations file within the database/migrations folder?

Migrations file:


Source: (StackOverflow)

Laravel 4 migrate rollback problems

I can easily run the artisan migrate etc, but when i try to roll it back, with migration:rollback i keep getting this error,

c:\xampp\htdocs\laravel>php artisan migrate:rollback
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Class 'CreateCodesnippetsTable' not found","file":"C:\\xampp\\htdocs\\laravel\\vendor\\laravel\\framework\\src\\Illum

Is this a bug? or how should i debug this?

Source: (StackOverflow)

Laravel's Blade: how can I set variables in a template?

I'm reading Laravel Blade's templating docs and I can't find how I can assign variables inside a template for use later in the template. I can't do {{ $old_section = "whatever" }} because that will echo "whatever" and I don't want that.

I see that I can do <?php $old_section = "whatever"; ?>, but that's not elegant.

Is there an elegant way to do that in Blade?

Source: (StackOverflow)

How do I get the query builder to output its raw SQL query as a string?

Given the following code:


I want to get the raw SQL query string that the query builder above will generate, so in this example it would be SELECT * FROM users.

How do I do this?

Source: (StackOverflow)

Laravel 4 eloquent WHERE with OR AND OR?

How do I say WHERE (a=1 OR b=1) AND (c=1 OR d=1)

For more complicated queries am I supposed to use raw SQL?

Source: (StackOverflow)