laravel
A PHP Framework For Web Artisans
Laravel - The PHP Framework For Web Artisans laravel - the php framework for web artisans.
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: http://laravel.com/docs/eloquent#relationships.
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)
It's a Laravel-install related question. I have a public-facing Unix server setup:
<VirtualHost *:80>
ServerAdmin webmaster@mydomain.org
DocumentRoot "/var/www/mydomain"
ServerName mydomain.org
ServerAlias www.mydomain.org
ErrorLog "/var/log/mydomain.org-error_log"
CustomLog "/var/log/mydomain.org-access_log" common
</VirtualHost>
I can serve documents fine out of /var/www/mydomain i.e. http://mydomain.org/test.php 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 CONTRIBUTING.md artisan composer.json phpunit.xml readme.md vendor
.gitignore app bootstrap composer.lock public server.php
So when I browse to:
http://mydomain.org/my-laravel-project/public/
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)
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.
<?php
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:
<?php
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:
<?php
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.
Help?
Clearly I need to rethink things a little when working with repositories. Can anyone enlighten on how this is best handled?
Source: (StackOverflow)
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?
SELECT * FROM `games` LIMIT 30 , 30
And with Eloquent ?
Source: (StackOverflow)
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:
2013_05_31_220658_create_users_table
Source: (StackOverflow)
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
inate\\Database\\Migrations\\Migrator.php","line":301}}
Is this a bug? or how should i debug this?
Source: (StackOverflow)
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)
Given the following code:
DB::table('users')->get();
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)
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)
I am pretty new to Laravel4 and Composer. While I do laravel 4 tutorials, I couldn't understand between those two commands; php artisan dump-autoload
and composer dump-autoload
What are difference between them ??
Source: (StackOverflow)
How can I get the executed query in Laravel 3/4, using Fluent Query Builder or Eloquent ORM.
For example:
DB::table('users')->where_status(1)->get();
Or (posts (id, user_id, ...))
User::find(1)->posts->get();
Or... how I can save in log, all querys executed.
Source: (StackOverflow)
I'd like to be able to add a custom attribute/property to an Laravel/Eloquent model when it is loaded, similar to how that might be achieved with RedBean's $model->open()
method.
For instance, at the moment, in my controller I have:
public function index()
{
$sessions = EventSession::all();
foreach ($sessions as $i => $session) {
$sessions[$i]->available = $session->getAvailability();
}
return $sessions;
}
It would be nice to be able to omit the loop and have the 'available' attribute already set and populated.
I've tried using some of the model events described in the documentation to attach this property when the object loads, but without success so far.
Notes:
- 'available' is not a field in the underlying table.
$sessions
is being returned as a JSON object as part of an API, and therefore calling something like $session->available()
in a template isn't an option
Source: (StackOverflow)
I've recently read about namespaces and how they are beneficial.
I'm creating a project in Laravel and was trying to move from class map autoloading to namespacing.
I can't seem to grasp what the actual difference is between PSR-0 and PSR-4.
Some resources that I've read from are
What I understand:
- PSR-4 does not convert underscores to directory separators
- Certain specific rules of composer cause the directory structure to become complex which in turn makes PSR-0 namespacing verbose and thus PSR-4 was created
Examples explaining the difference would be appreciated.
Source: (StackOverflow)
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)