We Guarantee the Best and Free technical solutions
Some Usefull link in this Application will redirect your mind to a new techical world
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.
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 laravelAns: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
}
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']);
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
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});
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.
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.
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'];
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')
}
});
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
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
Ans: path method returns requestsURI ,For Example
incoming request from http://mydoubts.in/test/hello
$request->path() will return test/hello
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
}
Ans: To get the full URL, not just the path info, you may use the url method
$request->url();
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')) {
//
}
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
Ans: retrieve all the input data as an array using all method
$input = $request->all();
dd($input) will print the result as array.
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')) {
//
}
Ans: for this you can use only,except method
$input = $request->only(['username', 'password']);
$input = $request->except('credit_card');
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
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
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;
}
Ans: Eloquent 'all' method will return all of the results from the table,Example
use App\Employee;
$employees = App\Employee::all();
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();
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');
}
}
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();
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') }}
Ans: if you want to see a view file located at resources/views/mydata/myprofile.blade.php
return view('mydata.myprofile', ['name' => 'vasanthan']);
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')) {
}
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);
Ans: To retrieve all data in the session you can use the 'all' method
$data = $request->session()->all();
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
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');
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();
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();
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...
}
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;
}
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;
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();