Reputation: 857
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?
Upvotes: 1
Views: 2626
Reputation: 50481
$venue = $client->venues()->findOrNew($id);
This can be an existing record (is in the database, has an 'id', might have relationships as well) or a new instance, no attributes, no 'id', doesn't exist in the database (and therefor couldn't possibly have any existing relationships). This is NOT the null
part. findOrNew
does not return a null
.
The null
part is potentially here:
$venue->venueParameter->fill($request->all())->save();
If $venue
is a new instance it will not have any relationships setup. Even if it is an existing record, it may not have that relationship in the database. So trying to set a property on a related model via dynamic property for a relationship isn't going to get very far. This is going to try and resolve the venueParameter
relationship (load it), which doesn't exist so it returns a null
, so:
null->fill($request->all())->save(); // is what is happening
Upvotes: 1
Reputation: 628
This error is self descriptive
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
Upvotes: 0
Reputation: 1246
Becuase you are doing it wrong way here
$venue = false;
according to api doc object must be the instance of laravel Model.
so try changing it to the respective model
$venue = new Venue();
if your model is Venue
Upvotes: 0