Accessor is not called

I’ve a model named Order :

<?php

namespace App;

use IlluminateDatabaseEloquentModel;
use CarbonCarbon;
class Order extends Model
{
    public function getsystemIdAttribute()
    {
        return 99;
    }
    public function setCreatedAtAttribute($value){
        $this->attributes['createdAt'] = Carbon::parse($value)->format('Y-m-d');
    }
    protected $fillable = ['customerId', 'itemId','systemId', 'createdAt'];
    public $timestamps = false;
}

The problem is that the accessor – getsystemIdAttribute()suprise isn’t getting called, I tried it on other fields, however the accessor isn’t called again.Of course I’ve changed the name form getsystemIdAttribute to getitemIdAttribute(for example). I copied the method on another model, and again changed the name a little bit and it worked. I fired up my debugger, and visited the view which is using this model, and to my surprise the method isn’t getting called, but the fields from the database are returned. The version of Laravel is 5.3 and the database is MySQL, the version of php is 7.
Any idea what my be the problem.


Source: stackoverflow-php

Laravel – Get row that has today between begin and end

I have a table that has two dates fields, begin and end.

I’m trying to get all rows that have begun but not yet ended. and if today is less than begin, they shouldn’t be included. and if today is grater than end they shouldn’t be included.

I’m using laravels eloquent. Here is what i’ve tried

$now = CarbonCarbon::now();

$promos = AppModelsLivePromo::
    where("begin", "<=", $now)
    ->where("end", ">=", $now)
    //Other conditions
    ->get(); 

This seemingly work, but it also selects rows that are greater have already ended.
How can i make this work as i expect?


Source: stackoverflow-php

Laravel Eloquent Pluck without loosing Key

I have the following collection in Laravel:

 ["TheNumbers":[{"episodeID":16818,"episodeNumber":100,"created_at":null,"updated_at":null,{"episodeID":16818,"episodeNumber":210,"created_at":"2017-02-20 21:30:38","updated_at":"2017-02-20 21:30:38"}]

If I run the following code:

$TheEpisode->TheNumbers->pluck('episodeNumber');

I will get the following result:

"TheNumbers":[100,210]

I would like to keep the keys for each number, how can I achieve this?


Source: stackoverflow-php

Laravel/Eloquent mutate model on join

I’m trying to mutate a model to display an array of strings and not an array of objects.

Current Output

{
  "id": 1,
  "company_name": "blah"
  "language": [
    {
      "name": "English"
    },
    {
      "name": "French"
    }
  ] 
}

Desired Output

 {
  "id": 1,
  "company_name": "blah"
  "language": ["English","French"]
}

Partner Model

class Partner extends Eloquent
{

   protected $table = 'property_managers';
   protected $visible = array('id','company_name','language');

    public function language(){
       return $this->belongsToMany('Language', 'property_manager_language', 'property_manager_id', 'language_id');
    }

}

Language Model

class Language extends Eloquent
{
    protected $table = 'languages';
    protected $visible = array('name');

    public function propertyManager()
    {
      return $this->belongsToMany('PropertyManager');
    }
}

I’m accessing it via the code snippet below

$pm = Partner::with('language')->get()->first();

I looked into mutators but it doesn’t seem to hit that attribute within the partner model.

public function getLanguageAttribute(){
   //run my code to flatten to array
}

So I’m curious as to if mutators don’t work on joins or how you would be able to modify the return on the join. I can add a mutator to the Language model but that will only modify the attributes of that specific model. Any insight would be helpful. Thanks


Source: stackoverflow-php