jeudi 24 septembre 2015

Laravel homestead: Unknown database 'projectadmin_db'

I am new to laravel. I use homestead for development. I have two database connection

database.php

'cust_main_db'   =>  [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'project_db',
        'username'  => 'homestead',
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],
    'admin_main_db'   =>  [
        'driver'    => 'mysql',
        'host'      => 'localhost',
        'database'  => 'projectadmin_db',
        'username'  => 'homestead',
        'password'  => 'secret',
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
    ],

Homestead.yaml

databases:
    - homestead

In my local mysql has both databases project_db and projectadmin_db

When I run the project http://ift.tt/1mROfU6 it shows SQLSTATE[HY000] [1049] Unknown database 'projectadmin_db'

What I have missed here? Correct me if anything wrong.



via Chebli Mohamed

Parse error after deploy Laravel 5.1 on shared hosting

I am using Laravel 5.1

To get rid of Public folder:

  1. I moved everything in a folder named 'root' except public folder.

  2. Move all public folder content in Root.

  3. Changed require __DIR__.'/root/bootstrap/autoload.php'; & $app = require_once __DIR__.'/root/bootstrap/app.php'; in index.php at root folder.

Everything is working perfectly in localhost. I uploaded my project in a shared hosting. And change database information in .env and change the url in Config\App.php 'url' => 'localhost', to 'url' => 'http://ift.tt/1jdbkqA',.

Now when I go to myproject.com it shows a Parse error Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /home/zamzamtransport/public_html/index.php on line 50

index.php:

require __DIR__.'/root/bootstrap/autoload.php';

$app = require_once __DIR__.'/root/bootstrap/app.php';

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

Another problem is my .env file is accessible.



via Chebli Mohamed

mercredi 23 septembre 2015

Retrieve all rows from table except few rows in laravel

I am using Laravel 5.1 & MySQL as backend to serve REST-API requests made from the mobile app.

I have a Cart table and Items table. So whenever user comes to 'Items screen' in his mobile App, In the backend I should perform 2 tasks.

  1. First check if any Items are there in his cart. If yes (i.e.,there are items in Cart table), then fetch those items from Cart table and the remaining items from Item table.
  2. If there are no items in Cart, then I will easily fetch all items from the Items table and show it to user.

I am struck by not able to perform the task 1. Because I am first retrieving all the item_ids from the Cart table. It will return a collection. Now I should check if these item_ids(from cart table) are present in Items table. If yes, don't fetch those items from Items table, BUT fetch all other items from Items table. Combine those items from Items table & items from Cart table and show it to user. How can I achieve this?

Currently, problem is, I am getting all 'item_ids' from Cart table. It returns a collection of item-ids. Using foreach() loop, for every item_id, I am quering Items table as follows: ("where('item_id','!=',$getItemId)->get();")

$itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('item_id','!=',$getItemId)->get();

And it returns collection checking against each individual item. and replaces collection with every new iteration in foreach loop.

Here is the function in my CartController:

public function getItemsWithCartItems($uuid,$store_id,$category_id,$subcategory_id,$subsubcategory_id=null)
{
    try
    {
        $getCartItems = UserCartDetailBng::where('uuid','=',$uuid)->get();
        if($getCartItems->isEmpty()) //Performing task 2. No problem here.
        {
            if($subsubcategory_id==null || $subsubcategory_id==0) // Bcoz, subsubcategory_id is optional
            {
                $itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->get();
                if($itemDetails->isEmpty()){
                    return ResponseService::buildFailureResponse("Store Item not found");
                }
            }
            else
            {
                $itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('subsubcategory_id','=',$subsubcategory_id)->get();
                if($itemDetails->isEmpty()){
                    return ResponseService::buildFailureResponse("Store Item not found");
                }
            }
            $count = $itemDetails->count();
            $data = array('count'=>$count,'result'=>$itemDetails);
            return ResponseService::buildSuccessResponse($data);
        }
        else  //Performing task 1. Here is the problem.
        {
          // I am using "where('item_id','!=',$getItemId)->get()" And it returns collection checking against each individual item. and replaces collection with every new iteration in foreach loop.
            foreach($getCartItems as $getCartItem)
            {
                $getItemId = $getCartItem->id;
                if($subsubcategory_id==null || $subsubcategory_id==0)
                {
                    $itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('item_id','!=',$getItemId)->get();
                    if($itemDetails->isEmpty()){
                        return ResponseService::buildFailureResponse("Store items not found");
                    }
                }
                else
                {
                    $itemDetails = ItemBng::where('store_id','=',$store_id)->where('category_id','=',$category_id)->where('subcategory_id','=',$subcategory_id)->where('subsubcategory_id','=',$subsubcategory_id)->where('item_id','!=',$getItemId)->get();
                    if($itemDetails->isEmpty()){
                        return ResponseService::buildFailureResponse("Store items not found");
                    }
                }
                $count = $itemDetails->count();
                $data = array('count'=>$count,'result'=>$itemDetails);
                return ResponseService::buildSuccessResponse($data);
            }
        }
    }

please help me solve this problem, TIA.



via Chebli Mohamed

Laravel Change Login URL - MethodNotAllowedHttpException

i want change laravel login url from /auth/login to login

already make change in AuthController

protected $loginPath = 'login';

and routes

Route::get('login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('logout', 'Auth\AuthController@getLogout');

i can access login page if manually go to that page

but if redirect to login page after trying to access page already protected with Middleware its still redirect to /auth/login



via Chebli Mohamed

Calculating time difference with values retrieved from MySQL TIME field

I've done a bit of searching and there is a bunch of information on calculating time difference between two times using strtotime('09:00:00) and then putting this against another value.

Currently, I have a database that stores regular hours for staff like so:

+----------+-----------+------------+-------------+--------------+
| staff_id |    day    | start_time | finish_time | total_breaks |
+----------+-----------+------------+-------------+--------------+
|        1 | Monday    | 18:00:00   | 22:00:00    |            0 |
|        2 | Wednesday | 09:00:00   | 17:30:00    |           30 |
+----------+-----------+------------+-------------+--------------+

start_time and finish_time are stored as TIME values in MySQL and total_breaks as an INT which I should probably convert to TIME values as well and represent as 00:30:00 or 00:00:00 for consistency.

What I want to be able to do is display a table that displays all of this information, as well as their total hours worked and then total hours minus any breaks.

I've been able to generate a table with the staff_id, day, start_time and finish_time but am struggling to figure out the best way to calculate the total amount of time worked etc.

I'm using Laravel, and have read that strtotime('00:00:00') is a great way to do this but this won't work as I'm not using a string but rather pulling the time from the database.

What is the best way to calculate the time difference between the two times, and then take in to account the breaks as well.

Have I even set this up correctly? I don't want to set the times up as a DATETIME as I'm just wanting to calculate the time difference of their normal working hours which they do from week to week - although I can always use dummy dates to get this working if necessary.

Any ideas?



via Chebli Mohamed

Load Datatables with Json Object as source come from API on Laravel 5.1

i am making a datatables which have datsource come from my API, this API will return the JSON and return to view (jquery) in order to load on datatables. But i don't know why the datatables ajax is automatically add params like bellow to the url, so the website report to me the 404 error. How can i fix this?, this is my code to call ajax to the controller route.

 <script>

      $("#example1").DataTable({
        "processing": true,
        "serverSide": true,
        "ajax": "/getAllStaff/1",
        "columns": [
            { "data": "UserCode" },
            { "data": "FullNameSta" },
            { "data": "EmailSta" },
            { "data": "PhoneNumberSta" },
            { "data": "UserRoll" },
            { "data": "Status" },
            { "data": "Status" }
        ]
    });
</script>

And this is my route (call to controller to return data to ajax call):

Route::get('/getAllStaff/{page}',['as' => 'Value', 'uses' =>  'admin\adminFunctionController@getDisplayInfor']);

Here is my controller code:

 public function getDisplayInfor($page)
{

    $params = ''.$page;
    $action = 'getAllStaff';

    $result = ApiController::getAPI($params, $action);
    try{
        if($result->{'success'}){
            return response()->json(['recordsTotal'=>$result->{'CountData'}, 'recordsFiltered'=>$result->{'CountData'}, 'data'=>$result->{'data'}]);
        }else{
            return response()->json(['success'=>false, 'error'=>$result->{'error'}]);
        }
    }catch (\Exception $ex){
        return response()->json(['success'=>false, 'error'=>$ex]);
    }
}

So i was tried to get it on postman and it's working fine, but it isn't when i call by ajax cause the datatables auto add params to the url and more than that, when i tried to call many time to the ajax i see that i have some request that return data to me. That's weird, if the route is the root of the problem i don't think it can return data in some request!.

Thanks.



via Chebli Mohamed

Upload file option not shown in trumbowyg wysiwyg editor

I am using trumbowyg wysiwyg editor in my laravel app. In the editor, upload option is not show though upload plugin is already installed. enter image description here



via Chebli Mohamed