andy0517

andy0517

Member Since 4 Months Ago

Experience Points
370
Total
Experience

4,630 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
0
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 1
370 XP
Apr
05
4 months ago
Activity icon

Replied to Laravel Passport API, Method Attempt Does Not Exist.

@tisuchi

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('login','[email protected]');
Route::post('register','[email protected]');

Route::group(['middleware' => 'auth:member-api'], function(){
  Route::post('/member', '[email protected]');
});
Activity icon

Started a new Conversation Laravel Passport API, Method Attempt Does Not Exist.

Currently I am using the passport to do the API authentication. While I doing the login function, it returns me BadMethodCallException method attempt does not exist.

public function login(Request $request)
    {

      $validator = Validator::make($request->all(), [
          'email' => 'required|email',
          'password' => 'required',
        ]);
      if ($validator->fails()) {
        return response()->json(['error'=>$validator->errors()], 401);
      }

      $credentials = $request->only('email', 'password');

      if(Auth::guard('member-api')->attempt($credentials))
      {
        //return response()->json(['sucess'=>'Success'], 401);
        $user = Auth::guard('member-api')->user();
        $success['token'] =  $user->createToken('asApp')->accessToken;
        return response()->json(['success' => $success], 200);
      }else {
        return response()->json(['error'=>'Email or password incorrect'], 401);
      }
'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
        'member-api' => [
          'driver' => 'passport',
          'provider' => 'members',
        ],
    ],

it seems like the guard in front of attempt cause the problem, but i can't find the solution to solve it. Anyone have idea about it?

Apr
04
4 months ago
Activity icon

Replied to Laravel API Authentication Return {"message": "Unauthenticated."} But Not User Data.

what I mean is why the checking function if(Auth::guard('member-api')->attempt($credentials)) cannot work after I set the member-api driver to passport.

Activity icon

Replied to Laravel API Authentication Return {"message": "Unauthenticated."} But Not User Data.

I don't really understand what you explaining, can u provide some examples or teach me how to solve the problem?

Activity icon

Replied to Laravel API Authentication Return {"message": "Unauthenticated."} But Not User Data.

I got another problem is I just realized my guard is set to session. After I change it back to passport.

$credentials = $request->only('email', 'password');

      if(Auth::guard('member-api')->attempt($credentials))
      {
        $user = Auth::guard('member-api')->user();
        $success['token'] =  $user->createToken('asApp')->accessToken;
        return response()->json(['success' => $success], 200);
      }

This function cannot work and return

BadMethodCallException: Method attempt does not exist. in file D:\fypLaravel\vendor\laravel\framework\src\Illuminate\Support\Traits\Macroable.php on line 96
Apr
03
4 months ago
Activity icon

Replied to Laravel API Authentication Return {"message": "Unauthenticated."} But Not User Data.

The personal access client is a must, right? I think I forgot about it.

Activity icon

Replied to Laravel API Authentication Return {"message": "Unauthenticated."} But Not User Data.

I just realised i set my guards of member-api driver as session. If I change it to passport, the login cannot even work because (Method attempt does not exist.). Any way to solve the attempt function

Activity icon

Started a new Conversation Laravel API Authentication Return {"message": "Unauthenticated."} But Not User Data.

I am new to Laravel and I am doing a react-native program with Laravel backend. In this case, the data table that I used is the Member table but not the user table. I can successfully log into the react-native by member email & password, but fail to get the data from user Laravel. It returns

{"message": "Unauthenticated."}

APIAuthController

public function login(Request $request)
    {

      $validator = Validator::make($request->all(), [
          'email' => 'required|email',
          'password' => 'required',
        ]);
      if ($validator->fails()) {
        return response()->json(['error'=>$validator->errors()], 401);
      }

      $credentials = $request->only('email', 'password');

      if(Auth::guard('member-api')->attempt($credentials))
      {
        $user = Auth::guard('member-api')->user();
        $success['token'] =  $user->createToken('asApp')->accessToken;
        return response()->json(['success' => $success], 200);
      }else {
        return response()->json(['error'=>'Email or password incorrect'], 401);
      }
    }
    public function user()
    {
        return response()->json(['user' => Auth::guard('member-api')->user()], 200);
    }

The login function is work and return the accesstoken

api.php

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('login','[email protected]');
Route::post('register','[email protected]');

