Call to undefined method App\Models\User::createSetupIntent()

huangapple go评论93阅读模式
英文:

Call to undefined method App\Models\User::createSetupIntent()

问题

I am trying to implement subscriptions with stripe integration using cashier packages in my Laravel application. I am facing this error whenever I select a plan.

My PlanController.php file code is as follows:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http Request;
  4. use App Models Plan;
  5. class PlanController extends Controller
  6. {
  7. public function index()
  8. {
  9. $plans = Plan get();
  10. return view("plans", compact("plans"));
  11. }
  12. public function show(Plan plan, Request request)
  13. {
  14. $intent = auth()->user()->createSetupIntent();
  15. return view("subscription", compact("plan", "intent"));
  16. }
  17. public function subscription(Request request)
  18. {
  19. $plan = Plan::find($request->plan);
  20. $subscription = $request->user()->newSubscription($request->plan, $plan->stripe_plan)
  21. ->create($request->token);
  22. return view("subscription_success");
  23. }
  24. }

After searching for solutions online, I am still stuck at the same problem. Kindly help.

Some other models that might help you in finding a solution are as follows:

Plan.php The model file for plans:

  1. <?php
  2. namespace App Models;
  3. use Illuminate\Database Eloquent Factories HasFactory;
  4. use Illuminate\Database Eloquent Model;
  5. class Plan extends Model
  6. {
  7. use HasFactory;
  8. protected $fillable = [
  9. 'name',
  10. 'slug',
  11. 'stripe_plan',
  12. 'price',
  13. 'description',
  14. ];
  15. public function getRouteKeyName()
  16. {
  17. return 'slug';
  18. }
  19. }

Model file for users User.php:

  1. <?php
  2. namespace App Models;
  3. use Illuminate Contracts Auth MustVerifyEmail;
  4. use Illuminate\Database Eloquent Factories HasFactory;
  5. use Illuminate Foundation Auth User as Authenticatable;
  6. use Illuminate Notifications Notifiable;
  7. use Laravel Sanctum HasApiTokens;
  8. use Laravel Cashier Billable;
  9. class User extends Authenticatable
  10. {
  11. use HasApiTokens, HasFactory, Notifiable;
  12. protected $fillable = [
  13. 'name',
  14. 'email',
  15. 'password',
  16. 'role',
  17. 'plan',
  18. 'created_by',
  19. 'status',
  20. ];
  21. protected $hidden = [
  22. 'password',
  23. 'remember_token',
  24. ];
  25. protected $casts = [
  26. 'email_verified_at' => 'datetime',
  27. ];
  28. }

(Note: I have removed HTML escape codes from your provided code for better readability.)

英文:

I am trying to implement subscriptions with stripe integration using cashier packages in my Laravel application. I am facing this error whenever I select a plan.

My PlanController.php file code is as followed

  1. &lt;?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Models\Plan;
  5. class PlanController extends Controller
  6. {
  7. /**
  8. * Write code on Method
  9. *
  10. * @return response()
  11. */
  12. public function index()
  13. {
  14. $plans = Plan::get();
  15. return view(&quot;plans&quot;, compact(&quot;plans&quot;));
  16. }
  17. /**
  18. * Write code on Method
  19. *
  20. * @return response()
  21. */
  22. public function show(Plan $plan, Request $request)
  23. {
  24. $intent = auth()-&gt;user()-&gt;createSetupIntent();
  25. return view(&quot;subscription&quot;, compact(&quot;plan&quot;, &quot;intent&quot;));
  26. }
  27. /**
  28. * Write code on Method
  29. *
  30. * @return response()
  31. */
  32. public function subscription(Request $request)
  33. {
  34. $plan = Plan::find($request-&gt;plan);
  35. $subscription = $request-&gt;user()-&gt;newSubscription($request-&gt;plan, $plan-&gt;stripe_plan)
  36. -&gt;create($request-&gt;token);
  37. return view(&quot;subscription_success&quot;);
  38. }
  39. }

After searching for solutions online I am still stuck at the same problem. Kindly help.

Some other models that might help you in finding solution are as followed:

Plan.php The model file for plans

  1. &lt;?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. class plan extends Model
  6. {
  7. use HasFactory;
  8. protected $fillable = [
  9. &#39;name&#39;,
  10. &#39;slug&#39;,
  11. &#39;stripe_plan&#39;,
  12. &#39;price&#39;,
  13. &#39;description&#39;,
  14. ];
  15. /**
  16. * Write code on Method
  17. *
  18. * @return response()
  19. */
  20. public function getRouteKeyName()
  21. {
  22. return &#39;slug&#39;;
  23. }
  24. }

Model file for users User.php

  1. &lt;?php
  2. namespace App\Models;
  3. // use Illuminate\Contracts\Auth\MustVerifyEmail;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Foundation\Auth\User as Authenticatable;
  6. use Illuminate\Notifications\Notifiable;
  7. use Laravel\Sanctum\HasApiTokens;
  8. use Laravel\Cashier\Billable;
  9. class User extends Authenticatable
  10. {
  11. use HasApiTokens, HasFactory, Notifiable;
  12. /**
  13. * The attributes that are mass assignable.
  14. *
  15. * @var array&lt;int, string&gt;
  16. */
  17. protected $fillable = [
  18. &#39;name&#39;,
  19. &#39;email&#39;,
  20. &#39;password&#39;,
  21. &#39;role&#39;,
  22. &#39;plan&#39;,
  23. &#39;created_by&#39;,
  24. &#39;status&#39;,
  25. ];
  26. /**
  27. * The attributes that should be hidden for serialization.
  28. *
  29. * @var array&lt;int, string&gt;
  30. */
  31. protected $hidden = [
  32. &#39;password&#39;,
  33. &#39;remember_token&#39;,
  34. ];
  35. /**
  36. * The attributes that should be cast.
  37. *
  38. * @var array&lt;string, string&gt;
  39. */
  40. protected $casts = [
  41. &#39;email_verified_at&#39; =&gt; &#39;datetime&#39;,
  42. ];
  43. }

答案1

得分: 2

将Laravel Cashier中的Billable特性添加到您的User模型中。

  1. use Laravel\Cashier\Billable;
  2. class User extends Authenticatable
  3. {
  4. use Billable, HasApiTokens, HasFactory, Notifiable;
  5. }
英文:

Add the Billable trait from Laravel Cashier to your User model.

  1. use Laravel\Cashier\Billable;
  2. class User extends Authenticatable
  3. {
  4. use Billable, HasApiTokens, HasFactory, Notifiable;
  5. }

huangapple
  • 本文由 发表于 2023年2月6日 21:31:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定