【发布时间】: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) 数据。