راکت
حل شده

ذخیره نشدن اطلاعات در دیتابیس

25 پاسخ

سلام دوستان من میخام سیستم دانلود فایل بعد از پرداخت پیاده سازی کنم

دیتابیس به شکل زیر طراحی کردم ایا غلطه ؟

  public function up()
    {
        Schema::create('files', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

            $table->string('title');
            $table->text('description');
            $table->integer('price');
            $table->string('image');
            $table->string('file');
            $table->string('slug')->unique();
            $table->integer('hit');
            $table->tinyInteger('status');

            $table->integer('inventory')->default(0);
            $table->timestamps();

      });

        Schema::create('category_file' , function(Blueprint $table) {
            $table->unsignedBigInteger('category_id');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
            $table->unsignedBigInteger('file_id');
            $table->foreign('file_id')->references('id')->on('files')->onDelete('cascade');
            $table->primary(['category_id' , 'file_id']);
        });

    }

مدل فایل به شکل زیر


class File extends Model
{
    use Sluggable;

    protected $fillable = [
        'title' , 'slug', 'description' , 'status' ,'price' , 'inventory' , 'image' , 'file'
    ];
    protected $attributes = [
        'hit'=> 1,
    ];
        public function comments()
    {
        return $this->morphMany(Comment::class, 'commentable');
    }

    public function categories()
    {
        return $this->belongsToMany(Category::class);
    }

    public function user() {

        return $this->belongsTo(User::class);
    }

    public function sluggable()
    {
        return [
            'slug' => [
                'source' => ''
            ]
        ];
    }
}

کنترلر به شکل زیر

    public function store(Request $request , File $files)
    {
       $validData = $request->validate([
            'title' => 'required',
            'description' => 'required',
            'image' => 'required',
            'file' => 'required',
            'categories' => 'required',
            'slug' => 'unique:files',
            'status' => ''
        ]);

        if (empty($request->slug)) {
            $slug = SlugService::createSlug(File::class, 'slug', $request->title);
        } else {
            $slug = SlugService::createSlug(File::class, 'slug', $request->slug);
        }
        $request->merge(['slug' => $slug]);

        $files = Storage::disk('public')->putFileAs('files' , $request->file('file') , $request->file('file')->getClientOriginalName())->create($request->all());
        $files->categories()->sync($validData['categories']);

        alert()->success('مطلب مورد نظر با موفقیت ثبت شد' , 'با تشکر');

        return redirect(route('admin.files.index'));
    }

بلید هم به شکل زیر

