mysql - 数据透视表的问题,使用braintree附加不起作用
问题描述
我正在尝试将音乐家 ID、赞助 ID 和结束日期附加到数据透视表 (musician_sponsorship) 但我收到此错误:
这是我需要将数据附加到数据透视表的功能,一旦使用braintree 付款顺利,我需要用所有数据填充数据透视表。
public function payment(Request $request, Sponsorship $sponsorship) {
$data = $request->all();
// dd($data['price']);
$gateway = new Braintree\Gateway([
'environment' => config('services.braintree.environment'),
'merchantId' => config('services.braintree.merchantId'),
'publicKey' => config('services.braintree.publicKey'),
'privateKey' => config('services.braintree.privateKey')
]);
$amount = $data['price'];
// dd($amount);c
$nonce = 'fake-valid-nonce';
$result = $gateway->transaction()->sale([
'amount' => $amount,
'paymentMethodNonce' => $nonce,
// 'customer' => [
// 'firstName' => 'Tony',
// 'lastName' => 'Stark',
// 'email' => 'tony@avengers.com',
// ],
'options' => [
'submitForSettlement' => true
]
]);
if ($result->success) {
$transaction = $result->transaction;
// header("Location: transaction.php?id=" . $transaction->id);
// $musician = Musician::with('sponsorships')->find(Auth::id());
// $end_date = new DateTime();
$musician = Musician::where('user_id', Auth::id())->first();
$musician->sponsorships()->attach($musician, [
'sponsorship_id' => 1,
'musician_id' => $musician->id,
'end_date' => Carbon::now()
]);
return back()->with('success_message', 'Transaction successful. The ID is: '. $transaction->id);
} else {
$errorString = "";
foreach ($result->errors->deepAll() as $error) {
$errorString .= 'Error: ' . $error->code . ": " . $error->message . "\n";
}
// $_SESSION["errors"] = $errorString;
// header("Location: index.php");
return back()->withErrors('An error occurred with the message: '.$result->message);
}
}
赞助模式
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Sponsorship extends Model
{
protected $fillable = [
'name',
'price',
'duration',
'description'
];
public function musicians() {
return $this->belongsToMany('App\Musician');
}
}
音乐家模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Musician extends Model
{
protected $fillable = [
'user_id',
'stagename',
'slug',
'description',
'bio',
'services',
'typology',
'cover'
];
public function user() {
return $this->hasOne('App\User');
}
public function genres() {
return $this->belongsToMany('App\Genre');
}
public function sponsorships() {
return $this->belongsToMany('App\Sponsorship');
}
public function messages() {
return $this->belongsToMany('App\Message');
}
public function reviews() {
return $this->belongsToMany('App\Review');
}
}
数据透视表迁移
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateMusicianSponsorshipTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('musician_sponsorship', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->dateTime('end_date');
$table->unsignedBigInteger('musician_id');
$table->foreign('musician_id')
->references('id')
->on('musicians')
->onDelete('CASCADE');
$table->unsignedBigInteger('sponsorship_id');
$table->foreign('sponsorship_id')
->references('id')
->on('sponsorships')
->onDelete('CASCADE');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('musician_sponsorship');
}
}
解决方案
推荐阅读
- java - 如何在 Java 中转换 TensorProto 中的图像
- javascript - 不满足条件时在 IF 语句中执行代码
- javascript - 按委员会排序数组,然后按用户角色排序
- javascript - 将变量设置为自身加上 JavaScript 中的另一个变量
- javascript - 我在为服务器工作的节点中使用socket.io,但无法连接到我的移动端,作为客户端反应本机
- python - Python中的随机选择失败
- jquery - 更改没有 ID 的元素的类
- java - 二维数组中每一行的总和
- javascript - 对象无法获取方法或属性“getCars”
- c - 将结构数组传递给函数以进行输入