pietertje

pietertje

Member Since 4 Years Ago

Experience Points 2,150
Experience Level 1

2,850 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed 48
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • Chatty Cathy Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

27 Jan
3 years ago

pietertje started a new conversation Fatal Error: Uncaught Exception 'ReflectionException' In Laravel 5.2

I've just upgraded my project from 5.1 to 5.2, it works flawlessly in homestead, however when I deploy to production, I get the following stacktrace:

Fatal error: Uncaught exception 'ReflectionException' with message 'Class log does not exist' in /var/www/domains/moetikschoonmaken.nl/vendor/laravel/framework/src/Illuminate/Container/Container.php:738 Stack trace: #0 /var/www/domains/moetikschoonmaken.nl/vendor/laravel/framework/src/Illuminate/Container/Container.php(738): ReflectionClass->__construct('log') #1 /var/www/domains/moetikschoonmaken.nl/vendor/laravel/framework/src/Illuminate/Container/Container.php(633): Illuminate\Container\Container->build('log', Array) #2 /var/www/domains/moetikschoonmaken.nl/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(674): Illuminate\Container\Container->make('log', Array) #3 /var/www/domains/moetikschoonmaken.nl/vendor/laravel/framework/src/Illuminate/Container/Container.php(853): Illuminate\Foundation\Application->make('Psr\Log\LoggerI...') #4 /var/www/domains/moetikschoonmaken.nl/vendor/laravel/framework/src/Illuminate/Container/Container.php(808): Illuminate\Container\Container->resolveClass(Object(Reflection in /var/www/domains/moetikschoonmaken.nl/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 738

The only difference between my homestead and production environment, is that the latter runs on PHP 5.5.9. Anyone any ideas as to what could be causing this?

30 Sep
3 years ago

pietertje started a new conversation Middleware Only Applying To Standard Controller Methods

Hi,

I was messing around with my project a bit when I discovered that the route using the getBalances() method in my UserController was accessible for user whom were not logged in. Even though In the constructor of my controller I use the standard $this->middleware('auth');.

After doing some more research I very quickly learned that Middleware defined in the constructor of a controller ONLY applies to standard controller functions and not ones you added yourself.

I'm reasonably sure this is intentional behaviour of middleware, however this isn't explicitly mentioned anywhere (I checked) in the documentation/laracasts/api. As such I believe this quite a serious vulnerability for many (beginning) laravel developers, who might think their routes are safely protected behind middleware, while in fact this is certainly not the case.

Thoughts?

14 Jul
3 years ago

pietertje left a reply on Need Help Debugging A Very Strange "undefined Method" Error

Okay, I'm a complete idiot. I just found out that I had 'from_user_balances' field appended to one of my models. Took me two whole days to find this :(.

pietertje started a new conversation Need Help Debugging A Very Strange "undefined Method" Error

Hi guys,

I'm having a very weird problem: under very specific circumstances I get the error: "Call to undefined method Illuminate\Database\Query\Builder::getFromUserBalancesAttribute()". The weird thing is, I don't have that method anywhere in my code. I have a user model with a getBalancesAttribute method and I have another model with a getFromUserAttribute, but nowhere do I have the function getFromUserBalancesAttribute.

I have the feeling that somewhere laravel confuses these two functions and somehow calls some kind of portmanteau of these methods. However, I'm not yet familiar enough with laravel that I completely understand its interpretation of method names. So I guess what I'm asking is, what processes could lead to laravel mixing up method names and how would I go about finding this process?

I realize this is a very vague question, but I feel that if I posted my code, I would just make the whole situation even more needlessly complicated. Though, if you need more information, just ask and I'll try my best to provide it

Thanks in advance,

Pieter

14 May
4 years ago

pietertje started a new conversation Observe Sync() Operation?

So I have a Transaction model which has a belongs-to-many relationship with the User model. In my TransactionController I have an update() method which looks like this:

    public function update($id, Request $request)
    {
        $affected       = $request->get('affected');
        $transaction    = Transaction::find($id);
        $transaction->users()->sync($affected);
        $transaction->save();
    }

In Providers\EventServiceProvider.php I have the following code:

    {
        parent::boot($events);

        Transaction::saving(function($transaction)
        {
            Cache::forever('test', 'test');
        });
    }

My problem is that the code in the Transaction::saving block is not executed when the above update function() is called. My (uneducated) guess is that the sync() call alone doesn't trigger the saving method. So my question is: what do I need to do in order for the saving the method be called?

13 May
4 years ago

pietertje started a new conversation Is This A Good Way To Cache Query Results

So I want to cache the results of some pretty resource intensive queries and this is the way I implented it:

class Ledger extends Model
{
    protected $guarded  = [
        'balance_cache'
    ];

    protected $appends  = [
        'balance',
        'balance_cache'
    ];

    public function getBalanceAttribute()
    {
        if($this->balance_cache != null)
        {
            return $this->balance_cache;
        }
        $balance      = Transaction::where('ledger_id', $this->id)->sum('amount');

        Cache::forever('ledger_balance_' . $this->id, $balance);
        return $balance;
    }
    public function getBalanceCacheAttribute()
    {
        if(Cache::has('ledger_balance_' . $this->id))
        {
            return Cache::get('ledger_balance_' . $this->id);
        }
        return null;
    }
    public function transactions()
    {
        return $this->hasMany('App\Transaction');
    }
}

And then in EventServiceProvider I do:

    public function boot(DispatcherContract $events)
    {
        parent::boot($events);

        Transaction::creating(function($transaction)
        {
            Cache::remove('ledger_balance_' . $transaction->ledger_id);
        });
    }

Is this an accepted way of using the cache, or is there a better way I'm not aware of?

09 Jan
4 years ago

pietertje left a reply on L5 With Angularjs, Routing Problems

@zefman, I don't use blade so this did the job nicely:

Route::any('{path?}', function()
{
    return File::get(public_path() . '/angular.html');
})->where("path", ".+");

Thanks!!

pietertje left a reply on L5 With Angularjs, Routing Problems

@zefman, that is correct. Where did you put that code? In routes.php?

pietertje left a reply on Response::json Doesn't Work With Custom Attributes

I'm passing it to an angularJS frontend. I know I can store the balance value, but I don't want to. It's complicated, but the gist is that balance should not be stored, but instead calculated.

pietertje left a reply on Response::json Doesn't Work With Custom Attributes

Nope, I realize it's in the arguments for the get function, but that's just because I tried @garbee's suggestion and forgot to take it out when posting it here.

pietertje left a reply on Response::json Doesn't Work With Custom Attributes

Adding 'balance' to get just results in an SQL error, which makes sense, I guess. I trimmed down the code I posed, originally I had: ```` public function index() { $transactions = $this->transaction->orderBy('verwerkings_datum', 'DESC')->get(); $users = User::with("transactions")->get([ 'id', 'name', 'afkorting', 'balance', 'huidig_hg'])->where("huidig_hg", 1);

    foreach($users as &$user)
    {
        $balance = 0;

        foreach($user->transactions as &$transaction)
        {
            $affected = $transaction->users->count();
            $amount   = $transaction->bedrag/$affected;
            $balance  = $transaction->cred_deb == "C" ? $balance + $amount : $balance - $amount;
        }
        $user->setBalanceAttribute($balance);
    }
    return Response::json($users);
}

````

pietertje started a new conversation Response::json Doesn't Work With Custom Attributes

Hi,

My User model looks like this:

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

    (...)

    protected $appends  = ["balance"];
    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    public function transactions()
    {
        return $this->belongsToMany("App\\Transaction");
    }
    public function getBalanceAttribute()
    {
        return $this->attributes['balance'] ;
    }
    public function setBalanceAttribute($balance)
    {
        $this->attributes['balance'] = $balance;
    }
}

However when I try the following lines of code, I get an error with: "Undefined index: balance"

        $users        = User::with("transactions")->get([
            'id',
            'name',
            'current'])->where("current", 1);

        return Response::json($users);

pietertje started a new conversation L5 With Angularjs, Routing Problems

Hi,

I'm following this tutorial on using both angularjs and laravel: http://angular-tips.com/blog/2014/10/working-with-a-laravel-4-plus-angular-application/. I'm having problems, though. According to the tutorial Is should add:

App::missing(function($exception)
{
    return File::get(public_path() . '/angular.html');
});

To my routes.php, so that laravel will serve up the angularjs page. Only this doesn't work in L5. So I tried instead:

app('exception')->missing(function($exception)
{
    return File::get(public_path() . '/angular.html');
});

...But that also doesn't work. Does anyone have any idea on what could work?

08 Jan
4 years ago

pietertje left a reply on Log In Not Working

@schizofriendly, not hashing passwords is inexcusable in virtually any situation and there is literally no reason not to do it, other than laziness. Further reading: http://security.blogoverflow.com/2011/11/why-passwords-should-be-hashed/

pietertje left a reply on Log In Not Working

@bashy I know that, but the OP posted: "I already have and existing database and I'm using PostgreSQL,the name of my table is 'users' which consists of columns such as studentid(PK) and password (which is not hashed)."

pietertje left a reply on Log In Not Working

Also, hash your passwords, for fucks sake.

pietertje left a reply on How To Do This Very Complicated Query?

Heh, it was easier than I thought, god bless you Taylor Otwell.

        foreach($users as &$user)
        {
            $balance = 0;

            foreach($user->transactions as &$transaction)
            {
                $affected = $transaction->users->count();
                $amount   = $transaction->amount/$affected;
                $balance  = $transaction->cred_deb == "C" ? $balance + $amount : $balance - $amount;
            }
            $user->balance = $balance;
        }

pietertje left a reply on How To Do This Very Complicated Query?

The total amount of what? The balance or the transaction? The sum of all balances should equal the sum of all transactions. I think you're right about doing this in php, I just remembered that the field amount is always positive and that there is a second field which indicates whether the transaction was crediting or debiting. My head hurts. :(

pietertje started a new conversation How To Do This Very Complicated Query?

Hi,

I'm creating a small bookkeeping program for a checking account shared between my housemates and I. The project works with two models, User and Transaction which have a belongstomany relation with each other. The idea is that every transaction made on the checking account is entered into the program along with whoever the cost was made for. From this information a balance for each user should be calculated. So if for instance a purchase of $50 is done for user1 and user2, but not for user3 and user4 (4 users in total), then user1 and user2 should both have $25 deducted from their balance.

My question is, when displaying the table with the balance for each user, how do I calculate the actual balance? Are there some short-hand functions I can use, or do I have to start dicking around with foreach loops? My best attempt was

$User::with("transactions")->get();

foreach($users as $user)
{
    echo $user->transactions->sum('amount'));
}

But this is obviously not correct. So, yeah, how should I do this?

05 Jan
4 years ago

pietertje left a reply on Using Multiple Models In A Single Route

My situation is a little different than any of the options you provided. Allow me to elaborate: Every user is able to login and view a table with all transactions, but above this table there is another one which displays the standing of every other user. Like so:

+--------+----------+----------+---------+
| User1 | User2 | User3 | User4 |
+--------+----------+----------+---------+
| +$20 |  +$40  |   -$10   |  +$13  |
+--------+----------+----------+---------+
--whitespace--
--Transactions table--

So basically, the two models don't really interact with each other in any way.

pietertje started a new conversation Using Multiple Models In A Single Route

Hi,

If I want a particular route, say /transactions, to return a view which presents data from the Transaction model, as well as from the User model, what would be the 'laravel' way of doing this?

04 Jan
4 years ago

pietertje started a new conversation Where To Put Non-laravel Classes In Laravel 5

Hi,

I'm creating a small bookkeeping tool to track the transactions done on a checking account which is shared by multiple people. In this tool there is a Transaction model which is just your standard laravel model. Where things get complicated, is that from my bank I can download a .csv file which holds all transactions done up up until some date. For this .csv file I've written a class which neatly enters every transaction in to an array and that array can then passed to the TransactionController to populate the database. My problem is, I don't know what the convention is on placing classes which are only used to process data and nothing else. Am I supposed to create a separate directory, or just put it in with the controller, or...?

03 Jan
4 years ago

pietertje left a reply on PDO Connection Refused With Laravel, But Not With $con = New PDO()

Ah, I understand now. I've configured database.php as mentioned in my first post in this thread. I've configured homestead.yaml like so:

---
ip: "192.168.10.10"
memory: 2048
cpus: 1

authorize: ~/.ssh/id_rsa.pub

keys:
    - ~/.ssh/id_rsa

folders:
    - map: ~/Dropbox/workspace
      to: /home/vagrant/Code

sites:
    - map: huisrekening.app
      to: /home/vagrant/Code/HuisrekeningV2/public
    - map: phpmyadmin.app
      to: /home/vagrant/Code/phpmyadmin

databases:
    - homestead

variables:
    - key: APP_ENV
      value: local

pietertje left a reply on PDO Connection Refused With Laravel, But Not With $con = New PDO()

@bestmomo I've changed my .env, but that also didn't work. I've tried both localhost as well as 127.0.0.1. What is this .env file supposed to do?

@codeatbusiness Errr, config file? do you mean homestead.yaml? if so, it's set to local.

@bashy Why can't you use localhost with a VM. For the record, I've tried 127.0.0.1 as well as localhost multiple times.

pietertje left a reply on PDO Connection Refused With Laravel, But Not With $con = New PDO()

I've just run composer update on my project and the problem still persists.

My .env file: ```FOO=bar BAR=baz SPACED=with spaces

NULL= ```

pietertje started a new conversation PDO Connection Refused With Laravel, But Not With $con = New PDO()

I've configured mysql for my laravel 5.0-dev project like so:

'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST') ?: 'localhost', 'database' => env('DB_DATABASE') ?: 'homestead', 'username' => env('DB_USERNAME') ?: 'homestead', 'password' => env('DB_PASSWORD') ?: 'secret', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', 'strict' => false, ],

However, I get connection refused ('PDOException' with message 'SQLSTATE[HY000] [2002]') whenever I try to interact with the database using either php artisan, or the laravel app itself.

The weird thing is, when I run the code below, the connection doesn't get refused.

<?php $dsn = 'mysql:dbname=homestead;host=localhost'; $user = 'homestead'; $password = 'secret';

try { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); }

I've been googling for 3 hours now and I haven't found a single solution which actually works. So any help is greatly appreciated.