leonvr

leonvr

Member Since 1 Month Ago

Experience Points 440
Experience Level 1

4,560 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 2
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.

14 Jun
4 days ago

leonvr left a reply on Eloquent Query Error

@DEANSATCH - Thanks. I will think about which is the best approach

leonvr left a reply on Eloquent Query Error

@DAVIDPETROV - I think you are right. $invitation in the called function is of class Invitation, but $invitation in the calling function is not. Unfortunately I am not very good at PHP, so how do I do that?

leonvr left a reply on Eloquent Query Error

Some more info: When I dump $invitation inside the called function it looks different:

Invitation {#446 ▼
  #fillable: array:6 [▶]
  #connection: "mysql"
  #table: "invitations"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:9 [▼
    "id" => 45
    "user_host_id" => 14
    "wishlist_id" => 21
    "user_famvr_id" => 19
    "meta_user_type" => "fvanoniem"
    "active" => 1
    "accepted" => 1
    "created_at" => "2019-06-13 16:44:14"
    "updated_at" => "2019-06-13 16:44:14"
  ]
  #original: array:9 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

So it looks that as soon as it is returned to the calling function it is not correctly formatted anymore. Maybe I should do something like this?

        return response()->json([
            //'message' => 'Successfully logged in',
            //'user'=> $user,
            'access_token' => $tokenResult->accessToken,
            'expires_at' => Carbon::parse($tokenResult->token->expires_at)->toDateTimeString(),
            'invitation' => Invitation::class $invitation
        ]);

So explicitly returning it as an invitation-object. But how do I do that?

leonvr left a reply on Eloquent Query Error

Some more info: when I dump $inlogdata:

{#510 ▼
  +"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjJmYjVjM2RhNTAyOTU0ZGY5ZTIzOGQ0Mzg4MTMxYjcxMTZmYTZmZDQyYmU0NWVlZjQzZGU0ZjZhNmQ2Y2UzNGE3MjRkYjU1NzI5Y2E0MGY1In0.eyJh ▶"
  +"expires_at": "2020-06-14 15:16:53"
  +"invitation": {#511 ▼
    +"id": 45
    +"user_host_id": 14
    +"wishlist_id": 21
    +"user_famvr_id": 19
    +"meta_user_type": "fvanoniem"
    +"active": 1
    +"accepted": 1
    +"created_at": "2019-06-13 16:44:14"
    +"updated_at": "2019-06-13 16:44:14"
  }
}

leonvr left a reply on Eloquent Query Error

@DEANSATCH - then I get:

Call to undefined method stdClass::invitation()

leonvr left a reply on Eloquent Query Error

The invitation is part of the result of a function-call:

            $result = $this->login_ffa_nopw($request, $email);
            $inlogdata = $result->getData();

            $invitation = $inlogdata->invitation;

            $wishlist = $invitation->wishlist;

From the same controller:

    protected function login_ffa_nopw(Request $request, String $email)
    {
        if(!Auth::attempt(['email' => $email, 'password' => config('custom.fvanoniem.ww')])) {
            //zou niet moeten kunnen, want in aanroepende view_wishlist is al gecheckt dat inlog klopt
            abort(401, 'Viewing the wishlist failed');
        }

        $user = Auth()->user();

        // invitation has to be active (accepted is not relevant)
        $invitation = $user->famvr()->where([
                ['meta_user_type', 'fvanoniem'],
                ['active', '1']
            ])->first();
        abort_if($invitation == null, 403, 'This link has no valid Invitation. You may contact the owner of the Wishlist');

        $tokenResult = $user->createToken('TokenVll4');

        return response()->json([
            //'message' => 'Successfully logged in',
            //'user'=> $user,
            'access_token' => $tokenResult->accessToken,
            'expires_at' => Carbon::parse($tokenResult->token->expires_at)->toDateTimeString(),
            'invitation' => $invitation
        ]);
    }

