twoarmtom

Experience

6,030

1 Best Reply Awards

  • Member Since 6 Months Ago
  • 53 Lessons Completed
  • 4 Favorites

16th July, 2018

twoarmtom left a reply on Accessing The Users Data From Vue Component With API Routes • 4 days ago

@bulresearch I just removed the api.php resource routes and made regular web routes to the controller and I was able to pull in the data with axios. I think when I tried that earlier I was logged in as a user without any pivot table data so there was no data. I tried it again and it worked (with proper user data).

Route (protected by middleware for the user role)

Route::get('/attempts', '[email protected]');

This works now

public function index()
    {
        //
        $user = Auth::user();
        return response($user->competency()->orderBy('category')->get());
    }
mounted() {
        axios.get('/attempts')
        .then(response => this.comps = response.data);
    }

Thanks for your replies, I appreciate it, motivated me to keep at it! I'll hold off on Passport for now and see if this continues to work for what I need it for,

twoarmtom left a reply on Accessing The Users Data From Vue Component With API Routes • 4 days ago

@burlresearch The route is in the api.php

Route::resource('/attempts', 'AttemptsController');

Vue component file

<script>
export default {
    data() {
        return {
            comps: []
        }
    },
    mounted() {
        axios.get('/api/attempts')
        .then(response => this.comps = response.data);
    }
}
</script>

Everything works if I load data not associated with the user, but when I try to load data affiliated to the user I get > "Call to a member function competencies() on null"

15th July, 2018

twoarmtom left a reply on Laravel 5.4 Messaging Logout • 4 days ago

You should be able to create the session before the redirect.

public function authenticated(Request $request, $user)
{

    if ($user->confirmed != 1) {

      Auth::logout($request);
    session()->flash('logout', "You successfully logged out");

       return redirect('/login');

    }
    else{

        $request->session()->flash('status', 'You are successful log in!');

    }
}

And make sure you have the flash message displayed on your login page after redirection by including it somewhere on the page

@if ($flash = session('logout'))
            <strong>{{ $flash }}</strong>
@endif 

Wrap it in an alert box and fade it out if you want it to look nice (like bootstrap alert component).

twoarmtom left a reply on Accessing The Users Data From Vue Component With API Routes • 4 days ago

@burlresearch The controller is part of an axio api request, and it can't seem to utilize the current authenticated user. I'm trying access a list of competencies associated with the logged in user. I want them to be able to interact with them using ajax (axios) requests.

''' public function index() { // return response(Auth::user()->competencies()->orderBy('category')->get()); } '''

Fails because user is null.

twoarmtom started a new conversation Accessing The Users Data From Vue Component With API Routes • 4 days ago

I would like to have a users stored information accessible in a Vue component. However, when I use Auth::user() in the controller associated with the API resource (using a axios), it returns null. After extensive reading it seems like I may need to use Passport in order to use the current user in a controller function when the controller is part of an API.

Before I start going down that road I’m wondering if anyone knows another way to pass the current logged in user into the a API controller to retrieve their data? Or do I have to use Passport?

9th July, 2018

twoarmtom left a reply on Counting A Value Only If It Appears More Than Once In A Table. • 1 week ago

I found a way to do it by adding another column to the table that is set to true if the evaluation for the student and competency is given the value of 3 a second time.

In my controller, after evaluation is created:

if (SubmittedCompetency::where('competency_id', $evaluation->competency_id)->where('student_id', $evaluation->student_id)->where('evaluation', 3)->count() == 2) 
            {
                $evaluation->update([
                    'isEtP' => true
                ]);
            }

Then I run the following query in the view (because there is more than one student on in the page, and I had to do this for each student) the count I needed:

$completed_competencies = DB::table('submitted_competencies')
                                ->where('student_id', $student->id)
                                ->where('isEtP', 1)
                                ->count();

Blade:

{{ $completed_competencies }} Complete

8th July, 2018

twoarmtom left a reply on Counting A Value Only If It Appears More Than Once In A Table. • 1 week ago

@Danchez close, but I want to know when evaluation >2 for the distinct competency happens at least twice in the table. This example counts it even if it only appears once in the table.

twoarmtom left a reply on Counting A Value Only If It Appears More Than Once In A Table. • 1 week ago

@mhmd_daka Unfortunately, it does not work. And it ignore a few things. 1) I need it to be only competencies belong to a certain students. 2) I only want it to count the row belonging to the student and specific competency if the evaluation score was 3 more than once. In other words, the student received a perfect score (3) at least twice before it shows up on a report (which I'm displaying as a numeric count).

I tried adjusting the code to consider these things, and it counts all distinct competencies with evaluation of 3 belonging to a student, but it still counts it even it only happened once.

$completed_competencies = DB::table('submitted_competencies')
                                ->where('student_id', $student->id)
                                ->where('evaluation', 3)
                                ->groupBy('competency_id')
                                ->havingRaw('Count(competency_id) > 1')
                                ->count('competency_id');

With "competency_id", it seems to at least group the distinct competencies that occur more than once. If I use "evaluation" above for groupBy and havingRaw, it just does a total count where evaluations are 3, even if its the same competency multiple times.

twoarmtom left a reply on Counting A Value Only If It Appears More Than Once In A Table. • 1 week ago

@Danchez The query works for count how many evaluations has a value of 3 that belong to the student, but I only want the count if they have received the score of 3 more than once.

twoarmtom started a new conversation Counting A Value Only If It Appears More Than Once In A Table. • 1 week ago

I'm having trouble getting a count based on a special condition.

Basically, a "completed competency" is one that receives an evaluation of 3 at least two times. The following code works for counting how many distinct competencies have an evaluation of 3, but I want it to only count them if the distinct competency_id has an evaluation of 3 at least twice in the database for the specified student. Any suggestions?

$completed_competencies = DB::table('submitted_competencies')
                                ->where('student_id', 202)
                                ->where('evaluation', 3) // I want this to happen twice for the same competency_id before it is counted.
                                ->distinct('competency_id')
                                ->count('competency_id');
Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.