Sultenhest

Sultenhest

Member Since 2 Years Ago

Experience Points
46,730
Total
Experience

3,270 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
463
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your 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-in-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 Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist 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 10
46,730 XP
May
25
3 days ago
Activity icon

Awarded Best Reply on Laravel 7 CORS Issues

To fix this issue i removed

"fruitcake/laravel-cors": "^1.0",

and updated it to

"fruitcake/laravel-cors": "^2.0",
Activity icon

Replied to Laravel 7 CORS Issues

To fix this issue i removed

"fruitcake/laravel-cors": "^1.0",

and updated it to

"fruitcake/laravel-cors": "^2.0",
May
19
1 week ago
Activity icon

Started a new Conversation Laravel 7 CORS Issues

Hi,

I recently uploaded my Laravel API to Laravel Forge, however when i try to access the api from a Nuxt app hosted on Netlify, i'm getting CORS errors:

Access to XMLHttpRequest at 'API-url' from origin 'Nuxt App-url' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Both of the sites are running https.

My Laravel 7 CORS config file looks like this:

return [
  'paths' => ['api/*'],

  'allowed_methods' => ['*'],

  'allowed_origins' => ['*'],

  'allowed_origins_patterns' => [],

  'allowed_headers' => ['*'],

  'exposed_headers' => [],

  'max_age' => 0,

  'supports_credentials' => true,
]

Everything works as expected locally when i run the Laravel Api via valet and my Nuxt app on a regular localhost:3000.

Could this have anything to do with the nginx settings on Forge?

May
12
2 weeks ago
Activity icon

Started a new Conversation GROUP BY Followed By A GROUP BY And COUNT

Hi,

I'm building an activity feed like the one Jeffrey Way builds in the Forum and the Birdboard series.

However i want to be able to get all activities grouped by week, and then getting the count of the description (contains: 'created_project', 'updated_project' and so on) column in my database, based on the specific week.

In my Activity model i have the following code, that returns a collection of activities grouped by week number

return static::where('user_id', auth()->id())
    ->latest()
    ->get()
    ->groupBy(function ($activity) {
        return $activity->created_at->format('W');
    })
    ->take(10);

The SQL query i want to perform on the sub-collections (each week) of the above function would look like this:

SELECT COUNT(description), description FROM activities GROUP BY description

How can i combine the two?

May
10
2 weeks ago
Activity icon

Replied to Inserting Eloquent Model With Multiple Foreign Keys

@michaloravec

I see thank you for the help :)

May
07
3 weeks ago
Activity icon

Replied to Inserting Eloquent Model With Multiple Foreign Keys

Hi @michaloravec and @wingly

Thank you both for the replies, however i'm getting

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed

on both project_id and user_id, since none of them are present in the $fillable array on the Task model.

Isn't it considered bad practice to allow mass assignment of foreign keys?

Activity icon

Started a new Conversation Inserting Eloquent Model With Multiple Foreign Keys

Hi,

I have a Task model that i need to create with a reference to a Project, but also the User that creates the Task. My code looks like this:

TaskController.php:

public function store(Request $request, Project $project)
{
    $this->authorize('update', $project);

    $task = $project->addTask($this->validateRequest($request));

    return response()->json([
        'task'    => $task,
        'message' => 'Task was successfully created.'
    ], 201);
}

Project.php :

public function addTask(array $task)
{
    $task = new Task($task);
    $task->project()->associate($this->id);
    $task->user()->associate(auth()->id());
    $task->save();

    return $task;
}

The code works fine, but i cant help but feel that it looks a bit messy (especially in Project.php). Is there a more efficient/clean way to do this?

My relationships are:

User

  • hasMany Projects
  • hasMany Tasks

Project

  • belongsTo User
  • hasMany Tasks

Task

  • belongsTo User
  • belongsTo Project
Apr
24
1 month ago
Activity icon

Replied to In_array-type Validation

You're right. Thanks @michaloravec!

Activity icon

Started a new Conversation In_array-type Validation

Hi

I'm want to validate a foreign key against the users other private models.

In short: a user has their private tags and posts. Posts don't need a tag_id (just one), so it's voluntary. However i want to make sure that the Tag id that get's attached to the Post is an id of the users private Tags, and not some other users private tags.

Example: User has tags with id 1, 2 and 3. How do i make sure that tag_id on the Post is in that array?

This is the validation i have on the Post now, that doesn't work:

return request()->validate([
    'tag_id' => [
        'sometimes',
        Rule::in(auth()->user()->tags()->pluck('id')->all())
    ],
    'title' => 'required'
]);
Apr
22
1 month ago
Activity icon

Replied to Consuming Own API - Passport (Sanctum)

Hi @oussama.tn

Of course! I forgot about Laravel Sanctum. Thank you :)

So if i want to consume my own API, all i need to do is add the HasApiTokens trait to my User models and create a token on registration like this in my RegisterController.php:

protected function create(array $data)
{
    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    $user->createToken('token-name');

    return $user;
}

All my routes with the auth:sanctum middleware seems to work correctly... it all seems suspiciously easy

Apr
21
1 month ago
Activity icon

Started a new Conversation Consuming Own API - Passport (Sanctum)

Hi

I want to build a Laravel application where most of the functionality (CRUD) will exist once the user has logged in. I want to build this backend part as an SPA (using vue-router) inside the Laravel project, consuming it's own API.

I also want to use the "standard" authentication from the laravel/ui package to log the user in and, as of now, i don't plan on building external consumers or letting the user create their own consumers of the API.

Since Laravel 7, the section about API Authentication with api_tokens ( https://laravel.com/docs/6.x/api-authentication ) has been removed from the docs, so i suppose this approach has been deprecated, even though, this basically does what i need with localStorage, but lacks security.

So my question is, is it overkill to use Passport for this or is this generally the way to do it in Laravel 7 for a use case like this? What is, if there is one, the alternative?