laravel-4 interview questions
Top laravel-4 frequently asked interview questions
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)
I'm stuck on a simple task.
I just need to order results coming from this call
$results = Project::all();
Where Project
is a model. I've tried this
$results = Project::all()->orderBy("name");
But it didn't work. Which is the better way to obtain all data from a table and get them ordered?
Source: (StackOverflow)
I need to create a new "auth" config with another table and users. I have one table for the "admin" users and another table for the normal users.
But how can I create another instance of Auth
with a different configuration?
Source: (StackOverflow)
I want to rename a table in Laravel 4, but don't know how to do that.
The SQL is alter table photos rename to images
. If there is an Eloquent solution, I'd also like to know how to run a raw SQL, cause sometimes there's just no alternative.
Source: (StackOverflow)
In Laravel 3, you could call a controller using the Controller::call method, like so:
Controller::call('api.items@index', $params);
I looked through the Controller class in L4 and found this method which seems to replace the older method: callAction(). Though it isn't a static method and I couldn't get it to work. Probably not the right way to do it?
How can I do this in Laravel 4?
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)
How do you manage assets in the new Laravel 4? It looks like Taylor Otwell has replaced Asset::add
with something new.
What I am trying to do is have Laravel add my CSS and JS files. There were several ways to do this in Laravel 3, but they seem to be gone now. One was Asset::add
and the other was HTML
. What are the replacements for these?
Source: (StackOverflow)
I have a Laravel 4 project, and I would like to know which files should be ignored when using a version control software such as Git, Mercury or SVN?
The structure of my project looks like the following screen capture.
I'm pretty new to Composer so I'm not very clear about what goes to a repo what not. If someone can post their .gitignore
file or their SVN ignore property, it could be handy.
Source: (StackOverflow)
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?
Source: (StackOverflow)
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)
This question already has an answer here:
Simple question - how do I order by 'id' descending in Laravel 4.
The relevant part of my controller looks like this:
$posts = $this->post->all()
As I understand you use this line:
->orderBy('id', 'DESC');
But how does that fit in with my above code?
Source: (StackOverflow)
I'm trying to use the Mail Class in Laravel 4, and I'm not able to pass variables to the $m object.
the $team object contains data I grabbed from the DB with eloquent.
Mail::send('emails.report', $data, function($m)
{
$m->to($team->senior->email, $team->senior->first_name . ' '. $team->senior->last_name );
$m->cc($team->junior->email, $team->junior->first_name . ' '. $team->junior->last_name );
$m->subject('Monthly Report');
$m->from('info@website.com', 'Sender');
});
For some reason I get an error where $team object is not available. I suppose it has something to do with the scope.
Any ideas ?
Source: (StackOverflow)
What is the right way to remove a package from Laravel 4?
So long I've tried:
- Remove declaration from
composer.json
(in "require" section)
- Remove any Class Aliases from
app.php
- Remove any references to the package from my code :-)
- Run
composer update
- Run
composer dump-autoload
Not working! Am I missing something?
Some packages publish their configuration via "artisan config:publish ...". Is there a way to "unpublish" them?
Source: (StackOverflow)
This seems like a pretty basic flow, and Laravel
has so many nice solutions for basic things, I feel like I'm missing something.
A user clicks a link that requires authentication. Laravel's auth filter kicks in and routes them to a login page. User logs in, then goes to the original page they were trying to get to before the 'auth' filter kicked in.
Is there a good way to know what page they were trying to get to originally? Since Laravel is the one intercepting the request, I didn't know if it keeps track somewhere for easy routing after the user logs in.
If not, I'd be curious to hear how some of you have implemented this manually.
Source: (StackOverflow)
I believe that I've successfully deployed my (very basic) site to fortrabbit, but as soon as I connect to SSH to run some commands (such as php artisan migrate
or php artisan db:seed
) I get an error message:
[PDOException]
SQLSTATE[HY000] [2002] No such file or directory
At some point the migration must have worked, because my tables are there - but this doesn't explain why it isn't working for me now.
Source: (StackOverflow)