【问题标题】:Laravel update Query not work Call to a member function fill() on nullLaravel更新查询不起作用在null上调用成员函数fill()
【发布时间】:2018-05-18 18:25:58
【问题描述】:

当我通过更改下拉值中的用户 ID 更新数据并将特定场所分配给用户时,它返回错误并且无法更新数据。

错误:在 null 上调用成员函数 fill()

图片添加:客户是用户,当我将明星场地分配给 vinay 时,它返回错误

这是我尝试的模型功能代码

  public static function saveOrUpdate(Request $request) {
    try {

        $checkBoxes = [
            'is_premium',
            'is_verified',
            'is_outside_catering',
            'is_indoor',
            'is_outdoor',
            'has_parking',
            'has_valet'
        ];
        foreach($checkBoxes as $checkbox) {
            if(!$request->has($checkbox)) {
                $request->merge([$checkbox => 0]);
            }
        }

        $venue = false;

        DB::transaction(function () use ($request, &$venue) {
            $id = $request->get('id', false); // gt id here
            $clientId = $request->get('client_id', false); // gt cleint id here
            $client = Client::findOrFail($clientId);
            $venue = $client->venues()->findOrNew($id); // gt venue data
            // added dd($venue) below
            //dd($request->all()); Added array in below

            $venue->fill($request->all());

            try {
                $venue->save();
                $occasions = $request->get('occasions', []);
                $venue->occasions()->sync($occasions);

                if($id) {
                    // Here I am gtng error
                    $venue->venueParameter->fill($request->all())->save();
                } else {
                    $venue->venueParameter()->create($request->all());
                }
            } catch (\Exception $ex) {
                echo $ex->getMessage();
                dd($ex->getTraceAsString());
            }
        });

        return $venue;
    } catch (\Exception $ex) {
        throw $ex;
    }
}

这是参数的场地模型函数

    public function venueParameter() {
    //dd("Welcome"); gt here successfully
    return $this->hasOne(VenueParameter::class);
}

这是我的场地参数模型

<?php

     namespace App;

     use Illuminate\Database\Eloquent\Model;

  class VenueParameter extends Model
  {

    protected $fillable = [

    'venue_id',
    'min_capacity',
    'max_capacity',
    'is_outside_catering',
    'price_list_view',
    'price_details_view',
    'area',
    'is_indoor',
    'indoor_area',
    'is_outdoor',
    'outdoor_area',
    'has_parking',
    'no_of_parkings',
    'has_valet',
    'no_of_rooms',
    'decorator',
];


public $timestamps = false;

const DECORATOR = [
    'Inside' => 'Inside',
    'Outside' => 'Outside',
    'Both' => 'Both'
];

public function venue() {

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

当我 dd($request->all())

                        "_token" => "dJcVc2pP6fGtBD9FUZYFMnKfHf0ArScjy9mLJfcg"
                        "id" => "8"
                        "name" => "test"
                        "client_id" => "2"
                        "logo" => "public/venue-logo/BO7ZuxbZyUZjo7u35WAyx4ReNlBnGxcFIKo77euo.jpeg"
                        "venue_type_id" => "1"
                        "is_premium" => "1"
                        "is_verified" => "1"
                        "tripadvisor_url" => "http://test.com/home"
                        "venue_url" => "http://test.com/home"
                        "status" => "Active"
                        "min_capacity" => "1"
                        "max_capacity" => "1"
                        "price_list_view" => "1"
                        "price_details_view" => ""
                        "area" => "5000"
                        "indoor_area" => "0"
                        "outdoor_area" => "0"
                        "no_of_parkings" => "0"
                        "decorator" => "Inside"
                        "is_outside_catering" => 0
                        "is_indoor" => 0
                        "is_outdoor" => 0
                        "has_parking" => 0
                        "has_valet" => 0

这是我 dd($venue) 时的结果

  #attributes: array:12 [▼
"id" => 11
"client_id" => 1
"name" => "zuber"
"venue_url" => "http://premiumbanquets.com/home"
"logo" => "public/venue-logo/Rkt8SV5OLz8pMW6sFfJJUhUFmhSI2VCfBLvI6STd.jpeg"
"venue_type_id" => 1
"is_premium" => 1
"is_verified" => 1
"tripadvisor_url" => "http://premiumbanquets.com/home"
"status" => "Active"
"created_at" => "2017-12-04 13:19:25"
"updated_at" => "2017-12-04 13:19:25"

]

我该如何克服这个问题?

【问题讨论】:

  • 我的猜测是场地没有相关的场地参数
  • 转储场地并检查关系
  • 我认为findOrNew($id);没有找到任何与$id相关的记录,先看$venue的结果
  • 还 findOrNew($id) 数据。

标签: php laravel


【解决方案1】:
$venue = $client->venues()->findOrNew($id); 

这可以是现有记录(在数据库中,有一个“id”,也可能有关系)或一个新实例,没有属性,没有“id”,在数据库中不存在(因此不可能有任何现有的关系)。这 不是 null 部分。 findOrNew 不返回 null

null 部分可能在这里:

$venue->venueParameter->fill($request->all())->save();

如果$venue 是一个新实例,它将不会有任何关系设置。即使它是现有记录,它也可能在数据库中没有这种关系。因此,尝试通过关系的动态属性在相关模型上设置属性不会走得太远。这将尝试解决venueParameter 关系(加载它),该关系不存在,因此它返回null,所以:

null->fill($request->all())->save(); // is what is happening

【讨论】:

  • 描述得很好。明白你想说的话。我的下一步是什么?因为我很困惑。
  • 你必须检查$venue-&gt;venueParameter 是否为空(这种关系是否存在),如果不存在,创建它,如果存在,更新它.. 基本上你正在尝试做,你只需要对这种关系做额外的检查
  • 在 dd($venue->venueParameter); 时为空;
  • 是的,这正是预期的,因为这就是整篇文章的全部内容
  • 如果它的null 关系不存在..你必须创建它......它是你已经尝试做的,你只是做错了
【解决方案2】:

因为你在这里做错了

$venue = false;

根据apidoc对象必须是laravel Model的实例。

所以尝试将其更改为相应的模型

$venue = new Venue();

如果你的模型是Venue

【讨论】:

    【解决方案3】:

    这个错误是自我描述的

    Error :Call to a member function fill() on null here $vanue get null value try to print $vanue data and check it dd($vanue);

        DB::transaction(function () use ($request, &$venue) {
        $id = $request->get('id', false); // gt id here
        $clientId = $request->get('client_id', false); // gt cleint id here
        $client = Client::findOrFail($clientId);
        $venue = $client->venues()->findOrNew($id); // gt venue data
        **//dd($request->all()); Added array in below**
    
        $venue->fill($request->all()); **//here value of  $venue  is null or blank** 
    
        dd($vanue) // check this whether it contain NULL or not
    

    【讨论】:

    • 在描述中添加了场地数组
    • 现在尝试从场地数据创建 $vanue 对象 #attributes: array:12 "id" => 11 ... $vanue = Vanue::findOrFail(11//来自 vanue 数组的 id 值);
    猜你喜欢
    • 2023-04-02
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 2022-10-16
    • 1970-01-01
    • 2016-09-04
    • 2016-06-29
    相关资源
    最近更新 更多