JJK

Experience

330

0 Best Reply Awards

  • Member Since 3 Weeks Ago
  • 1 Lesson Completed
  • 0 Favorites

13th September, 2017

JJK left a reply on User->save(); Retrieved By A Helper Class Doesn't Do Anything. • 1 week ago

If anyone else is having this issue. Create a variable in which you'll be storing object that your helper function is returning because it's creating a new one each time you do so.

JJK started a new conversation User->save(); Retrieved By A Helper Class Doesn't Do Anything. • 1 week ago

I've created my own auth/user model/controller which worked fine. Then I've decided to make things simpler and instead of fetching constantly user via session('user_id') - I've created a helper function Perm::user() that worked flawlessly.

The thing is - save() executed on object fetched by Perm::user() doesn't seem to have any effects. When I print it out, it does show the proper user with all there is to that specific object and I'm able to call ->id, ->login, ->name etc. without any issues. ..the only issue is, if I change name or any other attribute and run ->save() afterwards, it seems to have no effect.

Here's example code with save() that doesn't work:

    public static function verify($id)
    {
        Perm::user()->verified = true;
        Perm::user()->save();
        return redirect()->back()->with( ["notification" => "User verified."] );
    }

JJK left a reply on Helper Doesn't Work With Controllers • 1 week ago

@Jaytee Yeah, but MyFacade::user() (ergo PermHelper::user()) was the goal all along Where can I look up the creation of facades? Including the 'inclusion' into views.

Thanks for being patient and helping me out btw.

JJK left a reply on Helper Doesn't Work With Controllers • 1 week ago

@Jaytee It's more of a 'damn-I-removed-something-I-could-use-but-now-the-project-has-over-90k-lines-of-code' rather than 'it's fun' haha.

I'll definitely use the built in auth/user when I'll be remaking the website during winter break.

Anyways - how would I go about binding the class to all views so it's accessible via PermHelper::user()? Or perhaps should I create a facade?

JJK left a reply on Helper Doesn't Work With Controllers • 1 week ago

@Jaytee It's urgent for me to have a class helper rather than a single function. What is surprising to me is that as soon as I add 'namespace App\Helpers' to PermHelper.php, it stops working for the view - which I assume is because it's 'added' to a specific namespace - so using App\Helpers\PermHelper::user() rather than PermHelper fixes the issue.

So, maybe a different approach would solve this issue considering I can now use it fine in Controller (as long as I do the 'use' thing) and I can use it in a view using 'App\Helpers\Perm::user()'.

By different approach I mean - how can I force 'use App\Helpers' on all views, so I don't have to pass it as an instance in a variable?

JJK left a reply on Helper Doesn't Work With Controllers • 1 week ago

@Dry7 when I did what you said, it broke for views. What you are basically telling me to do is create a control inside 'Helpers' folder that works like a controller becasue it is a controller.

That's not what I'm aiming for.

JJK left a reply on Helper Doesn't Work With Controllers • 1 week ago

@Dry7 That's kinda missing the point, considering I defined a helper so I don't have to 'use' it every time.

JJK left a reply on Helper Doesn't Work With Controllers • 1 week ago

@Cronix I did previously add it to autoload inside a 'file' array to no avail.

JJK left a reply on Helper Doesn't Work With Controllers • 1 week ago

@Dry7 "Class 'App\Http\Controllers\PermHelper' not found"

JJK started a new conversation Helper Doesn't Work With Controllers • 1 week ago

I've created myself a helper class called Perm that is meant to return user of current session (I know, I could use default auth/user, but that wouldn't be as much of a fun as creating one from scratch!)

..sadly, the created helper class only works inside a view, but doesn't work at all in controllers.. which kinda misses the point.

Whenever I'm trying to use it inside a controller, it pops:

"Class 'App\Http\Controllers\Perm' not found"

I would most appreciate any help.

HelperServiceProvider.php:

namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class HelperServiceProvider extends ServiceProvider
{
    public function boot()
    {
        //
    }

    public function register()
    {
        foreach( glob (app_path().'/Helpers/*.php' ) as $filename ) // register all helpers
        {
            require_once($filename);
        }
    }
}
Helpers/PermHelper.php:

use App\User;

class Perm
{
    public static function user()
    {
        if(!session('user_id')) return null;
        return User::find(session('user_id'));
    }
}

Portion of config/app.php, the 'providers' array:

// Custom
App\Providers\HelperServiceProvider::class,

7th September, 2017

JJK started a new conversation Object Of Class App\Mail\PasswordRecovery Could Not Be Converted To String • 2 weeks ago

I'm completely clueless at this point, I've followed examples accessible here: https://laravel.com/docs/master/mail to no avail.

I'm constantly getting: (1/1) ErrorException Object of class App\Mail\PasswordRecovery could not be converted to string

Here's the code, from app/Mail/PasswordRecovery.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class PasswordRecovery extends Mailable
{
    use Queueable, SerializesModels;

    //public $nickname;
    //public $pin;
    public function __construct()
    {
        //$this->nickname = $user->nickname;
        //$this->pin = "02391f";
        //$this->build();
    }

    public function build()
    {
        return $this->view('emails.passwordrecovery');
    }
}

Here's code from web.php route file

Route::get('/mailable', function () {
    return new App\Mail\PasswordRecovery();
});
Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.