leonvr started a new conversation Eloquent Query Error

I have the following code in a controller:

$wishlist = $invitation->wishlist;

This causes the following error

Undefined property: stdClass::$wishlist

$invitation is correct at that time:

{#511 ▼
  +"id": 45
  +"user_host_id": 14
  +"wishlist_id": 21
  +"user_famvr_id": 19
  +"meta_user_type": "fvanoniem"
  +"active": 1
  +"accepted": 1
  +"created_at": "2019-06-13 16:44:14"
  +"updated_at": "2019-06-13 16:44:14"
}

In my Invitation class:

    public function wishlist()
    {
        return $this->belongsTo(Wishlist::class);
    }

In this case I am expecting the $wishlist to be filled with the wishlist-record with id=21, but I get an error. What am I doing wrong?

11 Jun
1 week ago

leonvr left a reply on Policy Not Working

Thanks Nakov, that solved the problem.

I also had to change the use in the WishlistPolicy:

<?php

namespace App\Policies;

use App\User;
// use App\Wishlist; replaced by line below
use App\Models\Wishlist;
use Illuminate\Auth\Access\HandlesAuthorization;

class WishlistPolicy
{
    use HandlesAuthorization;

    //Determine whether the user can maintain the wishlist.
    public function maintain(User $user, Wishlist $wishlist)
    {
        //return true;
        return $user->type == 'beheerder' && $user->id === $wishlist->user_id;
    }

}

leonvr started a new conversation Policy Not Working

I am trying to implement a policy on my Wishlist resource, but I always get "This action is unauthorized." even if my policy always returns true. What am I doing wrong?

WishlistPolicy:
<?php

namespace App\Policies;

use App\User;
use App\Wishlist;
use Illuminate\Auth\Access\HandlesAuthorization;

class WishlistPolicy
{
    use HandlesAuthorization;

    //Determine whether the user can maintain the wishlist.
    public function maintain(User $user, Wishlist $wishlist)
    {
        return true;
        //return $user->type == 'beheerder' && $user->id === $wishlist->user_id;
    }

}

AuthServiceProvider:

    protected $policies = [
        // 'App\Model' => 'App\Policies\ModelPolicy',
        'App\Models\Wishlist::class' => 'App\Policies\WishlistPolicy::class',
    ];

api.php:

Route::middleware(['auth:api', 'verified', 'can:maintain,wishlist'])->group(function () {
    Route::apiResource('wishlists', 'WishlistController');
});

get request with Insomnia: http://laravelapi1.test/api/wishlists/21

result: "message": "This action is unauthorized.", "exception": "Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException", "file": "/home/vagrant/code/laravelapi1/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php", "line": 202,

19 Apr
1 month ago

leonvr left a reply on API Call Not Returning A Response

You are right. I managed to run the code using xampp/apache. Thanks

leonvr left a reply on API Call Not Returning A Response

I am using php artisan serve

My code is a simplified version of the api/login route in this project: https://github.com/drehimself/todo-laravel

It is the same pattern: http://todo-laravel.test/api/login leading to an internal call http://todo-laravel.test/oauth/token video: https://youtu.be/HGh0cKEVXPI?list=PLEhEHUEU3x5q-xB1On4CsLPts0-rZ9oos&t=876

I do not know which server he is running.

But you are saying this is not able to work with php artisan serve? I am new to Laravel, but this is not an uncommon pattern for an api?

Is there a local webserver (simple to use), which you would recommand, and is able to run this? And do you think this will work in a standard production environment?

A lot of questions, but I appreciate your help.

leonvr started a new conversation API Call Not Returning A Response

This works fine: http://127.0.0.1:8000/api/all

api.php Route::get('all', function () { return 'everybody may access this api route'; });

But when I call the same route from within another route, I never get a response, and it keeps spinning...

What am I doing wrong?

api.php: Route::get('/test', '[email protected]');

AuthController.php: