1

In my Laravel 8 project I have a model called Campaign, my front-end though is build in Vue JS so needs to have some keys on a Campaign for contextual purposes, such as opening and closing a dropdown menu when looping over the elements, a database column isn't nessecery for this.

I'd like to add some default key/value pairs to my Campaign model, for example: dropdown_is_open and should have a default value of false.

I came across the default attributes for a model and tried adding this but cannot see my new key on the object, what am I missing?

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Campaign extends Model
{
    use HasFactory, SoftDeletes;

    /**
     * Indicates if the model's ID is auto-incrementing.
     *
     * @var bool
     */
    public $incrementing = false;

    /**
    * The table associated with the model.
    *
    * @var string
    */
    protected $table = 'campaigns';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'campaign',
        'template'
    ];

    /**
     * The model's default values for attributes.
     *
     * @var array
     */
    protected $attributes = [
        'dropdown_is_open' => false
    ];
}

Index function in controller:

/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $campaigns = Campaign::where('user_id', Auth::id())
                         ->orderBy('created_at', 'desc')
                         ->get();

    if (!$campaigns) {
        return response()->json([
            'message' => "You have no campaigns"
        ], 404);
    }

    return response()->json([
        'campaigns' => $campaigns
    ], 200);
}

I expect to see:

{
  campaign: 'my campaign',
  template: '',
  dropdown_is_open: false <-- my key
}

Previously I was doing a foreach in my index function and adding the contextual keys on each item, but this would only show for the index function and I'd have to add it everywhere.

4
  • Rather than adding what feels like some front end display logic to your model (seems janky to me) why not just maintain either a single value, or array of values in your Vue view? The value(s) would correspond with the element index from your collection of models. Commented Feb 27, 2022 at 12:53
  • A single value wouldn't work, it's a v-for so in my case, opening a single item's dropdown would therefore open all of the dropdowns - bad ui. My question here is perfectly acceptable, I've simply asked why my current code isn't working and what I'm missing, please advise. Commented Feb 27, 2022 at 13:05
  • How would it open all dropdowns if the value corresponds to a specific element index within an iterable? I never said your question wasn't acceptable, simply provided an alternative and my reasoning for the suggestion. Commented Feb 27, 2022 at 13:13
  • Thanks for your alternative suggestion, however I do wish to receive an answer for my original question and attempt attached above. Commented Feb 27, 2022 at 14:19

1 Answer 1

1

I hope something like below helps.

Change it from my_custom_field to dropdown_is_open key (and from getMyCustomFieldAttribute to getDropdownIsOpenAttribute method-name).

Custom attribute (or Accessor)

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model {
    protected $appends = ['my_custom_field'];

    public function getMyCustomFieldAttribute()
    {
        return false;
    }
}

The $appends in above is required only, to ensure that my_custom_field is preset/cached, and even sent as JSON-Response.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.