jeudi 15 septembre 2016

Why hasMany Relation still working even i removed relation in Laravel

enter image description here

enter image description here In the above both models i commented the method itemDestribution() but still it is working



via Chebli Mohamed

How can i display a table with two foreign keys in laravel

I have 3 tables

Users
-------------
user_id  (PK)
user_name

Items_distributed
-------------
user_id (FK) 
item_id(FK)

Item List
-----------------
item_id(PK)
item_name

Now i need to print Items_distributed table by taking both the user_id and item_id and dispaly their item_name and user_name

i dont know how to display both things at a time if i display either item_name or user_name its working but when i try to print both it is not working . Can any one solve my problem .



via Chebli Mohamed

Different css for different company - Laravel

I've made a website which has 1 design. There could be users with different roles. Roles are next :

  • normal user
  • admin of company
  • worker
  • ...

Each user on website has his company (in database its integer field) and i would like to make that each user with role - admin of company can change design of website for his company (for start colors of body, header, footer etc.). Example of that is that company 1 has yellow design, company 2 red etc. So far i have come up with idea to do it like this:

In table company expand table schema for additional columns (body_color-string, footer_color-string etc) and write values for each company into database.

After user is successfully logged then those values load from database in session and in my app.blade.php override css sections with session ones.

For example if i have div

<div class="test123">Something</div> 

I would override that value in my app.blade.php, if session is not set then apply default color white.

<style>
    .test123 {
        background-color:}
</style>

And this works, but it's kinda messy, so i would ask for some advice or improvement for later on (when i will have maybe height, width of some elements, etc)...

Am i doing it the right way, or this can be done even easier?



via Chebli Mohamed

mercredi 14 septembre 2016

JSON shows string type in behalf of bit/boolean field

{
  "Status": true,
  "Message": "Roles retrieved successfully",
  "Data": [
    {
      "RoleID": 1,
      "Role": "Super Admin",
      "IsPredefined": "1"
    }
  ]
}

I am fetching above results in json format. I am using below query to fetch the data from MySql database.

Select * form tblrole;

I am using PHP-Laravel 5.3

Is there any way to make the resultset like below.

{
  "Status": true,
  "Message": "Roles retrieved successfully",
  "Data": [
    {
      "RoleID": 1,
      "Role": "Super Admin",
      "IsPredefined": true
    }
  ]
}

Issue is in IsPredefined. I want to retrieve it Boolean type. In database it is of type bit



via Chebli Mohamed

Advanced Logging System Using Laravel

Hello I have a little dilemma, I need to create a logging system on an application that I've built that basically just logs users different activities such as "Created/Updated/Deleted" etc. I know this can be accomplished by doing something like the following

public function deleteUser($user_id)
{

    // Delete the user
    User::where('id', $user_id)->delete();

    // Log what action took place
    $log = new Log();
    $log->name   = $current_username;
    $log->action = 'Deleted User';
    $log->time   = Carbon::now();
    $log->save();

}

But using this method means that not only do i have to go to each event i want logging and add this code but any methods i make in the future will need to have this again.

My question is, is there any way to achieve logging more.. dynamically? I can't think of the appropriate logic.



via Chebli Mohamed

Laravel 5.1: Validation rule for unique datetime

I have a field that is a datetime and must be unique in the database. In the form, the user will inform a date in the format mm/dd/yyyy. So when I use the validation rule unique in the repository for the date field, it is ok because it consider the hour, minute and second. They always will be different.

Do you know if there is any validation rule that ignore the hour, minute and second for a datetime?

Thanks in advace.



via Chebli Mohamed

$HTTP_RAW_POST_DATA warning still showing up in POST response after setting 'always_populate_raw_post_data' in php.ini to -1

So i am using PHP 5.6 and building a Laravel 5.1 API and also building an android app.

When i try sending a post request from the android emulator i get the following message along with the proper data/response:

Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version. 
To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead. in Unknown on line 0
Warning: Cannot modify header information - headers already sent in Unknown on line 0.

I know why the error is showing up, but i cant seem to fix it.

The normal fix would be to set always_populate_raw_post_data to -1 but that is not working.

I am thinking of upgrading to a later version of php where $HTTP_RAW_POST_DATA has been removed.

any help/advice would be appreciated.



via Chebli Mohamed