اذهب إلى المحتوى
  • 0

خطأ Creating default object from empty value في إضافة البيانات في لارافل ؟

محمود سامي حسين

السؤال

خطأ لارافيل ؟

Creating default object from empty value

<?php
//live wire code
namespace App\Http\Livewire\Admin;
use App\Models\Category;
use App\Models\Product;
use Carbon\Carbon;
use Illuminate\Contracts\Session\Session;
use Livewire\Component;
use Illuminate\Support\Str;
use Livewire\WithFileUploads;

class AdminEditProductComponent extends Component
{
    use WithFileUploads;

    public $name;
    public $slug;
    public $short_description;
    public $description;
    public $regular_price;
    public $sale_price;
    public $SKU;
    public $stock_status;
    public $featured;
    public $quantity;
    public $image;
    public $category_id;
    public $newimage;
    public $product_id;

    public function mount($product_slug){

        $product = Product::where('slug',$product_slug)->first();

         $this->name = $product->name;
         $this->slug = $product->slug ;
         $this->short_description= $product->short_description;
         $this->description = $product->description;
         $this->regular_price = $product->regular_price;
         $this->sale_price = $product->sale_price;
         $this->SKU = $product->SKU;
         $this->stock_status = $product->stock_status;
         $this->featured = $product->featured;
         $this->quantity = $product->quantity;
         $this->image = $product->image;
         $this->category_id = $product->category_id;
         $this->newimage = $product->newimage ;
         $this->product_id = $product->product_id;

    }
    public function generateslug(){

        $this->slug = Str::slug($this->name,'-');
    }

    public function updateProduct(){

        $product = Product::find($this->product_id);
        $product->name = $this->name;
        $product->slug = $this->slug;
        $product->short_description = $this->short_description;
        $product->description = $this->description;
        $product->regular_price = $this->regular_price;
        $product->sale_price = $this->sale_price;
        $product->SKU = $this->SKU;
        $product->stock_status = $this->stock_status;
        $product->featured = $this->featured;
        $product->quantity = $this->quantity;
        if($this->newimage){
            $imageName = Carbon::now()->timestamp.'-' .$this->newimage->extension();
            $this->newimage->storeAs('products',$imageName);
            $product->image = $imageName;
        }

        $product->category_id = $this->category_id;

        $product->save();

        Session()->flash('message','Product Has Been Updated Successfully!');
    }
    public function render()
    {
        $categories = Category::all();

        return view('livewire.admin.admin-edit-product-component',[ 'categories' => $categories ])->layout('layouts.base');
    }
}

<!--ملف العرض view -->
<div>
    <div class="container" style="padding: 30px 0;">
        <div class="row">
            <div class="col-md-12">
                <div class="panel panel default">
                    <div class="panel-heading">
                        <div class="row">
                            <div class="col-md-6">
                                Edit Product
                            </div>
                            <div class="col-md-6">
                                <a href="{{route('admin.products')}}" class="btn btn-success pull-right">All Products</a>
                            </div>
                            <div class="panel-body">
                                @if(Session::has('message'))
                                    <div class="alert alert-success" role="alert">{{ Session::get('message')}}</div>
                                @endif
                                <form class="form-horizontal" enctype="multipart/form-data" wire:submit.prevent='updateProduct'>
                                    @csrf
                                    <div class="form-group">
                                        <label class="col-md-4 control-label">Product Name</label>
                                        <div class="col-md-4">
                                            <input type="text" placeholder="Product Name"  class="form-control input-md" wire:model='name' wire:keyup='generateslug'>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-4 control-label">Product Slug</label>
                                        <div class="col-md-4">
                                            <input type="text" placeholder="Product Slug"  class="form-control input-md" wire:model='slug'>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-4 control-label">Short Description</label>
                                        <div class="col-md-4">
                                            <textarea type="text" placeholder="Short Description"  class="form-control" wire:model='short_description'></textarea>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-4 control-label">Description</label>
                                        <div class="col-md-4">
                                            <textarea type="text" placeholder="Description"  class="form-control" wire:model='description'></textarea>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-4 control-label">Regular Price</label>
                                        <div class="col-md-4">
                                            <input type="text" placeholder="Regular Price" class="form-control input-md" wire:model='regular_price'>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-4 control-label">Sale Price</label>
                                        <div class="col-md-4">
                                            <input type="text" placeholder="Sale Price" class="form-control input-md" wire:model='sale_price'>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-4 control-label">SKU</label>
                                        <div class="col-md-4">
                                            <input type="text" placeholder="SKU" class="form-control input-md" wire:model='SKU'>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-4 control-label">Stock</label>
                                        <div class="col-md-4">
                                            <select class="form-control" wire:model='stock_status'>
                                                <option value="instock">In Stock </option>
                                                <option value="outofstock">Out Of Stock </option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-4 control-label">Featured</label>
                                        <div class="col-md-4">
                                            <select class="form-control" wire:model='featured'>
                                                <option value="0">No</option>
                                                <option value="1">Yes</option>
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-4 control-label">Quantity</label>
                                        <div class="col-md-4">
                                            <input type="text" placeholder="Quantity" class="form-control input-md" wire:model='quantity'>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-4 control-label">Product Image</label>
                                        <div class="col-md-4">
                                            <input type="file" class="input-file" wire:model='newimage'>
                                            @if($newimage)
                                                <img src="{{$newimage->temporaryUrl()}}" width="120">
                                            @else
                                                <img src="{{ asset('assets/images/products')}}/{{$image}}" width="120" >
                                            @endif
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-4 control-label">Category</label>
                                        <div class="col-md-4">
                                            <select class="form-control" wire:model='category_id'>
                                                <option value="">Select Category</option>
                                                @foreach ($categories as $category)
                                                    <option value="{{$category->id}}">{{$category->name}}</option>
                                                @endforeach
                                                
                                            </select>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="col-md-4 control-label"></label>
                                        <div class="col-md-4">
                                            <button type="submit" class="btn btn-primary">Update</button>
                                        </div>
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    
    </div>
</div>







 

errors.PNG

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 1

هذا الخطأ يشير إلى أنه تقوم بإضافة أو إنشاء كائن من قيمة فارغة لذلك يجب التأكد من أن المتغير التالي 

$this->product_id

يحتوي على قيمة أو لا ، يبدو أنه لا يحتوي على قيمة لذلك لارافل ترجع هذا الخطأ ,  فلاحظ في الكود التالي

$product = Product::find($this->product_id);

أنه لا يوجد منتج لتقوم بالتعديل عليه ، يمكنك التأكد من ذلك و من ثم أخباري بالنتيجة لو سمحت ؟ 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0
بتاريخ الآن قال محمود سامي حسين:

نعم الخطأ كان في السطر التالي : 


$this->product_id = $product->product_id;

وتم تعديله الي التالي


 $this->product_id = $product->id;:

 

هل تم حل المشكلة ؟ 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...