lundi 1 juillet 2019

Sort by document array field laravel Mongodb

I'm trying to get results sorted by count of array attribute of the document, in my case each document has an array of posts count, I want to fetch sorted result based on posts count, here is the schema.

[{
        "_id" : ObjectId("4e84f78b26d2046d5d00b5b2"),
        "user_id" : 3,
        "posts" : [
                "123",
                "1234"
        ],
        "text" : "John"
},
{
        "_id" : ObjectId("4e84f78b26d2046d5d00b5b3"),
        "user_id" : 2,
        "posts" : [
                "3432",
                "2322",
                "1232"
        ],
        "text" : "James"
},
{
        "_id" : ObjectId("4e84f78b26d2046d5d00b5b4"),
        "user_id" : 3,
        "posts" : [
        ],
        "text" : "Jimmy"
}]

Can someone help to make the query that can fetch result sorted by count of posts using laravel jessenger mongodb.

Thank you



via Chebli Mohamed

Can i use Helper @can in controller?

i have this code in my controller

public function index()
   {
       if(request()->ajax())
       {
           return datatables()->of(User::latest()->get())
               ->addColumn('action', function($data){
                   $button = '<a href="#" name="show" id="'.$data->id.'" class="show-modal"><i title="Visualizar" style="color: #00897b;" class="material-icons">visibility</i></a>';
                   $button .= '&nbsp;&nbsp;';
                   $button .= '<a href="#" name="edit" id="'.$data->id.'" class="edit-modal"><i title="Editar" style="color: black;" class="material-icons">edit</i></a>';
                   $button .= '&nbsp;&nbsp;';
                   $button .= '<a href="#" name="delete" id="'.$data->id.'" class="delete-modal"><i title="Excluir" style="color: red;" class="material-icons">delete</i></a>';
                   return $button;
               })
               ->rawColumns(['action'])
               ->make(true);
       }
       return view('admin.usuarios.index');
   }

Can i use helper @can in button? Like this:

$button = '@can ('isAdmin') <a href="#" name="show" id="'.$data->id.'" class="show-modal"><i title="Visualizar" style="color: #00897b;" class="material-icons">visibility</i></a> @endCan';



via Chebli Mohamed

Why the Laravel helpers are deprecated in version 5.8?

I am a happy Laravel user and I love the Laravel helpers. They are very easy to use:



But really really don't understand why this is they way:



Why...?



via Chebli Mohamed

Laravel Logging - Monolog stderr ignores logLevel

I'm using laravel 5.7 in a docker container. My goal is to write error-log directly to stderr instead of laravel.log

logging.php

<?php
return [

'default' => env('LOG_CHANNEL', 'stack'),
'channels' => [
        'stack' => [
            'driver' => 'stack',
            'channels' => ['stderr'], //before it was stderr
        ],

        'single' => [
            'driver' => 'single',
            'path' => storage_path('logs/laravel.log'),
            'level' => 'error',
        ],
//...
        'stderr' => [
            'driver' => 'monolog',
            'handler' => StreamHandler::class,
            'level' => 'error',
            'with' => [
                'stream' => 'php://stderr',
            ],
        ],
//...
    ],
];

The log target itself is working - laravel.log is empty - but it seems like the log level is ignored. In the docker logs I get also the debug messages

docker-compose logs --tail=100 container
...
ESC[36mcontainer_1         |ESC[0m [2019-07-01 09:57:51] local.DEBUG: ...
...

If I just change channels (stack) from stderr to single the logs (but only error level) are appended correctly to laravel.log



via Chebli Mohamed

How read values of config/filesystems.php

How in laravel 5.8 read values of config/filesystems.php, with default value :

return [

    'default' => env('FILESYSTEM_DRIVER', 'local'),


    'cloud' => env('FILESYSTEM_CLOUD', 's3'),


    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

I expected as :

$filesystem_pablic_path  = config('filesystem.disks.public');

but empty value returned ...



via Chebli Mohamed

Laravel API boilerplates, Unable to log in to web

I would like to use create a laravel API to serve content through a mobile app while at the same time using a laravel dashboard as a CMS for content generation I have tried several API boilerplates for laravel one og my favourites being https://github.com/francescomalatesta/laravel-api-boilerplate-jwt however i always get the same problem whenever i try to install a laravel Dashboard like voyager. The installation goes on succesfully but i cannot log into voyager itself, no error messages just a 302 redirect response, i even tried generating the laravel auth scaffold and creating the user from there but the situation remains the same Could somebody have a clue why this is the case? Thanks



via Chebli Mohamed

save() method only writing last record in foreach loop

 foreach ($updateOrInsert as $subordinate) {

        if ($key = array_search($subordinate, $existingDowns))
        {
         // $update[] = ['id'=>$key,'up'=>$up,'down'=>$subordinate,'status'=>0];
          $hierarchy = Hierarchy::find($key);

          $hierarchy->status = 0;

          $hierarchy->save();
      }
      else{
          $insert[] = ['up'=>$up,'down'=>$subordinate];
      }
  }

Here $key is the id of hierarchies table. the above code only update the last id only. I googled for this problem and spend couple of hours but no luck. Any idea?



via Chebli Mohamed