Skip to content

Working with a resource when update nova tries update other table #3693

@abkrim

Description

@abkrim
  • Laravel Version: 8.77.1
  • Nova Version: 3.30.0
  • PHP Version: 8.0.14
  • Database Driver & Version: mysql 8.0.27
  • Operating System and Version: Ubuntu 20.04
  • Browser type and version: Brave Versión 1.33.106 Chromium: 96.0.4664.110 (Build oficial) (64 bits)

Description:

Resource has issue with ONE column.

When I try to update get error 1054 of mysql, and message show a ilegal acction.

Nova tries to update a table usersinstead table of model of resource.

Error_Dns

Detailed steps to reproduce the issue on a fresh Nova installation:

Resource

<?php

namespace App\Nova;

use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Laravel\Nova\Fields\Boolean;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\Select;
use Laravel\Nova\Fields\Text;
use Laravel\Nova\Http\Requests\NovaRequest;

class Campaign extends Resource
{
    /**
     * The model the resource corresponds to.
     *
     * @var string
     */
    public static $model = \App\Models\Campaign::class;

    /**
     * The single value that should be used to represent the resource when being displayed.
     *
     * @var string
     */
    public static $title = 'campaign';

    /**
     * The columns that should be searched.
     *
     * @var array
     */
    public static $search = [
        'id', 'campaign', 'web'
    ];

    public static $group = 'Mailer';

    /**
     * Get the fields displayed by the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function fields(Request $request)
    {
        return [
            ID::make(__('ID'), 'id')->sortable(),

            Text::make('Campaña', 'campaign')
                ->sortable()
                ->rules(
                    'required',
                    'max:100',
                    'min:5',
                    Rule::unique('campaigns')->ignore($this->id),
                ),

            Text::make('Db', 'db')->rules('required', 'max:64', 'min:5')->hideFromIndex(),

            Text::make('Web', 'web')->rules('max:254')->hideFromIndex(),

            Text::make('Cp', 'cp_user'), //->rules('required', 'min:3', 'max:8')->hideFromIndex(),

            Boolean::make('DNS burcode', 'own_dns')->trueValue(1)->falseValue(0)->rules('required'),

            Text::make('Tabla', 'table')->rules('required','max:64')->hideFromIndex(),

            Text::make('Email', 'email_from')->rules('required', 'max:264')->hideFromIndex(),

            Text::make('Nombre Emisor', 'name_from')->rules('max:264')->hideFromIndex(),

            Text::make('Identificador', 'identifier')
                ->rules(
                    'size:8',
                    function ($attribute, $value, $fail) {
                        if (! preg_match('/^[A-Z0-9]{8}$/', $value)) {
                            $fail('Identificador debe tener 8 digitos en mayusculas y números solamente');
                        }
                    },
                    Rule::unique('campaigns')->ignore($this->id),
                )->hideFromIndex(),

            Select::make('Mailer', 'mailer')->options([
                1 => 'Sendgrid',
                2 => 'Smtp',
                3 => 'Mailtrap'
            ])->default(1)->displayUsingLabels(),

            Text::make('Estado', function ($model) {
                return  \App\Models\Campaign::STATUS_SELECT[$model->status];
            })->exceptOnForms(),
        ];
    }

    /**
     * Get the cards available for the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function cards(Request $request)
    {
        return [];
    }

    /**
     * Get the filters available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function filters(Request $request)
    {
        return [];
    }

    /**
     * Get the lenses available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function lenses(Request $request)
    {
        return [];
    }

    /**
     * Get the actions available for the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function actions(Request $request)
    {
        return [];
    }
}

Model

<?php

namespace App\Models;

use App\Values\CampaignExtra;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Campaign extends Model
{
    use HasFactory;

    public const STATUS_SELECT = [
        1 => 'Pendiente', // Without status (initial)
        2 => 'Configurada', // Tiene registros cpanel y sendgrid
        3 => 'Activa', // Sending campaign
        4 => 'Finalizada', // End sending
        5 => 'Archivada',
        6 => 'Pendiente DNS', //  Pending external DNS for verification
    ];

    public const MAILER_SELECT = [
        1 => 'Sendgrid',
        2 => 'Smtp',
        3 => 'Mailtrap'
    ];

    public $table = 'campaigns';

    protected $dates = [
        'start_in',
        'end_at',
        'datetime_events',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $guarded = [];

//    protected $fillable = [
//        'campaign',
//        'db',
//        'table',
//        'cpuser',
//        'own_dns',
//        'email_from',
//        'name_from',
//        'identifier',
//        'extra',
//        'datetime_event',
//        'start_in',
//        'end_at',
//        'status',
//        'mailer',
//        'extra->organizer_name',
//        'extra->organizer_address',
//        'extra->organizer_zipcode',
//        'extra->organizer_city',
//        'extra->organizer_phone',
//        'extra->organizer_person',
//        'external'
//    ];

    protected $casts = [
        'start_in' => 'datetime:Y-m-d H:i:s',
        'end_at' => 'datetime:Y-m-d H:i:s',
        'datetime_event' => 'datetime:Y-m-d H:i:s',
        'extra' => CampaignExtra::class,
        //'external' =>
    ];

    public function getStatusColorAttribute()
    {
        return [
            1 => 'yellow',
            2 => 'indigo',
            3 => 'pink',
            4 => 'green',
            5 => 'gray'
        ][$this->status];
    }

    public function getStartInForHumansAttribute()
    {
        return is_null($this->start_in)
            ? ''
            : $this->start_in->format('M, d Y');
    }

    public function getStartInForEditingAttribute()
    {
        return $this->start_in->format('Y-m-d H:i:s');
    }

    public function getDateTimeEventForHumansAttribute()
    {
        return is_null($this->datetime_event)
            ? ''
            : $this->datetime_event->format('M, d Y H:i:s');
    }

    public function getDateTimeEventForEditingAttribute()
    {
        return $this->datetime_event->format('Y-m-d H:i:s');
    }

    public function getEndAtForHumansAttribute()
    {
        return is_null($this->end_at)
            ? ''
            : $this->end_at->format('M, d Y');
    }

    public function getEndAtForEditingAttribute()
    {
        return $this->end_at->format('Y-m-d H:i:s');
    }

    public function getStatusLabelAttribute($value)
    {
        return static::STATUS_SELECT[$this->status] ?? null;
    }

    public function getExternalObjectAttribute()
    {
        return is_null($this->external)
            ? ''
            : json_decode($this->external);
        //return json_decode($this->external);
    }

    public function subscribers(): BelongsToMany
    {
        return $this->belongsToMany(Subscriber::class); //->using(CampaignSubscriber::class);
    }

    public function templates(): HasMany
    {
        return $this->hasMany(Template::class);
    }
}

Error when update --> SQLSTATE[42S22]: Column not found: 1054 Unknown column 'own_dns' inf 'field list' (SQL: update users set own_dns = 1, users.updated_at = 2021-12-24... wher id= 128

users ?

Any relations between Users and Campaigns.

Users

``php

'datetime', ]; public $table = 'users'; public function avatarUrl() { return $this->avatar ? Storage::disk('avatars')->url($this->avatar) : 'https://www.gravatar.com/avatar/'.md5(strtolower(trim($this->email))); } } ```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions