Laravel更新查询不起作用的空调用一个成员函数填充()

问题描述 投票:1回答:3

当我在我的下拉值更改用户ID更新数据,并分配一个特定的场所,用户就返回错误,无法更新数据。

错误:调用一个成员函数填充()上的空

图片添加:其中客户端是一个用户,当我分配星级场所维奈返回错误

enter image description here

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

  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);
}

这里是我的venueparameter模型

<?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);
}
 }

以下是我GT当我的DD($请求 - >所有())

                        "_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

以下是我GT当我的DD($场地)

  #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"

]

我该如何克服这个问题?

php laravel
3个回答
1
投票
$venue = $client->venues()->findOrNew($id); 

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

null部分是可能在这里:

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

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

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

0
投票

因为你正在做在这里走错了路

$venue = false;

根据API doc对象必须是laravel模型的实例。

所以请尝试更改到相应的模式

$venue = new Venue();

如果你的模型是Venue


0
投票

这个错误是自描述性

错误:调用一个成员函数填充()上的空位置$ vanue得到空值尝试打印$ vanue数据,并检查它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
© www.soinside.com 2019 - 2024. All rights reserved.