leonvr

leonvr

Member Since 6 Months Ago

Experience Points
540
Total
Experience

4,460 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
3
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

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

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

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

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    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 Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

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

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

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

Level 1
540 XP
Jun
14
5 months ago
Activity icon

Replied to Eloquent Query Error

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

Activity icon

Replied to 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?

Activity icon

Replied to 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?

Activity icon

Replied to 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"
  }
}
Activity icon

Replied to Eloquent Query Error

@DEANSATCH - then I get:

Call to undefined method stdClass::invitation()
Activity icon

Replied to 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
        ]);
    }
Activity icon

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 (notice wishlist_id):

{#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?

Jun
11
5 months ago
Activity icon

Replied to 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;
    }

}
Activity icon

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,