vendredi 2 septembre 2016

Trying to reuse code in API and Non Api Controller

I have two controllers. One method is common in both controller. Store

Let me show both controllers.

Api Controller

class RoleApiController extends Controller
{
    private $Role;

    public function __construct(IRole $_role) {
        $this->Role = $_role;
    }

    private function InitializeInputs(RoleRequest $request) {
        if($request["ParentRoleID"] == -1) {
            $request["ParentRoleID"] = null;
        }
    }

    public function store(RoleRequest $request)
    {
        $this->InitializeInputs($request);

        $obj = new RoleOM();
        $obj->Role          = $request["Role"];
        $obj->ParentRoleID  = $request["ParentRoleID"];
        $obj->Active      = !is_bool($request["Active"]) ? false : $request["Active"];

        $result = $this->Role->Create($obj);

        return  \Response::json([
            'Status'   => $result["Success"],
            'Message'  => $result["ErrorMessage"]
        ]);
    }
}

Below is the controller for blade

class RoleController extends \App\Http\Controllers\BaseController
{
    private $Role;

    public function __construct(IRole $_role) {
        $this->Role = $_role;
        parent::__construct();
    }

    private function InitializeInputs(RoleRequest $request) {
        if($request["ParentRoleID"] == -1) {
            $request["ParentRoleID"] = null;
        }
    }

    public function store(RoleRequest $request)
    {
        $this->InitializeInputs($request);

        $obj = new RoleOM();
        $obj->Role          = $request["Role"];
        $obj->ParentRoleID  = $request["ParentRoleID"];
        $obj->Active      = !is_bool($request["Active"]) ? false : $request["Active"];

        $result = $this->Role->Create($obj);

        return  \Response::json([
            'Status'   => $result["Success"],
            'Message'  => $result["ErrorMessage"]
        ]);
    }
}

Question

If I use Trait: I don't need to write the action methods. So Controller will have no such method called Trait.

If I use class and define a method there, in that case, I will instantiate the class method in both controller's action method.

Please suggest.



via Chebli Mohamed

pagination in laravel 5.3 when count of pages = 1

I have cities table and it has 7 cities I have a view page to display these cities with 10 cities per page

Controller:

    $cities = City::orderBy('id', 'desc')->paginate(10);
    return view('cities.home', compact('cities'));

View:

<div class="table-responsive panel panel-sky">
                   <table class="table table-striped table-hover">
                       <tr>
                          <th>#</th>
                          <th>Name</th>
                          <th>Created At</th>
                          <th>Action</th>
                       </tr>

                       @foreach($cities as $city)
                          <tr id="product">
                             <td></td>
                             <td></td>
                             <td></td>
                             <td>  
                                <a href=""><i class="glyphicon glyphicon-edit action-icon"></i></a>
                                &nbsp;&nbsp;&nbsp;&nbsp;
                                <a type="button" class="pointer" data-toggle="modal" data-target="#deleteModal" data-type="btn-danger" data-action="delete-product" data-id="" data-msg="Are you sure you want to delete this city?" data-title="Confirm Deletion">
                                    <span class='glyphicon glyphicon-remove action-icon'></span>                                                           
                                </a>
                             </td>
                          </tr>
                       @endforeach
                   </table>
               <div>

               <div class="col-md-12"><?php echo $cities->render(); ?> </div>  

but the issue is the paginate render is displayed with page#1, this issue occurred in laravel5.3 and did not find in laravel5.2

enter image description here



via Chebli Mohamed

How to configure & run multiple websites with respective databases with single Laravel code instance

I have a scenario that I want to implement in Laravel.

I have already implemented this kind of approach in custom PHP. But this time, I want to shift all websites to a framework (Laravel).

I have multiple domains for different regions implemented (Localization), each domain has its own database and all database tables structure are same only the data is different.

Now I want to use single Laravel code instance with the ability to connect multiple databases with multiple domains and each domain has its own theme files.

Let suppose: I have some domains

 1. abc.com and it has database with name of db_abc with theme/template ABC
 2. xyz.com and it has database with name of db_xyz with theme/template XYZ

When abc.com domain is hit/accessed, I want to connect with DB db_abc and load data in ABC theme/template. Similar when xyz.com is hit/accessed the database connection should be made with db_xyz and data should be loaded into theme/template XYZ files.

Please help me in this regard.

Thank you.



via Chebli Mohamed

jeudi 1 septembre 2016

Can't append ajax in laravel for cascading for dinamic dropdown in laravel 5.1

i want to create dinamic dropdown in laravel 5.1 with ajax.. please help me

my routes.php

use App\users_groups;

Route::group(['middleware'=>'auth'],function(){

Route::get('/', 'HomeController@index');

Route::get('home', 'HomeController@index');

Route::resource('users', 'UsersController');

Route::resource('instalasis', 'InstalasisController');
Route::resource('groups', 'GroupsController');
Route::resource('parameters', 'ParametersController');

Route::resource('forms', 'FormsController');`

**Route::get('ajax-subcat',function(){
    $ins_code = Input::get('instalation_code');
   $usergrup = users_groups::where('master_instalation_code','=',$ins_code)->lists('group_name','group_code');
   return Response::json([$usergrup]);
 });**
 });

route for my dropdown is ajax-subcat

and then this is my view

   <div class="form-group">
   <label class="col-md-3 control-label" for="example-hf-email">Instalasi</label>
   <div class="col-md-9">
   {!! Form::select('master_instalasion_code',$formins,null,['class'=>'form-control','id'=>'instalation']) !!}

   <span class="help-block">isikan Group</span>
   </div>
   </div>
   <div class="form-group">
   <label class="col-md-3 control-label" for="example-hf-email">Group / Laporan</label>
   <div class="col-md-9">
   {!! Form::select('user_group_code',['placeholder'=>'Entry..'],null,['class'=>'form-control','id'=>'instalation']) !!}
  <span class="help-block">isikan Group</span>
  </div>
  </div>

and this my ajax for retrieving data

                             <script>
                                $('#instalation').change(function(e){
                                    console.log(e);

                                    var ins_code = e.target.value;

                                    $.get('../ajax-subcat?instalation_code='+ ins_code,function(data){

                                       $('#riskyy').empty();

                                        $.each(data, function(key, value) {
                                    console.log(data);

                                          $('#riskyy')
                                          .append($("<option></option>")
                                            .attr("value",key)
                                            .text(value));

                                        });

                                    });


                                });

                            </script>

when i get data from id instalation i get json data like this

 [{"03":"Jartes","076":"DAta"}]

the data is true, but i can't to show it in select form



via Chebli Mohamed

Database is not responding until all requests are submitted successfully: MySQL

I am using Laravel 5.3 for Web API and below is my JQuery Ajax request which saves records concurrently. Below is the code..

for (i = 0; i < 100; i++) {
    var 
     data={'Role' : "Role"+i},
     request = $.ajax({
        url:                'http://localhost:1234/Practise/public/api/SaveRoleApi',
        type:               "POST",
        data:               JSON.stringify(data),
        contentType:        "application/json; charset=utf-8",
        async:  true,
        success: function(d){ console.log(d); }
     });
}

I am using for loop to save 100 records. Please check there is async: true,. Reason I said it is saving records concurrently because the requests are being submitted randomly although I confirmed that all requests are being submitted successfully.

My Question is: I can't submit select statement select * from tblrole until all the above ajax requests are submitted.

Problem is: Database is not responding until all requests has been submitted.

What should I do to retrieve records during the Post request(s) submission is in progress?



via Chebli Mohamed

Connect SQL Server with PHP: Laravel 5.3

Inside config\database.php below setting is changed from mysql to sqlsrv

'default' => env('DB_CONNECTION', 'sqlsrv'),

In connection list added the below code.

'sqlsrv' => [
    'driver' => 'sqlsrv',
    'host' => 'Pankaj\SQLEXPRESS', 
    'database' => '',
    'username' => 'sa',
    'password' => 'pankaj',
    'prefix' => '',
]

Inside .env file below are the modifications for Sql Server

DB_CONNECTION=sqlsrv
DB_HOST=Pankaj\SQLEXPRESS

DB_DATABASE=Pankaj
DB_USERNAME=sa
DB_PASSWORD=pankaj

Error Details

PDOException in Connector.php line 119: could not find driver



via Chebli Mohamed

Issue while doing Unit Testing for Code Performance

I was testing my code to check how it will behave if we think that 100 users submitted their registration all at once !!!

My code is in PHP Laravel 5.2 and JQuery Ajax is below.

for (i = 0; i < 100; i++) {
    var 
     data={'Role' : "Role"+i},
     request = $.ajax({
        url:                'http://localhost:1234/Practise/public/api/SaveRoleApi',
        type:               "POST",
        data:               JSON.stringify(data),
        contentType:        "application/json; charset=utf-8",
        async:  true,
        success: function(d){ console.log(d); }
     });
}

Out of 100, I am not successful in submitting more then 88 records.

I am using MySQL Database.

Am I doing something incorrect?



via Chebli Mohamed