laravel interview questions
Top laravel frequently asked interview questions
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 queries executed.
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)
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)
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)
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)
Is there a way to "limit" the result with ELOQUENT ORM of Laravel?
SELECT * FROM `games` LIMIT 30 , 30
And with Eloquent ?
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)
I want to sort for multiple columns in Laravel 4 by use method orderBy()
of Eloquent. The query will generate by Eloquent like this:
SELECT *
FROM mytable
ORDER BY
coloumn1 DESC, coloumn2 ASC
How can I do?
Source: (StackOverflow)
This maybe a trivial question but I am wondering if Laravel recommends a certain way to check whether an Eloquent collection returned from $result = Model::where(...)->get()
is empty, as well as counting the number of elements.
We are currently using !$result
to detect empty result, is that sufficient? As for count($result)
, does it actually cover all cases, including empty result?
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'm on Ubuntu 14.04 and I've been trying all possible methods to install Laravel to no avail. Error messages everything I try. I'm now trying the first method in the quickstart documentation, that is, via Laravel Installer, but it says to "Make sure to place the ~/.composer/vendor/bin directory in your PATH so the laravel executable is found when you run the laravel command in your terminal." so my question is, how do I do that? This may be a simple question but I'm really frustrated and would appreciate any help.
Source: (StackOverflow)
I'd like to know if I can install or use the Laravel PHP framework on any webserver without using Composer (PHP package/dependency manager) every time?
- I am learning PHP and frameworks and I am working on a CMS based on Laravel for practice.
- I would like to be able to drop it onto any webserver without having to use composer every time.
If I run $ composer install
the first time (locally), then all the dependencies should be present, correct?
Then, I should be able to drop it onto any server with all of the files (including the vendor directory)?
Source: (StackOverflow)
I'm using the Laravel Eloquent query builder and I have query where I want a WHERE
clause on multiple things. It works, but it is not elegant.
Example:
$results = User::
where('this', '=', 1)
->where('that', '=', 1)
->where('this_too', '=', 1)
->where('that_too', '=', 1)
->where('this_as_well', '=', 1)
->where('that_as_well', '=', 1)
->where('this_one_too', '=', 1)
->where('that_one_too', '=', 1)
->where('this_one_as_well', '=', 1)
->where('that_one_as_well', '=', 1)
->get();
Is there a better way to do this, or should I stick with this method?
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'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)