英文:
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
<?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("plans", compact("plans"));
}
/**
* Write code on Method
*
* @return response()
*/
public function show(Plan $plan, Request $request)
{
$intent = auth()->user()->createSetupIntent();
return view("subscription", compact("plan", "intent"));
}
/**
* Write code on Method
*
* @return response()
*/
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 solution are as followed:
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',
];
/**
* Write code on Method
*
* @return response()
*/
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;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'email',
'password',
'role',
'plan',
'created_by',
'status',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
答案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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论