【问题标题】:Attempt to assign property "parent_id" on null尝试在 null 上分配属性“parent_id”
【发布时间】:2025-12-31 05:45:12
【问题描述】:

我想用这些代码将数据保存到 mySql 表中

category.blade.php

 <form name="categoryForm" id="categoryForm" action="{{url('admin/add-edit-category')}}" method="post" enctype="multipart/form-data">
            @csrf

            <div class="form-group">
                <label>Select Category Level</label>
                <select name="parent_id" id="parent_id" class="form-control select2" style="width: 100%;">
                    <option value="0">Main Category</option>
                </select>
            </div>

           <div class="card-footer">
                    <button type="submit" class="btn btn-primary">Submit</button>
            </div>
    </form>

CategoryController.php

<?php

namespace App\Http\Controllers\admin;

use App\Category;
use App\Section;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Session;

class CategoryController extends Controller
{
public function addEditCategory(Request $request, $id = null)
{

    if ($id = "") {
        $title = "Add Category";
        // Add category functionality

        //if id is not get it will create a new 
        $category = new Category();
    } else {
        $title = "Edit Category";
        // Edit category functionality
    }

    //checking form data is comming or not?
    if ($request->isMethod('post')) {
        $data = $request->all();
        // echo "<pre>";
        // print_r($data);
        // die;


        $category->parent_id = $data['parent_id'];
       
        $category->save();
    }

    // Get All the Sections
    $getSections = Section::get();
    return view('admin.categories.add-edit-category')->with(compact('title', 'getSections'));
}
}

但是当我在 addEditCategory 视图中按下提交按钮时,给我 尝试在 null 上分配属性“parent_id” 我该如何解决?

【问题讨论】:

  • 您需要在Category 模型的可填充属性上添加parent_id
  • 你确定你的意思是=而不是==
  • 是的,我已经在类别播种器文件中通过将所有数据访问到类别模型中来做到这一点。即使我运行 if ($request->isMethod('post')) { $data = $request->all();回声“
    ”;打印_r($数据);死; } 它将打印所有数据,但当推送到数据库时显示错误
  • @MukhlisRaza tadman 表示您的if 声明:if ($id = ""),对吗?这是你控制器的所有代码吗,哪一行抛出了那个错误?
  • @tadman 是的,你说得对,谢谢问题已解决我做错了。不幸的是,我放了一个 = 而不是 double 。

标签: php mysql laravel laravel-blade


【解决方案1】:

我在 CategoryController.php 中犯了一个错误。不幸的是,我放了一个'='而不是双'=='。检查 id 是否等于空。 现已更新

CategoryController.php

class CategoryController extends Controller
{
public function addEditCategory(Request $request, $id = null)
{
   if ($id == "") {
       $title = "Add Category";
       // Add category functionality     
   } else {
      $title = "Edit Category";
      // Edit category functionality
   }
 
    if ($request->isMethod('post')) {
      
        $parent_id = $request->get("parent_id");

           $category = Category::updateOrCreate([
               "parent_id" => $parent_id 
           ], [
               "parent_id" => $parent_id 
           ]);
    }

    // Get All the Sections
    $getSections = Section::get();
    return view('admin.categories.add-edit-category')->with(compact('title', 'getSections'));
}
}

【讨论】:

    【解决方案2】:

    如果你使用的是 laravel => 5.3,请试试这个方法

    <?php
    
    namespace App\Http\Controllers\admin;
    
    use App\Category;
    use App\Section;
    use App\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use Session;
    
    class CategoryController extends Controller
    {
    public function addEditCategory(Request $request, $id = null)
    {
    
     
       if ($id = "") {
           $title = "Add Category";
           // Add category functionality
    
        
       } else {
          $title = "Edit Category";
          // Edit category functionality
       }
     
        if ($request->isMethod('post')) {
          
            $parent_id = $request->get("parent_id");
    
               $category = Category::updateOrCreate([
                   "parent_id" => $parent_id 
               ], [
                   "parent_id" => $parent_id 
               ]);
        }
    
        // Get All the Sections
        $getSections = Section::get();
        return view('admin.categories.add-edit-category')->with(compact('title', 'getSections'));
    }
    }
    

    【讨论】: