无法将课程上传到 Laravel 项目中的数据库

问题描述 投票:0回答:1

我正在进行 Laravel 项目。特征:

Laravel Framework 10.48.12
Composer version 2.7.7 2024-06-10 22:11:12
PHP 8.3.7 (cli) (built: May  8 2024 08:56:56) (ZTS Visual C++ 2019 x64) Copyright (c) The PHP Group Zend Engine v4.3.7, Copyright (c) Zend Technologies
Intervention Image (^2.7)
我无法将课程上传到数据库,也没有显示错误消息。按提交后,它似乎只是刷新到 /add/course 页面,该页面应该重定向到 /all/course 页面。 这是代码

add_course.blade.php

@extends('instructor.instructor_dashboard')
@section('instructor')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>

<div class="page-content">
    <!--breadcrumb-->
    <div class="page-breadcrumb d-none d-sm-flex align-items-center mb-3"> 
        <div class="ps-3">
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-0 p-0">
                    <li class="breadcrumb-item"><a href="javascript:;"><i class="bx bx-home-alt"></i></a>
                    </li>
                    <li class="breadcrumb-item active" aria-current="page">Add Course</li>
                </ol>
            </nav>
        </div>
         
    </div>
    <!--end breadcrumb-->
 
    <div class="card">
        <div class="card-body p-4">
            <h5 class="mb-4">Add Course</h5>
            
            <form id="myForm" action="{{ route('store.course') }}" method="post" class="row g-3" enctype="multipart/form-data">
                @csrf

                <div class="form-group col-md-6">
                    <label for="input1" class="form-label">Course Name</label>
                    <input type="text" name="course_name" class="form-control" id="input1"  >
                </div>

                <div class="form-group col-md-6">
                    <label for="input1" class="form-label">Course Title </label>
                    <input type="text" name="course_title" class="form-control" id="input1"  >
                </div>

                 

                <div class="form-group col-md-6">
                    <label for="input2" class="form-label">Course Image </label>
                    <input class="form-control" name="course_image" type="file" id="image">
                </div>

                <div class="col-md-6"> 
                    <img id="showImage" src="{{ url('upload/no_image.jpg')}}" alt="Admin" class="rounded-circle p-1 bg-primary" width="100">  
                </div>

                
                <div class="form-group col-md-6">
                    <label for="input1" class="form-label">Course Intro Video </label>
                    <input type="file" name="video" class="form-control"  accept="video/mp4, video/webm" >
                </div>

                <div class="form-group col-md-6">

                </div>

            <div class="form-group col-md-6">
                <label for="input1" class="form-label">Course Category </label>
                <select name="category_id" class="form-select mb-3" aria-label="Default select example">
                    <option selected="" disabled>Open this select menu</option>
                    @foreach ($categories as $cat) 
                    <option value="{{ $cat->id }}">{{ $cat->category_name }}</option>
                    @endforeach
                    
                </select>
            </div>


            <div class="form-group col-md-6">
                <label for="input1" class="form-label">Course Subcategory </label>
                <select name="subcategory_id" class="form-select mb-3" aria-label="Default select example">
                    <option> </option> 
                    
                </select>
            </div>


            <div class="form-group col-md-6">
                <label for="input1" class="form-label">Certificate Available </label>
                <select name="certificate" class="form-select mb-3" aria-label="Default select example">
               <option selected="" disabled>Open this select menu</option> 
                    <option value="Yes">Yes</option>
                    <option value="No">No</option>
                </select>
            </div>

            <div class="form-group col-md-6">
                <label for="input1" class="form-label">Course Label </label>
                <select name="label" class="form-select mb-3" aria-label="Default select example">
               <option selected="" disabled>Open this select menu</option> 
                    <option value="Begginer">Begginer</option>
                    <option value="Middle">Middle</option>
                    <option value="Advance">Advance</option>
                </select>
            </div>


            <div class="form-group col-md-3">
                <label for="input1" class="form-label">Course Price </label>
                <input type="text" name="selling_price" class="form-control" id="input1"  >
            </div>


            <div class="form-group col-md-3">
                <label for="input1" class="form-label">Discount Price </label>
                <input type="text" name="discount_price" class="form-control" id="input1"  >
            </div>


            <div class="form-group col-md-3">
                <label for="input1" class="form-label">Duration </label>
                <input type="text" name="duration" class="form-control" id="input1"  >
            </div>


            <div class="form-group col-md-3">
                <label for="input1" class="form-label">Resources </label>
                <input type="text" name="resources" class="form-control" id="input1"  >
            </div>

            <div class="form-group col-md-12">
                <label for="input1" class="form-label">Course Prerequisites </label>
                <textarea name="prerequisites" class="form-control" id="input11" placeholder="Prerequisites ..." rows="3"></textarea>
            </div>

            <div class="form-group col-md-12">
                <label for="input1" class="form-label">Course Description </label>
                <textarea name="description" class="form-control" id="myeditorinstance"></textarea>
            </div>


            <p>Course Goals </p>
             
<!--   //////////// Goal Option /////////////// -->

        <div class="row add_item">
        
            <div class="col-md-6">
                  <div class="mb-3">
                        <label for="goals" class="form-label"> Goals </label>
                        <input type="text" name="course_goals[]" id="goals" class="form-control" placeholder="Goals ">
                  </div>
            </div>
            <div class="form-group col-md-6" style="padding-top: 30px;">
                  <a class="btn btn-success addeventmore"><i class="fa fa-plus-circle"></i> Add More..</a>
            </div>
     </div> <!---end row-->

<!--   //////////// End Goal Option /////////////// -->


            <hr>
            <div class="row">

                <div class="col-md-4">
                    <div class="form-check">
        <input class="form-check-input" type="checkbox" name="bestseller" value="1" id="flexCheckDefault">
          <label class="form-check-label" for="flexCheckDefault">BestSeller</label>
                    </div>
                </div>


             <div class="col-md-4">
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="featured" value="1" id="flexCheckDefault">
                    <label class="form-check-label" for="flexCheckDefault">Featured</label>
                </div>
            </div>

            <div class="col-md-4">
                <div class="form-check">
                    <input class="form-check-input" type="checkbox" name="highestrated" value="1" id="flexCheckDefault">
                    <label class="form-check-label" for="flexCheckDefault">Highest Rated</label>
                </div>
            </div>

            </div>

             
                <div class="col-md-12">
                    <div class="d-md-flex d-grid align-items-center gap-3">
          <button type="submit" class="btn btn-primary px-4">Save Changes</button>
                      
                    </div>
                </div>
            </form>
        </div>
    </div>
 
   
</div>


<!--========== Start of add multiple class with ajax ==============-->
<div style="visibility: hidden">
    <div class="whole_extra_item_add" id="whole_extra_item_add">
       <div class="whole_extra_item_delete" id="whole_extra_item_delete">
          <div class="container mt-2">
             <div class="row">
               
                
                <div class="form-group col-md-6">
                   <label for="goals">Goals</label>
                   <input type="text" name="course_goals[]" id="goals" class="form-control" placeholder="Goals  ">
                </div>
                <div class="form-group col-md-6" style="padding-top: 20px">
                   <span class="btn btn-success btn-sm addeventmore"><i class="fa fa-plus-circle">Add</i></span>
                   <span class="btn btn-danger btn-sm removeeventmore"><i class="fa fa-minus-circle">Remove</i></span>
                </div>
             </div>
          </div>
       </div>
    </div>
 </div>      
 
  
 <!----For Section-------->
 <script type="text/javascript">
    $(document).ready(function(){
       var counter = 0;
       $(document).on("click",".addeventmore",function(){
             var whole_extra_item_add = $("#whole_extra_item_add").html();
             $(this).closest(".add_item").append(whole_extra_item_add);
             counter++;
       });
       $(document).on("click",".removeeventmore",function(event){
             $(this).closest("#whole_extra_item_delete").remove();
             counter -= 1
       });
    });
 </script>
 <!--========== End of add multiple class with ajax ==============-->



<script type="text/javascript">
        
    $(document).ready(function(){
        $('select[name="category_id"]').on('change', function(){
            var category_id = $(this).val();
            if (category_id) {
                $.ajax({
                    url: "{{ url('/subcategory/ajax') }}/"+category_id,
                    type: "GET",
                    dataType:"json",
                    success:function(data){
                        $('select[name="subcategory_id"]').html('');
                        var d =$('select[name="subcategory_id"]').empty();
                        $.each(data, function(key, value){
                            $('select[name="subcategory_id"]').append('<option value="'+ value.id + '">' + value.subcategory_name + '</option>');
                        });
                    },

                }); 
            } else {
                alert('danger');
            }
        });
    });

</script>

<script type="text/javascript">
    $(document).ready(function (){
        $('#myForm').validate({
            rules: {
                course_name: {
                    required : true,
                }, 
                course_title: {
                    required : true,
                }, 
                
            },
            messages :{
                course_name: {
                    required : 'Please Enter Course Name',
                }, 
                course_title: {
                    required : 'Please Enter Course Titile',
                }, 
                 

            },
            errorElement : 'span', 
            errorPlacement: function (error,element) {
                error.addClass('invalid-feedback');
                element.closest('.form-group').append(error);
            },
            highlight : function(element, errorClass, validClass){
                $(element).addClass('is-invalid');
            },
            unhighlight : function(element, errorClass, validClass){
                $(element).removeClass('is-invalid');
            },
        });
    });
    
</script>
 
<script type="text/javascript">

    $(document).ready(function(){
        $('#image').change(function(e){
            var reader = new FileReader();
            reader.onload = function(e){
                $('#showImage').attr('src',e.target.result);
            }
            reader.readAsDataURL(e.target.files['0']);
        });
    });

</script>


@endsection

CourseController.php

<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Category;
use App\Models\SubCategory;
use App\Models\Course;
use App\Models\Course_goal;
use App\Models\CourseSection;
use App\Models\CourseLecture;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Auth; 
use Carbon\Carbon;

class CourseController extends Controller
{
    public function AllCourse(){
 
      $id = Auth::user()->id;
      $courses = Course::where('instructor_id',$id)->orderBy('id','desc')->get();
        return view('instructor.courses.all_course',compact('courses'));

    }// End Method 

    public function AddCourse(){

        $categories = Category::latest()->get();
        return view('instructor.courses.add_course',compact('categories'));

    }// End Method 


    public function GetSubCategory($category_id){

        $subcat = SubCategory::where('category_id',$category_id)->orderBy('subcategory_name','ASC')->get();
        return json_encode($subcat);

    }// End Method 

    public function StoreCourse(Request $request){

        $request->validate([
            'video' => 'required|mimes:mp4|max:10000',
        ]);

        $image = $request->file('course_image');  
        $name_gen = hexdec(uniqid()).'.'.$image->getClientOriginalExtension();
        Image::make($image)->resize(370,246)->save('upload/course/thambnail/'.$name_gen);
        $save_url = 'upload/course/thambnail/'.$name_gen;

        $video = $request->file('video');  
        $videoName = time().'.'.$video->getClientOriginalExtension();
        $video->move(public_path('upload/course/video/'),$videoName);
        $save_video = 'upload/course/video/'.$videoName;

        $course_id = Course::insertGetId([

            'category_id' => $request->category_id,
            'subcategory_id' => $request->subcategory_id,
            'instructor_id' => Auth::user()->id,
            'course_title' => $request->course_title,
            'course_name' => $request->course_name,
            'course_name_slug' => strtolower(str_replace(' ', '-', $request->course_name)),
            'description' => $request->description,
            'video' => $save_video,

            'label' => $request->label,
            'duration' => $request->duration,
            'resources' => $request->resources,
            'certificate' => $request->certificate,
            'selling_price' => $request->selling_price,
            'discount_price' => $request->discount_price,
            'prerequisites' => $request->prerequisites,

            'bestseller' => $request->bestseller,
            'featured' => $request->featured,
            'highestrated' => $request->highestrated,
            'status' => 1,
            'course_image' => $save_url,
            'created_at' => Carbon::now(),

        ]);

        /// Course Goals Add Form 

        $goles = Count($request->course_goals);
        if ($goles != NULL) {
            for ($i=0; $i < $goles; $i++) { 
                $gcount = new Course_goal();
                $gcount->course_id = $course_id;
                $gcount->goal_name = $request->course_goals[$i];
                $gcount->save();
            }
        }
        /// End Course Goals Add Form 

        $notification = array(
            'message' => 'Course Inserted Successfully',
            'alert-type' => 'success'
        );
        return redirect()->route('all.course')->with($notification);  

    }// End Method 


    



} 

web.php

<?php

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AdminController;
use App\Http\Controllers\InstructorController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\Backend\CategoryController;
use App\Http\Controllers\Backend\CourseController;


///// Instructor Group Middleware
Route::middleware(['auth','roles:instructor'])->group(function(){
  
Route::get('/instructor/dashboard', [InstructorController::class, 'InstructorDashboard'])->name('instructor.dashboard');
Route::get('/instructor/logout', [InstructorController::class, 'InstructorLogout'])->name('instructor.logout');

Route::get('/instructor/profile', [InstructorController::class, 'InstructorProfile'])->name('instructor.profile');
Route::post('/instructor/profile/store', [InstructorController::class, 'InstructorProfileStore'])->name('instructor.profile.store');

Route::get('/instructor/change/password', [InstructorController::class, 'InstructorChangePassword'])->name('instructor.change.password');
Route::post('/instructor/password/update', [InstructorController::class, 'InstructorPasswordUpdate'])->name('instructor.password.update');


// Instructor All Route 
Route::controller(CourseController::class)->group(function(){
    Route::get('/all/course','AllCourse')->name('all.course');
    Route::get('/add/course','AddCourse')->name('add.course');

    Route::get('/subcategory/ajax/{category_id}','GetSubCategory');

    Route::post('/store/course','StoreCourse')->name('store.course');


});

我需要帮助。预先感谢您。

我尝试检查名称是否与数据库中的名称匹配,我还添加了一些错误消息,但同样的事情。

我希望将课程上传到数据库,按提交后,它似乎只是刷新到 /add/course 页面,该页面应该重定向到 /all/course 页面。但没有显示错误信息。

laravel validation debugging file-upload form-submit
1个回答
0
投票

这是由于

php.ini
文件中
upload_max_filesize
post_max_size
的当前值造成的。它们分别限于
2M
8M
。我将它们的值分别增加到
10M
40M
,它起作用了

© www.soinside.com 2019 - 2024. All rights reserved.