Route::group(['middleware' => 'auth:member-api'], function(){
  Route::post('user', '[email protected]');
});

auth.php

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
        'member-api' => [
          'driver' => 'session',
          'provider' => 'members',
        ],
    ],

Here is React Native function is u need.

fetchUserInformation = async () => {
        const token = await AsyncStorage.getItem('id_token')
        console.log(token)
        const response = await fetch('http://192.168.0.025:8888/api/user', {
            method: 'GET',
            headers:{
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': 'Bearer '. token
            }
        });
        const user = await response.json();
        console.log(user)
    }

I have tried many way but still can't solve. Anyone facing this problem before? Its about return of user. I can provide anything else that you think is affected.

Apr
01
4 months ago
Activity icon

Started a new Conversation Authentication API Return Wrong Result (URGENT! Looking For Help)

Hi, I'm new to laravel. I doing the API authentication right now. And I found my API didn't work as what shd be. I followed the guide of building it but in the end there is no error but wrong output. HERE IS MY CODE I can't find whats the error from it.

auth.php

'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
        'member-api' => [
          'driver' => 'session',
          'provider' => 'members',
        ],
    ],
'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'members' => [
            'driver' => 'eloquent',
            'model' => App\Member::class,
        ],

APIController

class APIController extends Controller
{
    //
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required',
            'c_password' => 'required|same:password',
            'contact_no' => 'required'
          ]);
        if ($validator->fails()) {
          return response()->json(['error'=>$validator->errors()], 401);
        }

        $member = Member::create([
          'name' => $request->name,
          'email' => $request->email,
          'password' => bcrypt($request->password),
          'contact_no' => $request->contact_no
        ]);
        $success['token'] =  $member->createToken('asApp')->accessToken;
        $success['name'] =  $member->name;
        return response()->json(['success'=>$success], 200);

        //return response()->json(['success'=>'work'], 200);
    }

    public function login(Request $request)
    {
      $validator = Validator::make($request->all(), [
          'email' => 'required|email',
          'password' => 'required',
        ]);
      if ($validator->fails()) {
        return response()->json(['error'=>$validator->errors()], 401);
      }

      $credentials = $request->only('email', 'password');
      if(Auth::guard('member-api')->attempt($credentials))
      {
        $user = Auth::guard('member-api')->user();
        $success['token'] =  $user->createToken('asApp')->accessToken;
        return response()->json(['success' => $success], 200);
      }else {
        return response()->json(['error'=>'Email or password incorrect'], 401);

      }
    }
    public function details()
    {
      return response()->json(['user' => auth()->member()], 200);
    }

}

}

api.php

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('login','[email protected]');
Route::post('register','[email protected]');

Route::group(['middleware' => 'auth:api'], function(){
	Route::post('userDetails', '[email protected]');
});
Activity icon

Started a new Conversation Laravel Passport

Hi, I am new to Laravel. I face the problem in using the API authentication passport. It shows me undefined attempt function. I am not using the user table for API, so I had created a new guard and provider for frontend member

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'passport',
            'provider' => 'users',
        ],
        'member-api' => [
          'driver' => 'passport',
          'provider' => 'members',
        ],
    ],
'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'members' => [
            'driver' => 'eloquent',
            'model' => App\Member::class,
        ],

API Controller

public function login(Request $request)
    {

      if(Auth::guard('member-api')->attempt(['email' => $request->email, 'password' => $request->password]))
      {
        $member = Auth::member();
           $success['token'] =  $member->createToken('MyApp')->accessToken;
           return response()->json(['success' => $success], 200);
      }else {
        return response()->json(['error'=>'Unauthorised'], 401);

      }
    }

While I test my API login, it shows me error of

BadMethodCallException
Method attempt does not exist.

Does anyone have an idea about it? TQ

Activity icon

Started a new Conversation Laravel Passport Api Undefined Method Attempt

Im using passport for creating the API authentication. I facing the problem of showing the error in the login function. It shows the Error

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to undefined method Illuminate\Auth\TokenGuard::attempt()

