mardi 18 juillet 2017

Laravel 5 cannot update relationship

I've been trying for countless hours now, but still having issues updating a models relationship, the closest I've got to is a 'Method fill does not exist.' error.

Listing model:

 class Listing extends Model
    {       
        protected $fillable = [
            'uid', 'start_date',...........
        ];     

        public function locations()
        {
            return $this->hasMany('App\ListingLocation');
        }        
    }

Location (relationship to listing - hasMany):

class ListingLocation extends Model
{

    protected $fillable = [
        'listing_id', 'location',
    ];

    public function listing()
    {
        return $this->belongsTo('App\Listing');
    }
}

This returns my model and relationship, which I can view with dd($listing)

$listing = Listing::with('locations')->findOrFail($id);

This will update my listing model, which I can see the changes after calling dd($listing) again

$listing->fill($array);

However when I attempt to fill the relationship as per below, I get 'Method fill does not exist.'

$listing->locations->fill($array['locations']);

How can I update the relationship successfully before calling $listing->push();?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2u5GGrs
via IFTTT

Aucun commentaire:

Enregistrer un commentaire