从空值 Laravel 8 创建默认对象

Posted

技术标签:

【中文标题】从空值 Laravel 8 创建默认对象【英文标题】:Creating default object from empty value Laravel 8 【发布时间】:2021-05-22 03:09:19 【问题描述】:

我正在学习 YouTube Laravel 8 电子商务教程,但在尝试从管理面板更新商店产品时遇到了错误 Creating default object from empty value。我已经尝试了在这里和谷歌上可以找到的所有答案(检查任何拼写错误的单词,需要组件和 web.php 文件中的每个必要组件等)。我完全按照教程中的内容做了,到目前为止一切正常。

这是我的看法:

<div class="panel-body">
  @if(Session::has('message'))
    <div class="alert alert-success" role="alert"> Session::get('message') </div>
  @endif
    <form action="" class="form-horizontal" enctype="multipart/form-data" 
    wire:submit.prevent="updateProduct">
      <div class="form-group">
        <label for="" 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 for="" 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 for="" class="col-md-4 control-label">Short Description</label>
        <div class="col-md-4">
          <textarea class="form-control input-md" placeholder="Short Description" 
           wire:model="short_description"></textarea>
        </div>
      </div>
      <div class="form-group">
        <label for="" class="col-md-4 control-label">Description</label>
        <div class="col-md-4">
          <textarea class="form-control input-md" placeholder="Description" wire:model="description"> 
          </textarea>
        </div>
      </div>
      <div class="form-group">
        <label for="" 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 for="" 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 for="" 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 for="" class="col-md-4 control-label">Stock</label>
      <div class="col-md-4">
        <select name="" id="" class="form-control" wire:model="stock_status">
          <option value="In Stock">In Stock</option>
          <option value="Out of Stock">Out of Stock</option>
        </select>
      </div>
    </div>
    <div class="form-group">
      <label for="" class="col-md-4 control-label">Featured</label>
      <div class="col-md-4">
        <select name="" id="" class="form-control" wire:model="featured">
          <option value="0">No</option>
          <option value="1">Yes</option>
        </select>
      </div>
    </div>
    <div class="form-group">
      <label for="" 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 for="" 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() "  />
        @else
          <img src=" asset('assets/images/products') /$image"  />
        @endif
      </div>
    </div>
    <div class="form-group">
      <label for="" class="col-md-4 control-label">Category</label>
      <div class="col-md-4">
        <select name="" id="" 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 for="" 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>

这是我的 Livewire 组件文件:

<?php

namespace App\Http\Livewire\Admin;

use App\Models\Product;
use App\Models\Category;
use Livewire\Component;
use Illuminate\Support\Str;
use Carbon\Carbon;
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');
    


这是我的 web.php 文件

<?php

use App\Http\Livewire\Admin\AdminAddCategoryComponent;
use App\Http\Livewire\Admin\AdminAddProductComponent;
use App\Http\Livewire\Admin\AdminCategoryComponent;
use App\Http\Livewire\Admin\AdminDashboardComponent;
use App\Http\Livewire\Admin\AdminEditCategoryComponent;
use App\Http\Livewire\Admin\AdminProductComponent;
use App\Http\Livewire\Admin\AdminEditProductComponent;
use App\Http\Livewire\CartComponent;
use App\Http\Livewire\CategoryComponent;
use App\Http\Livewire\CheckoutComponent;
use App\Http\Livewire\DetailsComponent;
use App\Http\Livewire\HomeComponent;
use App\Http\Livewire\SearchComponent;
use App\Http\Livewire\ShopComponent;
use App\Http\Livewire\User\UserDashboardComponent;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

//Route::get('/', function () 
//    return view('welcome');
//);

Route::get('/', HomeComponent::class);

Route::get('/shop', ShopComponent::class);

Route::get('/cart', CartComponent::class)->name('product.cart');

Route::get('/checkout', CheckoutComponent::class);

Route::get('/product/slug', DetailsComponent::class)->name('product.details');

Route::get('/product-category/category_slug', CategoryComponent::class)->name('product.category');

Route::get('/search', SearchComponent::class)->name('product.search');

Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () 
    return view('dashboard');
)->name('dashboard');

//For User or Customer
Route::middleware(['auth:sanctum', 'verified'])->group(function() 
    Route::get('/user/dashboard', UserDashboardComponent::class)->name('user.dashboard');
);

//For Admin
Route::middleware(['auth:sanctum', 'verified', 'authadmin'])->group(function() 
    Route::get('/admin/dashboard', AdminDashboardComponent::class)->name('admin.dashboard');
    Route::get('/admin/categories', AdminCategoryComponent::class)->name('admin.categories');
    Route::get('/admin/category/add', AdminAddCategoryComponent::class)->name('admin.addcategory');
    Route::get('/admin/category/edit/category_slug', AdminEditCategoryComponent::class)->name('admin.editcategory');
    Route::get('/admin/products', AdminProductComponent::class)->name('admin.products');
    Route::get('/admin/product/add', AdminAddProductComponent::class)->name('admin.addproduct');
    Route::get('/admin/product/edit/product_slug', AdminEditProductComponent::class)->name('admin.editproduct');
);

【问题讨论】:

【参考方案1】:

你忘了添加这个

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

挂载函数, 因此,当您尝试在 updateProduct 函数中从数据库接收数据时,它会收到一个空白值

【讨论】:

【参考方案2】:

在 Here is my Livewire Component file:.

中使用 $this 代替 $product

【讨论】:

正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于从空值 Laravel 8 创建默认对象的主要内容,如果未能解决你的问题,请参考以下文章

在 laravel 中具有雄辩关系的更新期间从空值创建默认对象

PHP 5.4:禁用警告“从空值创建默认对象”

如何修复 PHP Strict 错误“从空值创建默认对象”?

PHP警告。从空值中创建默认对象

从 PHP 中的空值创建默认对象?

从子组件更新数据 - Vue.js/Axios/Laravel