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

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

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:

<?php
      
namespace App\Http\Controllers;
      
use Illuminate\Http Request;
use App Models Plan;

class PlanController extends Controller
{
    public function index()
    {
        $plans = Plan get();
      
        return view("plans", compact("plans"));
    }  

    public function show(Plan plan, Request request)
    {
        $intent = auth()->user()->createSetupIntent();
      
        return view("subscription", compact("plan", "intent"));
    }

    public function subscription(Request request)
    {
        $plan = Plan::find($request->plan);
      
        $subscription = $request->user()->newSubscription($request->plan, $plan->stripe_plan)
                        ->create($request->token);
      
        return view("subscription_success");
    }
}

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:

<?php

namespace App Models;

use Illuminate\Database Eloquent Factories HasFactory;
use Illuminate\Database Eloquent Model;

class Plan extends Model
{
    use HasFactory;
    protected $fillable = [
        'name',
        'slug',
        'stripe_plan',
        'price',
        'description',
    ];

    public function getRouteKeyName()
    {
        return 'slug';
    }
}

Model file for users User.php:

<?php

namespace App Models;

use Illuminate Contracts Auth MustVerifyEmail;
use Illuminate\Database Eloquent Factories HasFactory;
use Illuminate Foundation Auth User as Authenticatable;
use Illuminate Notifications Notifiable;
use Laravel Sanctum HasApiTokens;
use Laravel Cashier Billable;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $fillable = [
        'name',
        'email',
        'password',
        'role',
        'plan',
        'created_by',
        'status',
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

(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

&lt;?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Plan;


class PlanController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $plans = Plan::get();
  
        return view(&quot;plans&quot;, compact(&quot;plans&quot;));
    }  
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function show(Plan $plan, Request $request)
    {
        $intent = auth()-&gt;user()-&gt;createSetupIntent();
  
        return view(&quot;subscription&quot;, compact(&quot;plan&quot;, &quot;intent&quot;));
    }
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function subscription(Request $request)
    {
        $plan = Plan::find($request-&gt;plan);
  
        $subscription = $request-&gt;user()-&gt;newSubscription($request-&gt;plan, $plan-&gt;stripe_plan)
                        -&gt;create($request-&gt;token);
  
        return view(&quot;subscription_success&quot;);
    }
}

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

&lt;?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class plan extends Model
{
    use HasFactory;
    protected $fillable = [
        &#39;name&#39;,
        &#39;slug&#39;,
        &#39;stripe_plan&#39;,
        &#39;price&#39;,
        &#39;description&#39;,
    ];
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function getRouteKeyName()
    {
        return &#39;slug&#39;;
    }
}

Model file for users User.php

&lt;?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Cashier\Billable;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array&lt;int, string&gt;
     */
    protected $fillable = [
        &#39;name&#39;,
        &#39;email&#39;,
        &#39;password&#39;,
        &#39;role&#39;,
        &#39;plan&#39;,
        &#39;created_by&#39;,
        &#39;status&#39;,
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array&lt;int, string&gt;
     */
    protected $hidden = [
        &#39;password&#39;,
        &#39;remember_token&#39;,
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array&lt;string, string&gt;
     */
    protected $casts = [
        &#39;email_verified_at&#39; =&gt; &#39;datetime&#39;,
    ];
}

答案1

得分: 2

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

use Laravel\Cashier\Billable;
 
class User extends Authenticatable
{
    use Billable, HasApiTokens, HasFactory, Notifiable;
}
英文:

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

use Laravel\Cashier\Billable;
 
class User extends Authenticatable
{
    use Billable, HasApiTokens, HasFactory, Notifiable;
}

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:

确定