Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.08k stars 886 forks source link

create crud won't save relationship id when submitted #5047

Closed glenelkinsdev closed 1 year ago

glenelkinsdev commented 1 year ago

the screen in question allows to add a business to the database and has a dropdown of options to select the business type, which is populated from the BusinessType model.

I have included type_id in the fillable for the business, but the query never selects the field to insert so i'm getting "SQLSTATE[HY000]: General error: 1364 Field 'type_id' doesn't have a default value"

It generates this query:

insert into businesses (name, description, website_url, telephone, email, address1, address2, town, post_code, lat, lng, fb_url, tw_url, li_url) values (dsfsdf, dsfdsf, ?, ?, ?, ?, ?, dsfdsf, ?, ?, ?, ?, ?, ?)

As you can see it's missing type_id completely but all the other fields in fillable work:

protected $fillable = [
        'name',
        'description',
        'profile_photo_path',
        'website_url',
        'telephone',
        'email',
        'address1',
        'address2',
        'town',
        'post_code',
        'lat',
        'lng',
        'fb_url',
        'tw_url',
        'li_url',
        'type_id'
    ];

The type_id is present in the post request! Here is the setup method:

protected function setupCreateOperation()
    {
        CRUD::setValidation(BusinessRequest::class);

        CRUD::addField([
            'label'=>'Business type',
            'type'=>'select',
            'name'=>'type_id',
            'attribute'=>'name',
            'model'=>BusinessType::class,
            'options'=>(function(Builder $builder) {
                return $builder->orderBy('name','ASC')->get();
            })
        ]);

        CRUD::field('name')->type('text')->label('Business name');
        CRUD::field('description')->type('textarea')->label('Business description');
        CRUD::field('website_url')->type('url')->label('Website url');
        CRUD::field('telephone')->type('text')->label('Tel #');
        CRUD::field('email')->type('email')->label('Email address');

        CRUD::field('address1')->type('text')->label('Address line 1');
        CRUD::field('address2')->type('text')->label('Address line 2');
        CRUD::field('town')->type('text')->label('Town');
        CRUD::field('post_code')->type('text')->label('Post code');

        CRUD::field('lat')->type('text')->label('Lat');
        CRUD::field('lng')->type('text')->label('Lng');

        CRUD::field('fb_url')->type('url')->label('Facebook url');
        CRUD::field('tw_url')->type('url')->label('Twitter url');
        CRUD::field('li_url')->type('url')->label('LinkedIn url');

        CRUD::addField([
            'name'=>'profile_photo_path',
            'label'=>'Profile photo',
            'type'=>'upload',
            'upload'=>true,
            'disk'=>'businesses'
        ]);

        CRUD::addField([
            'name'=>'background_photo_path',
            'label'=>'Background photo',
            'type'=>'upload',
            'upload'=>true,
            'disk'=>'businesses'
        ]);

        Business::created(function(Business $business) {

            // get request
            /** @var Request $request */
            $request = $this->crud->getRequest();

            // make image destination
            $destinationPath = storage_path('businesses/'.$business->id);
            if(!is_dir($destinationPath))
                mkdir($destinationPath,0755,true);

            // get the photo file if it exists
            $photoFile = $request->file('profile_photo_path');
            $backgroundFile = $request->file('background_photo_path');

            if($backgroundFile instanceof UploadedFile){

                // upload background image
                $backgroundImageFilename = uniqid($business->id,true) . '-background.' . $backgroundFile->getClientOriginalExtension();
                $backgroundImage = Image::make($backgroundFile->getRealPath());
                $backgroundImage->resize(300,300, function($constraint) {
                    $constraint->aspectRatio();
                })->save($destinationPath . '/' . $backgroundImageFilename);

                $business->background_photo_path = 'businesses/' . $business->id . '/' . $backgroundImageFilename;
                $business->save();
            }

            if($photoFile instanceof UploadedFile){

                // upload profile image
                $profileImageFilename = uniqid($business->id,true) . '-profile.' . $photoFile->getClientOriginalExtension();
                $profileImage = Image::make($photoFile->getRealPath());
                $profileImage->resize(300,300, function($constraint) {
                    $constraint->aspectRatio();
                })->save($destinationPath . '/' . $profileImageFilename);

                $business->profile_photo_path = 'businesses/' . $business->id . '/' . $profileImageFilename;
                $business->save();

            }

        });

    }
karandatwani92 commented 1 year ago

Hey, @glenelkinsdev

could you please share the relationship code as well for a better understanding... I assume it is:

public function type(){
 return $this->belongsTo('App\Models\BusinessType', 'type_id');
}
glenelkinsdev commented 1 year ago

Hey, @glenelkinsdev

could you please share the relationship code as well for a better understanding... I assume it is:

public function type(){
 return $this->belongsTo('App\Models\BusinessType', 'type_id');
}
public function type() : HasOne
    {
        return $this->hasOne(BusinessType::class,'id','type_id');
    }
glenelkinsdev commented 1 year ago

i've just realised what my mistake is, i've created this as a one-to-one instead of a one-to-many