Tag Archives: laravel online course
Laravel Get Url Parameters
Laravel Get Url Parameters : If you are working with laravel, generally you need to access url parameter and their respective values. Laravel provides few simple methods to access url parameters and their values. We will explain the methods available in laravel which are useful in manipulating the url parameters. We will cover how to validate the url parameter whether it consists data or it is null.
Laravel Get Url Parameters Syntax
Here are examples and syntax to get Url parameters. Let us go step by step to learn the things easily –
Get Url Parameters
You can retreive url parameters as below –
Retrieve Url Parameter
$parameter_name = Input::get('parameter_name'); |
Where parameter_name is name of parameter you want to retrieve from url.
More About Url Parameters
Let’s have look over more example about url parameters here.
Set A default value and Retrieve It
It the value in url parameter is null you can set a default value for that parameter which will return defualt value in case the parameter retrieves null value.
Retrieve Url Parameter
$parameter_name = Input::get('parameter_name','default'); |
Will return default if the url parameter is null ie. absent.
Check if value isset/exists –
Set Default Input Parameter
$parameter_name = Input::get('parameter_name'); // returns true if value exists if($parameter_name){ } |
Get All Request Values –
If want to retrieve all values in request you can use following method as below-
Retrieve All Url Parameter
$all_params = Input::all(); // returns all params array |
it will return all parameters together.
Laravel session
Laravel session – : Let us have a look over session and how it works?
Breif about Session –
Sessions are used to store information on server side which are available throughout the application. Sessions are stored on server side but they are dependent on cookie as session ids are maintained on browser.
Laravel session provides the functionality to store information accross the application. You can store Laravel sessions in files or database.
Laravel Session – Set/Get Syntax And Example
Let us understand laravel session syntax with example-
Configurations
Session configuration file is config/session.php. Go and add the required parameters.
Here is config/session.php file with default settings-
session.php
session.php
return [ 'driver' => env('SESSION_DRIVER', 'file'), 'lifetime' => 120, 'expire_on_close' => false, 'encrypt' => false, 'files' => storage_path('framework/sessions'), 'connection' => null, 'table' => 'sessions', 'lottery' => [2, 100], 'cookie' => 'laravel_session', 'path' => '/', 'domain' => null, 'secure' => false, ]; |
- files – Files to store sessions located at – storage/framework/sessions
- Cookies – Stored at client side in encrypted form.
- Database table – Table to store sessions by default it is “sessions”
- Secure – By default it is false if you are working with https turn it as true.
- Memcached/Redis – Used for faster performance as it provides faster storage.
- Array – Are stored in variables only and not available throughout the application.
Create Session In Laravel –
Here is syntax to create session variable using the session function. put keyword is used to set the session variable.
Syntax
Session::put(Key, value); |
Where key is name of key you want to store the value.
Example
$userid = "98234324"; Session::put('userid', $userid); |
Retrive Value From laravel Session
Here is syntax to retrive session value –
Syntax
Session::get(Key, value); |
Example
$userid = Session::get('userid'); echo $userid; |
Which will print98234324
Delete Data from laravel session
forget keyword is used to delete data from sessions.
Here is syntax to delete data from session in laravel –
Syntax
Session::forget(key); |
Example
Session::forget('userid'); |
Which will delete userid from session.
Delete All Data From Session
If you want to clear all variables of session use flush() keyword.
Syntax
Session::flush(); |
Which will clear all session variables throughout the system.
Store Array Values in Session-
If you are working with array you can use following method to store values as –
Here is syntax to store array-
Syntax
Session::push(key,array); |
Where first parameter is key and second parameter is array.
Example
$data = array("name"=>"John","email"=>"john@example.com"); Session::push('userinfo',$data); |
Getting array data from session-
Example
$user = Session::get('userinfo'); echo $user[0]['name']." |
Updating Array Index In Session-
Use following method to update array index in session –
Example
Session::push('userinfo.name','Lee'); |
Which will update the array index name’s value as “Lee”
If want to read full documentation about laravel sessions you can refer laravel.com docs – http://laravel.com/docs/5.1/session
Laravel get last inserted Id
Laravel get last inserted Id : If you are working with laravel database tables and performing insert operation on them you can use the following method to get the last inserted id.
Syntax for Laravel get last inserted Id
Below is an insert query example in laravel.
DB::table('users')->insert(array("id"=>$uid,"name"=>$name,"email"=>$email)); $lastInsertedId = DB::connection('mysql')->pdo->lastInsertId();
Which will return the last inserted id in the laravel table.
Laravel Update Query
Laravel Update Query : Laravel Update Query Builder is used to update the existing records in a table.
update() is used to update the records in database table.
Syntax for Laravel Update Query
DB::table('table_name') ->where('column_name',value) ->update('data_array');
Where –
table_name : Name Of table
column_name: Name Of column and its corresponding value to match it.
data_array: Column Value pair to update the column value.
Laravel Update Query Example
$uid = 10; $name = "John"; DB::table('users')->update ->where("id",$uid) ->update(array("name"=>$name));
The above Query will produce the following Result
UPDATE users set name = “John” WHERE id = ’10’;
Get Current date time in laravel
Get Current date time in laravel : You can use datetime class to get current date time in laravel.
Get Current date time in laravel
$date = new DateTime(); echo $date->format('Y-m-d H:i:s');
The above method will give the current date time.
Laravel right join query example
Laravel right join query example : DB::table(‘table_name’)->rightJoin(‘condition’)->select(‘column_names’)->get(); is used for right join in laravel.
Laravel right join query example
DB::table('users') ->rightJoin('city','city.user_id','=','users.id') ->select('users.name','city.city_name') ->get();
Laravel left join query example
Laravel left join query example : DB::table(‘table_name’)->leftJoin(‘condition’)->select(‘column_names’)->get(); is used for join in laravel.
Laravel left join query example
DB::table('users') ->leftJoin('city','city.user_id','=','users.id') ->select('users.name','city.city_name') ->get();
Laravel Get Base Url
Laravel Get Base Url : You Can the get base url in laravel as below.
Laravel Get Base Url Syntax
$baseurl = url();
Which will the give base url of the application.
Laravel group by
DB::table(‘table_name’)->groupBy(‘column_name’)->get(); is used for group by condition in laravel.
laravel group by clause Example
We are going to fetch data with groupBy clause in laravel from the table emp.
DB::table('emp')->groupBy('manager_name')->get();
Will Generate the following query.
//Select * from emp group BY ‘manage_name’ ;