首页 > 解决方案 > 数据透视表的问题,使用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');
    }
}

标签: mysqllaraveleloquentlaravel-7braintree

解决方案


推荐阅读