When i update data with changing user id in my drop down value and allocate a specific venue to user it return error and cannot update data.
Error :Call to a member function fill() on null
Image Added: where client is a users and when i allocate star venue to vinay it returns error
Here is my model function code which i try
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;
}
}
Here is venue model function for parameter
public function venueParameter() {
//dd("Welcome"); gt here successfully
return $this->hasOne(VenueParameter::class);
}
Here is my venueparameter model
<?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);
}
}
Here is what i gt when i 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
Here is what i gt when i 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"
]
How can i overcome on this problem?
findOrNew($id);
is not find any record related to$id
, First see the result of$venue
– Hiren Gohel