@component('admin.layouts.content' , ['title' => 'ایجاد فایل'])
    @slot('breadcrumb')
        <li class="breadcrumb-item"><a href="https://roocket.ir/admin">پنل مدیریت</a></li>
        <li class="breadcrumb-item"><a href="https://roocket.ir/{{ route('admin.files.index') }}">لیست فایل</a></li>
        <li class="breadcrumb-item active">ایجاد فایل</li>
    @endslot

    @slot('script')

        <script src="/js/ckeditor/ckeditor.js"></script>
        <script>

            CKEDITOR.replace('description', { filebrowserImageBrowseUrl: '/file-manager/ckeditor' });

            document.addEventListener("DOMContentLoaded", function() {

                document.getElementById('button-image').addEventListener('click', (event) => {
                    event.preventDefault();

                    inputId = 'image_label';
                    window.open('/file-manager/fm-button', 'fm', 'width=1400,height=800');
                });

                document.getElementById('button-file').addEventListener('click', (event) => {
                    event.preventDefault();

                    inputId = 'file_label';
                    window.open('/file-manager/fm-button', 'fm2', 'width=1400,height=800');
                });
            });

            // set file link
            let inputId = '';

            function fmSetLink($url) {
                document.getElementById(inputId).value = $url;
            }

            $('#categories').select2({
                'placeholder' : 'دسترسی مورد نظر را انتخاب کنید'
            })

        </script>
    @endslot

    <div class="row">
        <div class="col-lg-12">
            @include('admin.layouts.errors')
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">فرم ایجاد فایل</h3>
                </div>
                <!-- /.card-header -->
                <!-- form start -->

                <form class="form-horizontal" action="{{ route('admin.files.store') }}" method="POST" enctype="multipart/form-data">
                    @csrf

                    <div class="card-body">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">نام فایل</label>
                            <input type="text" name="title" class="form-control @error('title') is-invalid @enderror" id="inputEmail3" placeholder="نام فایل را وارد کنید" value="{{ old('title') }}">
                            @error('title')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <label for="title" >نام مستعار :</label>
                            <div class="input-group">
                                <input type="text" class="form-control @error('slug') is-invalid @enderror" name="slug" value="{{old('slug')}}">
                            </div>
                            @error('slug')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>

                        <div class="form-group">
                            <label for="description" class="col-sm-2 control-label">توضیحات</label>
                            <textarea class="form-control" name="description" id="description" cols="30" rows="10" style="min-height: 300px">{{ old('description') }}</textarea>
                        </div>

                        <div class="form-group">
                            <label for="title" > وضعیت : </label>
                            <div class="input-group">
                                <select class="form-control" name="status" >
                                    <option value="0">منتشر نشده</option>
                                    <option value="1">منتشر شده</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="inputPassword3" class="col-sm-2 control-label">قیمت</label>
                            <input type="number" name="price" class="form-control @error('price') is-invalid @enderror" id="inputPassword3" placeholder="قیمت را وارد کنید" value="{{ old('price') }}">
                            @error('price')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <label for="inputPassword3" class="col-sm-2 control-label">موجودی</label>
                            <input type="number" name="inventory" class="form-control @error('inventory') is-invalid @enderror" id="inputPassword3" placeholder="موجودی را وارد کنید" value="{{ old('inventory') }}">
                            @error('inventory')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">آپلود تصویر شاخص</label>
                            <div class="input-group">
                                <input type="text" id="image_label" class="form-control" name="image">
                                <div class="input-group-append">
                                    <button class="btn btn-outline-secondary" type="button" id="button-image">انتخاب</button>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">آپلود فایل</label>
                            <div class="input-group">
                                <input type="text" id="file_label" class="form-control" name="file">
                                <div class="input-group-append">
                                    <button class="btn btn-outline-secondary" type="button" id="button-file">انتخاب</button>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">دسته بندی ها</label>
                            <select class="form-control" name="categories[]" id="categories" multiple>
                                @foreach(\Modules\Category\Entities\Category::all() as $category)
                                    <option value="{{ $category->id }}">{{ $category->name }}</option>
                                @endforeach
                            </select>
                        </div>

                    </div>
                    <!-- /.card-body -->
                    <div class="card-footer">
                        <button type="submit" class="btn btn-info">ثبت فایل</button>
                        <a href="https://roocket.ir/{{ route('admin.files.index') }}" class="btn btn-default float-left">لغو</a>
                    </div>
                    <!-- /.card-footer -->
                </form>
            </div>
        </div>
    </div>

@endcomponent

لطفا راهنمایی کنید کدوم قسمت مشکل داره نمیتونم اطلاعات تو دیتابیس ذخیره کنم

paradox
paradox
@wxyz4367
ثبت پرسش جدید

پاسخ‌ها

25
سبحان مولایی
سبحان مولایی
@websaz

تخصص: برنامه‌نویس وب: Python :: Django :: Node :: Php :: Laravel :: JS :: React :: React Native

بهترین پاسخ

به این شکل بنویسید.

                <form class="form-horizontal" action="{{ route('admin.files.update' , 'files'/* اسم پرامتر شما که در روت مشخص کردید */ =>$files->id) }}" method="POST" enctype="multipart/form-data">

@wxyz4367

paradox
paradox
@wxyz4367

برای بخش order هم باید تو دیتابیس فایل چیزی تعریف بشه ؟

محمدسجاد دباغ
محمدسجاد دباغ
@mdabbagh

تخصص: برنامه نویس لاراول

متن ارور چیه؟

شاید پاسخ شما همین‌جا باشد

paradox
paradox
@wxyz4367

@msdabbagh

Call to a member function getClientOriginalName() on null
paradox
paradox
@wxyz4367

@websaz
Ckeditor 4

قبل اینکه فیلد فایل اضافه کنم مشکلی نداشت همه چی به درستی انجام میشد الان که فایل اضافه کردم این ارو دارم نمیدونم کجا اشتباه انجام دادم

سبحان مولایی
سبحان مولایی
@websaz

تخصص: برنامه‌نویس وب: Python :: Django :: Node :: Php :: Laravel :: JS :: React :: React Native

این کد را در کنترلر اجرا کنید سپس خروجی را در اینجا قرار دهید

dd($request->get('file'))

@wxyz4367

سبحان مولایی
سبحان مولایی
@websaz

تخصص: برنامه‌نویس وب: Python :: Django :: Node :: Php :: Laravel :: JS :: React :: React Native

فایل شما از قبل در فایل منیجر وجود دارد وقتی از ان روش استفاده می کنید این url هست که در file دخیره می شود نه یک فایل.
@wxyz4367

moha li
moha li
@mohaligateway

تخصص: توسعه دهنده لاراول و Vue

سلام
مشکل شما اینجا هستش که input type رو به صورت text گذاشتی باید به صورت file قرار بدید و اون button رو هم حذف کنید.
@wxyz4367

<input type="text" id="file_label" class="form-control" name="file">
paradox
paradox
@wxyz4367

@websaz
خروجی فایل با dd نمایش میده
"http://localhost:8000/storage/144-512.png"
ولی اون تیکه کد مربوط به فایل مینویسم ارور دریافت میکنم

paradox
paradox
@wxyz4367

@mohaligateway
از ckeditor استفاده میکنم برای تصویر شاخص بخاطر همین تایپ از نوع text گذاشتم

سبحان مولایی
سبحان مولایی
@websaz

تخصص: برنامه‌نویس وب: Python :: Django :: Node :: Php :: Laravel :: JS :: React :: React Native

سلام دیگر نیازی به store کردن عکس نیست فقط url را ذخیره کنید.

$files = File::create($request->all());
$files->categories()->sync($validData['categories']);

@wxyz4367

paradox
paradox
@wxyz4367

@websaz
ممنون
فقط یه ارور دارم
مشکل تو دیتابیس هست؟

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into `files` (`hit`, `title`, `slug`, `description`, `status`, `price`, `inventory`, `image`, `file`, `updated_at`, `created_at`) values (1, nbnb, nbnb, <p>jhjk</p>, 1, 6565, 8, http://localhost:8000/images/AI-Drug.jpg, http://localhost:8000/storage/files/file.rar, 2021-06-11 01:01:45, 2021-06-11 01:01:45))
محمد امیری
محمد امیری
@mohammadeng3731

تخصص: backend coder

برای ارسال فایل باید از formData داخل جاوااسکریپت باید استفاده کنید وگرنه بی فایدست

محمد امیری
محمد امیری
@mohammadeng3731

تخصص: backend coder

برای خطای دیتابیس هم باید بگم که داخل مایریشن ها مقدار userid رو nullable قرار بده یا مقدار userid رو هنگام create کردن وارد نکردی داره خطا میده

moha li
moha li
@mohaligateway

تخصص: توسعه دهنده لاراول و Vue

در تکمیل مطالب دوستمون @mohammadeng3731 ممکنه داخل model به صورت fillable قرار نداده باشید.
@wxyz4367

paradox
paradox
@wxyz4367

@mohammadeng3731
اگر برابر با null قرار بدم بعد مشکل پیش نمیاد؟ چیزی تو دیتابیس ذخیره نمیشه
من میخام بعد از پرداخت هر کاربر بتونه فایل دانلودی رو ببینه
در این صورت null بودن مشکلی به وجود نمیاره؟

@mohaligateway
در fillable قرار دادم باز هم به این شکل هست

سبحان مولایی
سبحان مولایی
@websaz

تخصص: برنامه‌نویس وب: Python :: Django :: Node :: Php :: Laravel :: JS :: React :: React Native

سلام به fillable اضافه کنید و به این شکل صدا بزنید.

$data = $request->all();
$data['user_id'] = auth()->user()->id;
$files = File::create($data);
$files->categories()->sync($validData['categories']);

@wxyz4367

paradox
paradox
@wxyz4367

@websaz
ممنون از شما
به این شکل نوشتم حل شد

        $files = auth()->user()->files()->create($request->all());

اطلاعات به درستی ذخیره شد مشکل بعدی برای اپدیت هست
زمانی که روی دکمه ویرایش میزنم ارور زیر دارم

Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameters for [Route: admin.files.update] [URI: admin/files/{file}]. (View: C:\xampp\htdocs\laravelpro\Modules\File\Resources\views\admin\files\edit.blade.php)
http://localhost:8000/admin/files/4/edit

بلید ادیت

                <form class="form-horizontal" action="{{ route('admin.files.update' , $files->id) }}" method="POST" enctype="multipart/form-data">
paradox
paradox
@wxyz4367

@websaz
ممنون ازت به این شکل نوشتم درست شد

                <form class="form-horizontal" action="{{ route('admin.files.update' ,[ 'files', 'Admin\FileController' => $files->id]) }}" method="POST" enctype="multipart/form-data">

فقط وقتی صفحه ویرایش میاد اطلاعات قبلی نمایش نمیده

سبحان مولایی
سبحان مولایی
@websaz

تخصص: برنامه‌نویس وب: Python :: Django :: Node :: Php :: Laravel :: JS :: React :: React Native

لطفا فرم و کنترلر را قرار دهید.
@wxyz4367

paradox
paradox
@wxyz4367

@websaz

controller

    public function update(Request $request, File $files)
    {
        $validData = $request->validate([
            'title' => 'required',
            'description' => 'required',
            'price' => 'required',
            'inventory' => 'required',
            'image' => 'required',
            'file' => 'required',
            'categories' => 'required',
            'slug' => 'unique:file',
            'status' => ''
        ]);

        $files->update($validData);
        $files->categories()->sync($validData['categories']);

        alert()->success('فایل مورد نظر با موفقیت ویرایش شد' , 'با تشکر');
        return redirect(route('admin.files.index'));
    }

blade edit

@component('admin.layouts.content' , ['title' => 'ویرایش فایل'])
    @slot('breadcrumb')
        <li class="breadcrumb-item"><a href="https://roocket.ir/admin">پنل مدیریت</a></li>
        <li class="breadcrumb-item"><a href="https://roocket.ir/{{ route('admin.users.index') }}">لیست فایل</a></li>
        <li class="breadcrumb-item active">ویرایش فایل</li>
    @endslot

    @slot('script')

        <script src="/js/ckeditor/ckeditor.js"></script>

        <script>
           CKEDITOR.replace('description', { filebrowserImageBrowseUrl: '/file-manager/ckeditor' });

            document.addEventListener("DOMContentLoaded", function() {

                document.getElementById('button-image').addEventListener('click', (event) => {
                    event.preventDefault();

                    inputId = 'image_label';
                    window.open('/file-manager/fm-button', 'fm', 'width=1400,height=800');
                });

                document.getElementById('button-file').addEventListener('click', (event) => {
                    event.preventDefault();

                    inputId = 'file_label';
                    window.open('/file-manager/fm-button', 'fm2', 'width=1400,height=800');
                });
            });

            // set file link
            let inputId = '';

            function fmSetLink($url) {
                document.getElementById(inputId).value = $url;
            }

            $('#categories').select2({
                'placeholder' : 'دسترسی مورد نظر را انتخاب کنید'
            })

        </script>
    @endslot

    <div class="row">
        <div class="col-lg-12">
            @include('admin.layouts.errors')
            <div class="card">
                <div class="card-header">
                    <h3 class="card-title">فرم ویرایش فایل</h3>
                </div>
                <!-- /.card-header -->
                <!-- form start -->

                <form class="form-horizontal" action="{{ route('admin.files.update' , ['files', 'FileController' => $files->id ]) }}" method="POST" enctype="multipart/form-data">

                    @csrf
                    @method('patch')

                    <div class="card-body">
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">نام فایل</label>
                            <input type="text" name="title" class="form-control @error('inventory') is-invalid @enderror" id="inputEmail3" placeholder="نام فایل را وارد کنید" value="{{ old('title' , $files->title) }}">
                            @error('title')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <label for="slug" >نام مستعار :</label>
                            <div class="input-group">
                                <input type="text" class="form-control @error('slug') is-invalid @enderror" name="slug" value="{{$files->slug}}" placeholder="نام فایل را به انگلیسی وارد کنید">
                            </div>
                            @error('slug')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">توضیحات</label>
                            <textarea class="form-control" name="description" id="description" cols="30" rows="10">{{ old('description',$files->description) }}</textarea>
                        </div>

                        <div class="form-group">
                            <label for="title" > وضعیت : </label>
                            <div class="input-group">
                                <select class="form-control" name="status" >
                                    <option value="1"
                                    @if ($files->status == 1)
                                        {{'selected'}}
                                        @endif
                                    >فعال
                                    </option>
                                    <option value="0"
                                    @if ($files->status == 0)
                                        {{'selected'}}
                                        @endif
                                    >غیرفعال
                                    </option>
                                </select>
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="inputPassword3" class="col-sm-2 control-label">قیمت</label>
                            <input type="number" name="price" class="form-control @error('inventory') is-invalid @enderror" id="inputPassword3" placeholder="قیمت را وارد کنید" value="{{ old('price',$files->price) }}">
                            @error('price')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <label for="inputPassword3" class="col-sm-2 control-label">موجودی</label>
                            <input type="number" name="inventory" class="form-control @error('inventory') is-invalid @enderror" id="inputPassword3" placeholder="موجودی را وارد کنید" value="{{ old('inventory',$files->inventory) }}">
                            @error('inventory')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                            @enderror
                        </div>
                        <div class="form-group">
                            <hr>
                            <label class="col-sm-2 control-label">آپلود تصویر شاخص</label>
                            <div class="input-group mb-2">
                                <input type="text" id="image_label" class="form-control" name="image" dir="ltr" value="{{ $files->image }}">
                                <div class="input-group-append">
                                    <button class="btn btn-outline-secondary" type="button" id="button-image">انتخاب</button>
                                </div>
                            </div>
                            <img class="w-25" src="{{ $files->image }}" alt="">

                        </div>

                        <div class="form-group">
                            <hr>
                            <label class="col-sm-2 control-label">آپلود فایل </label>
                            <div class="input-group mb-2">
                                <input type="text" id="file_label" class="form-control" name="file" dir="ltr" value="{{ $files->file }}">
                                <div class="input-group-append">
                                    <button class="btn btn-outline-secondary" type="button" id="button-file">انتخاب</button>
                                </div>
                            </div>
                            <img class="w-25" src="{{ $files->image }}" alt="">

                        </div>

                        <div class="form-group">
                            <label for="inputEmail3" class="col-sm-2 control-label">دسته بندی ها</label>
                            <select class="form-control" name="categories[]" id="categories" multiple>
                                @foreach(\Modules\Category\Entities\Category::all() as $category)
                                    <option value="{{ $category->id }}" {{ in_array($category->id , $files->categories->pluck('id')->toArray()) ? 'selected' : '' }}>{{ $category->name }}</option>
                                @endforeach
                            </select>
                        </div>

                    </div>
                    <!-- /.card-body -->
                    <div class="card-footer">
                        <button type="submit" class="btn btn-info">ویرایش فایل</button>
                        <a href="https://roocket.ir/{{ route('admin.files.index') }}" class="btn btn-default float-left">لغو</a>
                    </div>
                    <!-- /.card-footer -->
                </form>
            </div>
        </div>
    </div>

@endcomponent

دکمه حذف و ststus هم عمل نمیکنه
تو بخش دیگه کار میکنه ولی برای فایل کار نمیکنه

 public function destroy(File $files)
    {
        $files->delete();

        alert()->success('فایل مورد نظر با موفقیت حذف شد' , 'با تشکر');
        return back();
    }

    public function updatestatus(File $files)
    {
        if ($files->status == 1) {
            $files->status = 0;
        } else {
            $files->status = 1;
        }
        $files->save();

        alert()->success('به روز رسانی با موفقیت انجام شد' , 'با تشکر');

        return redirect(route('admin.files.index'));
    }
صفحه 1 از 2

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام