samedi 28 mai 2016

Laravel 5 Get size from all files inside a directory

Im trying to make a simple file manager in laravel, really I just want to see all the files from 'files' folder, and using laravel Storage I managed to get the name from all the files inside the folder, but i want to get more data like the size of each file.

Controller:

public function showFiles() {
    $files =  Storage::disk('publicfiles')->files();
    return view('files')->with('files', $files);
}

View:

@foreach($files as $file)
    <tr>
        <td></td>
        <td></td>
        <td class="actions-hover actions-fade">
            <a href=""><i class="fa fa-download"></i></a>
        </td>
    </tr>
@endforeach

Getting this .

As I said I want to get the size, but how could I do that, I tought about processing that in the view but I don't really want to do that.

That being said, I actually know how to do it like this:

@foreach($files as $file)
    <tr>
        <td></td>
        <td>
        <?php
        $size = Storage::disk('publicfiles')->size($file);
        echo $size;
        ?>

        </td>
        <td class="actions-hover actions-fade">
            <a href=""><i class="fa fa-download"></i></a>
        </td>
    </tr>
@endforeach

But I think it is not right to do that in the view right?



via Chebli Mohamed

No supported encrypter found issue

When i post my form on my laravel (5.2) i get this when it has to return some value to the former page.

MY CONTROLLER

class WsRegisterController extends Controller{

public function register()
{
    $wsregistration = Input::all();
    $wsUserName = Input::get('name');
    $wsUserEmail = Input::get('email');
    $wsUserPassword = Input::get('password');

    /* Check if user is a bot */

    $wsrules = [
    // 'g-recaptcha-response' => 'required|recaptcha', capthcha
    'name'   => 'required|min:2|max:32',
    'email'  => 'required|email',
    'password' => 'required|alpha_num|min:8'
    ];

    $wsvalidator = Validator::make($wsregistration, $wsrules);

    if ($wsvalidator->passes()) {

        /* Check if the email address exits */

        $wsUser_count = User::where('email', '=', $wsUserEmail)->count();

        // return $wsUser_count; exit;

        if ( $wsUser_count > 1 ) {

            return Redirect::to('/test')->with(array('error_msg' => 'This email address exist, please use another email address.'));

        }
     }
   }
  }

So i tried stackoverflowing it with this link but it is still not working

CONFIG/APP.PHP FILE

/*
|--------------------------------------------------------------------------
| Encryption Key
|--------------------------------------------------------------------------
|
| This key is used by the Illuminate encrypter service and should be set
| to a random, 32 character string, otherwise these encrypted strings
| will not be safe. Please do this before deploying an application!
|
*/

'key' => env('o/tPhyhKmuLoJMWXZeV8b10OFoCT62z6WKuC3HO5Jbw='),// env('9TSL9BsEjZyoM9BjX9du0XaLnCDi4m4Z'),

'cipher' => 'AES-128-CBC',//'AES-256-CBC',

.ENV FILE

APP_KEY=base64:o/tPhyhKmuLoJMWXZeV8b10OFoCT62z6WKuC3HO5Jbw=
APP_URL=http://localhost

I even did this artisan command to generate new key php artisan key:generate please what did i do wrong @everyone.

No supported encrypter found error snapshot



via Chebli Mohamed

laravel5 did not show php image in controller or route

I use laravel with version 5.1.35, but i found it not show image which write by php raw code. The code in raw php is

header("Content-type: image/png");
$im = @imagecreate(200, 50) or die("create php image rs error");
imagecolorallocate($im, 255, 255, 255);
$text_color = imagecolorallocate($im, 0, 0, 255);
imagestring($im, 5, 0, 0, "Hello world!", $text_color);
imagepng($im);
imagedestroy($im);

output of php is hello world

but in laravel 5.1.35 in route define is

Route::get('png',function(){
//  echo \Image::make(public_path('assets/image/xundu/logo.jpg'))->response('png');
    header("Content-type: image/png");
    $im = @imagecreate(200, 50) or die("create php image rs error");
    imagecolorallocate($im, 255, 255, 255);
    $text_color = imagecolorallocate($im, 0, 0, 255);
    imagestring($im, 5, 0, 0, "Hello world!", $text_color);
    imagepng($im);
    imagedestroy($im);
});

Output of it is php raw code display in laravel



via Chebli Mohamed

angular laravel handle token expiracy

So, I'm using angularJs and laravel with tymon-JWTAuth and sattelite.

Everything so good so far but I wanted to match the sessionStorage token to expire the session on the laravel side.

How is this achievable since sessionStorage cant set Expiracy.

And if possible, how can I redirect to login if a $http request has the error 401 token_expired ?

Best regards,

Filipe



via Chebli Mohamed

Laravel 5 eloquent custom with relationship

i have in my eloquent model the record id of an api served data, i wold like to load this data using the with method to prevent multiple api request, is there any way to create a custom with method?



via Chebli Mohamed

Multiple dynamic url redirect in Laravel

I have looked at many similar questions bu they don't approach the real problem. I would like to redirect a user to a certain url just after login depending on a condition about the user.

I know this can be archieved with a middleware so I have tried this in app\Http\Middleware\RedirectIfAuthenticated.php

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::User()->check()) {
            $redirect = '/client';
            if (Auth::user()->hasRole('admin')){
                $redirect = '/admin';
            }
            return redirect($redirect);
        }
        return $next($request);
    }
}

I realise now this will not work just after login. I'd like to redirect a user depending whether he/she is an admin or a client. I know I could use: protected $redirectPath = '/url/to/redirect'; but I have multiple pages to redirect to.

What is the best way to do this?



via Chebli Mohamed

How to eager-load models with only some of their fields?

I have two models: Question and Answer. A question has-many answers. To eager-load a question's answers, one must write it like this:

$question->load('answers');

However, all of the answers' properties are loaded this way. The following code, while illustrating what I want to achieve, does not work:

$quesiton->load('answers')->select('id', 'body')

So, how can I eager-load questions' answers with only their respective id, and body properties?



via Chebli Mohamed