Leandro_Haruki

Leandro_Haruki

Member Since 1 Year Ago

São Paulo

Experience Points 4,050
Experience Level 1

950 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 27
Lessons
Completed
Best Reply Awards 2
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.

01 Oct
9 months ago

Leandro_Haruki left a reply on Is There Any Route Alignment/indentation Pattern? (and More Questions)

Thank you for the replies, i'll do as @Cronix said and just indent the controllers, it sounds cleaner.

28 Sep
9 months ago

Leandro_Haruki started a new conversation Is There Any Route Alignment/indentation Pattern?

Hello guys, i would like to know some nice way to set up my routes, since i have started learning Laravel my routes always felt really messy and unaligned.

This is how my routes usually look:

Route::post('password/reset', 'Auth\[email protected]')->name('password.reset');
Route::post('password/email', 'Auth\[email protected]')->name('password.email');
Route::post('signup', 'Auth\[email protected]')->name('signup');

Since i'm still new to the framework, i've been changing my routes time to time due to namespaces, Model bindings instead of putting it in the Request body, etc. So i thought about aligning routes like:

Route::post('password/reset',   'Auth\[email protected]')                 ->name('password.reset');
Route::post('password/email',   'Auth\[email protected]')   ->name('password.email');
Route::post('signup',           'Auth\[email protected]')                       ->name('signup');

I think that aligning the routes parameters would make the routes look better, however, i'm afraid it will be a pain to keep everything aligned.

I would like to know how you guys manage routes: -Should i separate my api routes in different route files? -Is this alignment thing bad? -Should i follow a single pattern of using only Route::resource, separate every request type or mix it when needed?

TL;DR: I'm a Laravel noob and my api route file look ugly and long.

26 Sep
9 months ago

Leandro_Haruki left a reply on Correct Way To Extend Base Model To Add Additional Functionality

Isn't it a bad practice overwriting the base model? I've always been concerned about deviating from Laravel's patterns, so new team members wouldn't overlook such things.

This post was led me to this way of thinking: https://laracasts.com/discuss/channels/code-review/i-have-made-a-base-controller-to-dry-my-api-controllers-i-would-like-some-feedback

21 Sep
9 months ago

Leandro_Haruki left a reply on Delete Eloquent Relationship Data

Try using cascading delete in your migrations:

    $table->foreign('university_id')->references('id')->on('universities')->onDelete('cascade');
20 Sep
9 months ago

Leandro_Haruki left a reply on Laravel API Validation

Could you send the dd() from the $input variable? sorry for the delay

19 Sep
9 months ago

Leandro_Haruki left a reply on Obtain Specific Attributes From Collection

Well, if you can't change the function to use array instead of model, i suggest you to temporarily hide the attributes you don't want.

$data = $products->map(function ($product) {
$products->makeHidden(array_keys(array_except($products->getAttributes(),['id','name'])))
});
18 Sep
10 months ago

Leandro_Haruki left a reply on Laravel API Validation

Try this:

$validator = Validator::make($input, [
        'exc.*.value' => 'required',
 ]);
14 Sep
10 months ago

Leandro_Haruki left a reply on How To Foreach This Data

@Leandro_Haruki thanks

You're welcome.

Indeed, should use @erikverbeek 's answer and put it in your controller as @cmdobueno said.

Leandro_Haruki left a reply on How To Foreach This Data

@foreach($genres as $genre)
    @foreach(explode($genre) as $genreName)
        <a href="{{ url('cinema/movies/genre/'$genreName) }}" class="item">{{$genreName}}</a>
    @endforeach 
@endforeach 

Leandro_Haruki left a reply on How To Foreach This Data

Well, i guess you should use a PHP method called explode() then.

http://php.net/manual/en/function.explode.php

Leandro_Haruki left a reply on How To Foreach This Data

So basically you need to list all existent movie genres. The best practice i was taught is that for types/genres/etc you create a new table for it, so it can be accessed and incremented dynamically.

13 Sep
10 months ago

Leandro_Haruki left a reply on Eager Load Accessors

Did @pmall solution work?

''' $users = User::with('posts')->get(); ''' I'm using Laravel 5.6, this gives me an error telling me Laravel expects a relationship.

It seems that i can't lazy load nor eager load an accessor that is not appended to the Model i want.

I'm trying to build an 'on demand accessor' (maybe not the best practice, or maybe i'm overcomplicating things), my solution: ''' public function getWatchedAttribute() { $this->append('watched'); return DB::table('watched_lessons')->where('lesson_id',$this->id)->where('user_id',Auth::id())->exists(); } '''

It would make me happy if i could just: ''' $lesson->load('watched') '''

14 Jun
1 year ago

Leandro_Haruki left a reply on Subqueries In Querybuilder

left join `user_roles` on `users`.`id` = `user_roles`.`user_id`
    and user_roles.startdate = (
      SELECT MAX(startdate) FROM user_roles WHERE user_roles.user_id = users.id
    )

I looked at Laravel's documentation Advanced Join Clauses. I got this:

->leftJoin('user_roles', function($join){
    $join->on('users.id', '=', 'user_roles.user_id');
    $join->on('user_roles.startdate', '=', DB::raw('SELECT MAX(startdate) FROM user_roles WHERE user_roles.user_id = users.id'))
})

Other references: StackOverflow: A join with additional condition using Query Builder or Eloquent

Laracasts: select statement inside another select

Leandro_Haruki left a reply on Best API Authentication Approach

I was concerned about best practice of doing this.

Same. I have an API that uses tymondesigns/JWT-Auth instead of Passport to generate tokens to the authenticated users ,refreshing it for every request made to the API to keep the user authenticated. I want to know if it is a bad practice and how i can improve it.

Leandro_Haruki left a reply on I Have Made A Base Controller To DRY My API Controllers, I Would Like Some Feedback

I'm really thankful for your reply @bugsysha , i was afraid my post was going to be ignored haha. I really have little experience when it comes to programming but i'm working on it !

It would get very dirty to put that logic in the controller you wrote.

It gets kind of cloudy when i try to imagine it, however i think i know where you're getting at. There are some polymorphic relationships that need some cascading delete i have to think of and it's probably the case you've mentioned.

I think I've heard @JeffreyWay say that programmers (especially juniors) are hesitant to create new classes and they tend to put everything in existing structure/files. This might not apply to you but it gave me that impression.

Hmmm, it is i think. After watching the video you suggested i found out that my controllers are pretty much the same as it was at the start of that video, i will have to create new controllers for relationship actions. Now i know why Route::resource is used.

Sorry if my thoughts are confusing. I lack some serious amount of sleep.

It was a good reply ! Thank you.

13 Jun
1 year ago

Leandro_Haruki left a reply on I Have Made A Base Controller To DRY My API Controllers, I Would Like Some Feedback

Thank you a bunch for that video, i will rethink my routes and controllers. I wish i could post the API on GitHub but i'm afraid i can't post it, i'm building it to the company i'm currently working, i can only share some parts of it.

Looks super clean but I do not like it.

What do you dislike about it?

Leandro_Haruki started a new conversation I Have Made A Base Controller To DRY My API Controllers, I Would Like Some Feedback

I'm still new at Laravel, i don't really know if what i coded is bad. I studied a little about repository pattern, however i felt like doing a base controller would be simple and would solve my problem.

My controller receives json and returns json response. Heres my base controller:

abstract class ApiController extends Controller
{
    use ApiResponser;

    protected $model;
    protected $storeRules, $updateRules, $deleteRules;

    public function store(Request $request)
    {
        $this->validate($request, $this->storeRules);

        $requestData = $request->get($this->model::table());
        if (!$this->model::insertAll($requestData))
            return $this->errorResponse('Could not insert data', 500);

        return $this->showAll($requestData, 201);
    }

    public function index()
    {
        $items = $this->model::all();
        return $this->showAll($items);
    }

    public function show($id)
    {
        $item = $this->model::findOrFail($id);
        return $this->showOne($item);
    }

    public function update(Request $request)
    {
        $this->validate($request, $this->updateRules);

        $requestData = $request->get($this->model::table());

        $updated = [];
        foreach ($requestData as $data) {
            $item = $this->model::findOrFail($data['id']);

            $item->fill($data);

            if ($item->isDirty()) {
                $item->save();
                array_push($updated, $item);
            }
        }

        return $this->showAll($updated, 200);

    }

    public function destroy(Request $request)
    {
        $this->validate($request, $this->deleteRules);
        $requestData = $request->input($this->model::table().'.*.id');

        $deleted = $this->model::destroy($requestData);

        return $this->showAll($deleted, 201);
    }

Here is how a child controller looks like:

class CourseController extends ApiController
{
    public function __construct()
    {
        $this->model = Course::class;
        $this->storeRules = Course::STORE_RULES;
        $this->updateRules = Course::UPDATE_RULES;
        $this->deleteRules = Course::DELETE_RULES;
    }

Please if anyone could review it or at least give me some advice i would appreciate. First time posting here in laracasts discussion, sorry if my post is poorly structured.