Here is my controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Member;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class APIController extends Controller
{
    //
    public function register(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
            'email' => 'required|email',
            'password' => 'required',
            'c_password' => 'required|same:password',
            'contact_no' => 'required'
          ]);
        if ($validator->fails()) {
          return response()->json(['error'=>$validator->errors()], 401);
        }

        $member = Member::create([
          'name' => $request->name,
          'email' => $request->email,
          'password' => bcrypt($request->password),
          'contact_no' => $request->contact_no
        ]);
        $success['token'] =  $member->createToken('asApp')->accessToken;
        $success['name'] =  $member->name;
        return response()->json(['success'=>$success], 200);

        //return response()->json(['success'=>'work'], 200);
    }

    public function login(Request $request)
    {

      if(Auth::guard('member-api')->attempt(['email' => $request->email, 'password' => $request->password]))
      {
        $member = Auth::member();
           $success['token'] =  $member->createToken('MyApp')->accessToken;
           return response()->json(['success' => $success], 200);
      }else {
        return response()->json(['error'=>'Unauthorised'], 401);

      }
    }
    public function details()
    {
      return response()->json(['user' => auth()->member()], 200);
    }

}

The error is the attempt function in the login. Anyone know what wrong with it? or ways to solve it

Activity icon

Replied to Laravel 5.5 Observer

Oh ya! what a silly mistake. I kept focusing on finding in the code but don't really focus on the import!! Tq so much sir!

Activity icon

Started a new Conversation Laravel 5.5 Observer

Hi I'm new to laravel and using laravel 5.5, I doing something using the observer. But while i run the code, it show me

Type error: Argument 1 passed to App\Observers\RecurrenceObserver::created() must be an instance of App\Observers\Activity, instance of App\Activity given

I don't know whats wrong with this. Here is my code.

<?php

namespace App\Observers;

use App\Avtivity;
use Carbon\Carbon;

class RecurrenceObserver
{
    /**
     *
     *
     * @param  \App\Activity  $activity
     * @return void
     */
    public function created(Activity $activity)
    {
        if(!$activity->activity()->exists())
        {
            $recurrences = [
                'daily'     => [
                    'times'     => 365,
                    'function'  => 'addDay'
                ],
                'weekly'    => [
                    'times'     => 52,
                    'function'  => 'addWeek'
                ],
                'monthly'    => [
                    'times'     => 12,
                    'function'  => 'addMonth'
                ]
            ];
            $startTime = Carbon::parse($activity->start_time);

            $recurrence = $recurrences[$activity->recurrence] ?? null;

            if($recurrence)
                for($i = 0; $i < $recurrence['times']; $i++)
                {
                    $startTime->{$recurrence['function']}();
                    $activity->activities()->create([
                        'name'          => $activity->name,
                        'start_time'    => $startTime,
                        'recurrence'    => $activity->recurrence,
                    ]);
                }
        }
    }

    /**
     * Handle the activity "updated" event.
     *
     * @param  \App\Activity  $activity
     * @return void
     */
    public function updated(Activity $activity)
    {
        if($activity->activities()->exists() || $activity->activity)
        {
            $startTime = Carbon::parse($activity->getOriginal('start_time'))->diffInSeconds($activity->start_time, false);
            if($activity->activity)
                $childActivities = $activity->activity->activities()->whereDate('start_time', '>', $activity->getOriginal('start_time'))->get();
            else
                $childActivities = $activity->activities;

            foreach($childActivities as $childActivity)
            {
                if($startTime)
                    $childActivity->start_time = Carbon::parse($childActivity->start_time)->addSeconds($startTime);
                if($activity->isDirty('name') && $childActivity->name == $activity->getOriginal('name'))
                    $childActivity->name = $activity->name;
                $childActivity->saveQuietly();
            }
        }

        if($activity->isDirty('recurrence') && $activity->recurrence != 'none')
            self::created($activity);
    }

    /**
     * Handle the Activity "deleted" event.
     *
     * @param  \App\Activity  $activity
     * @return void
     */
    public function deleted(Activity $activity)
    {
        if($activity->activities()->exists())
            $activities = $activity->activities()->pluck('id');
        else if($activity->activity)
            $activities = $activity->activity->activities()->whereDate('start_time', '>', $activity->start_time)->pluck('id');
        else
            $activities = [];

            Activity::whereIn('id', $activities)->delete();
    }
}

Controller


    public function store(Request $request)
    {
        Activity::create($request->all());

        return redirect()->route('activities.index');
    }