We Guarantee the Best and Free technical solutions

Some Usefull link in this Application will redirect your mind to a new techical world

Admininterviewquestions in laravel

1. What is Application key in Laravel

Ans:It is a 32 characters length random string. this is generating while installing laravel, and it is saved into .env file.we can generate new key using the command
php artisan key:generate
note:if key is not set,user session data encrypted data will not be secure.

2. What is .env file in laravel

Ans:Fresh installation of laravel will create .env.example file in the root directory of the application.if we install laravel through composer it will create .env file instead of .env.example.generally .env file will kept out of version controll because it contain sensitive information like API key and passwords.

3. How to identify current application environment in laravel

Ans:Laravel application environment is determined using APP_ENV variable of .env file.we can access this value using the following command
$environment = App::environment();
For example
if (App::environment('development')) {
// its development environment
}
if (App::environment(['development', 'testing'])) {
// its testing or development
}

4. Usage of config helper function in laravel

Ans:.its a global function we can access the configuration file value using dot operator.It includes the name of the file and the option we need to access.
Example:
To get the value :: config('app.timezone');
To set the configuration value:: config(['app.timezone'=>'America/Chicago']);

5. Maintenance mode in laravel

Ans:To enable maintenance mode we can use the command
php artisan down
This will throw 503 maintenance exception this is handled by defauly middleware.
adding message in the maintenance mode using the below command
php artisan down --message="site is in maintenance mide" --retry=60
to disable maintenance mode we can use the below command -
php artisan up

6. What is Routes? what are the available router methods.

