使用Laravel创建LMS并为学生分配课程

时间:2019-05-29 15:46:32

标签: php mysql laravel

我正在尝试使用Laravel创建一个简单的LMS系统。我在这里关注YouTube指南和QuickLMS存储库:https://github.com/LaravelDaily/QuickLMS/。大多数应用程序都可以正常运行,但是我想更改并删除其中一个功能。

我在为学生或用户分配课程的特定功能时遇到困难。每当学生在首页上打开课程时,顶部都会显示一个按钮,显示“购买课程”,这会加载Stripe弹出窗口供学生付款。付款完成后,会在course_student表中将课程分配给学生。

我从中借用的代码使用Stripe支付课程费用。我希望该按钮是“注册课程”按钮,而不是付款按钮。这样,当学生进入课程时,将显示一个注册课程的按钮,该按钮将学生添加到course_student表中,而无需任何付款信息。

CoursesController.php:

public function payment(Request $request)
    {
        $course = Course::findOrFail($request->get('course_id'));
        $this->createStripeCharge($request);
        $course->students()->attach(\Auth::id());
        return redirect()->back()->with('success', 'Payment completed successfully.');
    }
    private function createStripeCharge($request)
    {
        Stripe::setApiKey(env('STRIPE_API_KEY'));
        try {
            $customer = Customer::create([
                'email' => $request->get('stripeEmail'),
                'source'  => $request->get('stripeToken')
            ]);
            $charge = Charge::create([
                'customer' => $customer->id,
                'amount' => $request->get('amount'),
                'currency' => "usd"
            ]);
        } catch (\Stripe\Error\Base $e) {
            return redirect()->back()->withError($e->getMessage())->send();
        }
    }

course.blade.php:

<form action="{{ route('courses.payment') }}" method="POST">
                    <input type="hidden" name="course_id" value="{{ $course->id }}" />
                    <input type="hidden" name="amount" value="{{ $course->price * 100 }}" />
                    <script
                            src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                            data-key="{{ env('PUB_STRIPE_API_KEY') }}"
                            data-amount="{{ $course->price * 100 }}"
                            data-currency="usd"
                            data-name="Quick LMS"
                            data-label="Buy course (${{ $course->price }})"
                            data-description="Course: {{ $course->title }}"
                            data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
                            data-locale="auto"
                            data-zip-code="false">
                    </script>

web.php:

Route::post('course/payment', ['uses' => 'CoursesController@payment', 'as' => 'courses.payment']);

我尝试通过删除对Stripe的任何引用来更改所有这些内容,而是创建一个看起来像这样的新函数:

CoursesController.php:

 public function register (Request $request)
    {
        $this->createSubscription($request);
        return redirect()->back()->with('success', 'Payment completed successfully.');

    }

    private function createSubscription($request)
    {
        $course = Course::findOrFail($request->get('course_id'));
        $course->students()->attach(\Auth::id());
    }

但是,这只会从Laravel生成一条“页面已过期”消息,而我不知道如何仅删除“条纹”按钮。

我试图替换整个“付款”功能而没有成功。我认为这是我想要的代码,但是显然它需要进行一些更改才能工作,但是我无法弄清楚:

        $course->students()->attach(\Auth::id());

1 个答案:

答案 0 :(得分:0)

我自己通过创建两个新功能解决了这个问题: CoursesController.php:

BlockSource

以及Course.blade.php上的以下表格:

private async void ReceiveUdpPackets()
{
    await Task.Run(async () =>
    {
        try
        {
            while (receivePackets)
            {
                Log.Info("Listening for UDP packets...");

                listener.Client.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.BlockSource, false);
                byte[] bytes = listener.Receive(ref groupEP);
                listener.Client.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.BlockSource, true);

                Log.Info($"Received UDP broadcast from {groupEP} :");
                Log.Info($"{Encoding.ASCII.GetString(bytes, 0, bytes.Length)}");

                var bString = BitConverter.ToString(bytes).Replace("-", "");
                Log.Info("UDP ByteString: " + bString);

                DecodeByteString(bString);
                await Task.Delay(5000);
            }
        }
        catch (Exception ex)
        {
            Log.Error("There was an issue decoding the UDP packet: " + ex.ToString());
        }
    });
}

Web.php

public function register (Request $request)
    {
        $course = Course::findOrFail($request->get('course_id'));
        $this->createSubscription($request);
        $course->students()->attach(\Auth::id());
        return redirect()->back()->with('success', 'Payment completed successfully.');


    }

    private function createSubscription($request)
    {
        return;

    }

也许可以用更好的方法来完成,但这确实是我想要的。