Ans: Routes files are automatically loaded by the framework.routes/web.php defines the routes for web interface. following are the available router methods
Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);
For responding multiple http verbs or all http verbs we can use the below methods
Route::match(['get', 'post'], '/', function () { // multiple});
Route::any('foo', function () { //all});

7. Use of Route::redirect()

Ans: one route function redirect to another uri we can use Route::redirect() method. For example
redirect to http://test.com/second to http://test.com/first we can use the route function
Route::redirect('/second', '/first', 301);
This is convenient only when do not have to define full route or controller for simple redirect.

8.What is CSRF

Ans: Laravel automatically generates a CSRF token for each active user session.If you define an html form for posting data,you have to include hidden csrf field. Example add the below code inside a form
{{ csrf_field() }}
VerifycsrfToken middleware will automatically verify the token in the request input match with token stored in the session.

9.How to Exclude URI from the CSRF protection

An: If you want to exclude csrf protection mostly this will be usefull for stripe,paypal payments.
we can add $except property of verifyCsrfToken middleware.
Example
namespace App\Http\Middleware;
use Closure;
use Redirect;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
class VerifyCsrfToken extends BaseVerifier
{
/**

* The URIs that should be excluded from CSRF verification.
*

* @var array
*/

protected $except = ['employee/paypal'];

10.What is X-CSRF-TOKEN

Ans: verifycsrfToken middleware will check csrf token as request header instead of post parameter.
store the csrf token in meta tag,
meta name="csrf-token" content="{{ csrf_token() }}"
once created meta, you can instruct jquery to automatically add token to all request headers.
one more method for adding x-csrf-token in ajax based application as like below
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});

11.Route Caching in Laravel

Ans: if you are using controller based routes,you can use the below command to generate route cache
php artisan route:cache
This will decrease the amount of time required register all the routes.
Please note if you add new routes,you need to generate fresh route cache

12. What is Resource Controller

Ans: Resource routes assign a typical 'CRUD' routes inside a controller using the single line of command
php artisan make:controller EmployeeController --resource
here the Employeecontroller will contains method for each available resource operations

13. path method path->(); in Laravel

Ans: path method returns requestsURI ,For Example
incoming request from http://mydoubts.in/test/hello
$request->path() will return test/hello

14. Use of request->is in Laravel

Ans: is method allows you to verify that incoming request uri matches a given pattern.you can use * as wild character in this search
if ($request->is('superadmin/*')
{

//condtions
}

15. how to get full URL

Ans: To get the full URL, not just the path info, you may use the url method
$request->url();

16. How to identify the Request method or Request type

Ans: using the 'method' will get the type of method
$request->method();
using 'isMethod' to verify http verbs matches a string
if ($request->isMethod('post')) {
//
}

17. How Retrieving an input value in Laravel

Ans: using an instance of Illuminate\Http\Request,Example
$FirstName = $request->input('firstname');
one more method is $name = $FirstName->firstname;
In both the case no need to worry about the verbs used in the http request(post or get methods)
In the below method
$name = $request->input('name', 'Vasanthan'); if the name field is blank then default value second parameter(here vasanthan) will be returned

18. How to Retrieve all input data in laravel

Ans: retrieve all the input data as an array using all method
$input = $request->all();
dd($input) will print the result as array.

19. How to access array input using $request->input method

Ans: $firstname = $request->input('employeelist.0.firstname');

20. How to check value is present in the input request

Ans: you can use 'has' method to determine whether value is present in the request.it return true if have value
if ($request->has('firstname')) {
//
}

21. Retrieve portion of the input data

Ans: for this you can use only,except method
$input = $request->only(['username', 'password']);
$input = $request->except('credit_card');

22. Command used to download the file in laravel

Ans: return response()->download($pathToFile, $name, $headers);
here first parameter is the pathofthe file,second parameter name of the file, third parameter headers can be passed as an array of headers also

23. What is Eloquent ORM

Ans: Its a simple Activerecord implementation for working with database.Each databse table has corresponding Models.This model file used to interact with the database table.Models allows you to query and insert data into the table.

24. Command for creating Models

Ans: php artisan make:model Employee
here Employe model is created using php artisan command,you would like to migrate database while creating model use the below command
php artisan make:model Employee --migration
php artisan make:model Employee -m

25. Use of setting $timestamps=false in Model

Ans: By default, Eloquent expects created_at and updated_at columns to exist on your tables,if you do not want to insert records into this fields, you can use the following code
class Employee extends Model
{
public $timestamps = false;
}

26. How to Retrieve all models data

Ans: Eloquent 'all' method will return all of the results from the table,Example
use App\Employee;
$employees = App\Employee::all();

27. commands use to retrieve single record

Ans: find,first method

28. How to Delete a table record?

Ans: call a delete method on a model instance
For example,
$employees = App\Employee::find(1);
$employees->delete();

29. Creating Redirects in laravel

Ans: redirects are used to redirect the url to another location.redirect response are instance of Illuminate\Http\RedirectResponse. Proper headers are needed to redirect url.
simplest method is use the redirect helper For example
In the controller Employee controller
use redirect;
class EmployeeController extends Controller {
mytestfunction()
{
return redirect('home/dashboard');
}
}

30. Use of return back() in laravel

Ans: If the submitted form is invalid then you may wish to send back to the previous location with input values
Example :return back()->withInput();

31. Give an example redirect with flash data

Ans: if you use the below code it will redirect to new url and flashing data to the session at same time
Example
return redirect('myhomepage')->with('status', 'welcome mydoubts.in Technical blog!');
in the view blade we can display the message as
{{ session('status') }}

32. How to display a view page from a controller

Ans: if you want to see a view file located at resources/views/mydata/myprofile.blade.php
return view('mydata.myprofile', ['name' => 'vasanthan']);

33. How to check a view file exist?

Ans: you can use exists method to find out whether that filename exists, for this you can use 'view' facade
use Illuminate\Support\Facades\View;
if (View::exists('mydata.myprofile')) {
}

34. Use of View::composer

Ans: if u want to pass data to multiple pages at a time then we can use the view::composer, Example
View::composer('employee_module.employee_module_sidebar',function($view)use($employeeDetails){
return $view->with('employeeDetails', $employeeDetails);
});
return view('employee_module.employee_home', $data);

35. How to retrieve all session data

Ans: To retrieve all data in the session you can use the 'all' method
$data = $request->session()->all();

36. Differenece between $request->session()->has() and $request->session()->exists()

Ans: Both functions are using for value is present in the session.
'has' method returns true if the value is present and is not null
'exists' method To determine if a value is present in the session, even if its value is null

37. How to store data into the session

Ans: for storing data into the session you can use put method or session helper
For Example,
using request instance- $request->session()->put('myusername', 'testuser');
using global helper - session(['myusername' => 'testuser']);
one more method is
Session::put('myusername', 'testuser');

38. Deleting Data from the session

Ans: forget method to remove piece of data from the session
$request->session()->forget('myusername');
flush method to remove all data from the session
$request->session()->flush();

39. What is session regeneration

Ans: To prevent session fixation attack in the application, session regeneration is using.
Laravel automatically generating the sessionid during the authendication if we are using the default login controller. To generate manually you can use the below command
$request->session()->regenerate();

40. How to determine current user is Authenticated?

Ans: for checking the user is already logged into the application using the check method
Auth::check() will return true if the user is authenticated
Example:
if (Auth::check()) {
// The user is logged in...
}

41.What is the return result of an update query in laravel

Ans: The number of rows affected by the statement will be returned

42. What is the use of pluck()

Ans: pluck() method is used to retrieve a collection containing values of a single column.
Example:
$details = DB::table('tbl_employees')->pluck('first_name');
foreach ($details as $first_name) {
echo $first_name;
}

43. What is the use of first() ?

Ans: If you need to retrieve single row from the database table you can use the first() method
Example:
$details = Employees::select('emp_name') ->where('emp_id', $id)->first();
echo $details->emp_name;

44. use of DB::raw in Laravel

Ans: Some situations you may need to use raw expression in a query.this expressions can be used in the query as strings
For Example see the below sample query
$sqlResult = DB::table('tbl_employee')
->select(DB::raw('count(*) as tot_salary'))
->where('emp_id', '=', $emp_id)
->get();