Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The relationship is not added at creation #190

Open
dorinniscu opened this issue Jun 13, 2019 · 0 comments
Open

The relationship is not added at creation #190

dorinniscu opened this issue Jun 13, 2019 · 0 comments
Labels
bug Something isn't working

Comments

@dorinniscu
Copy link

dorinniscu commented Jun 13, 2019

PHP: 7.3
Laravel: 5.8
Laravel Scout: 7.1
Algolia Scout extended: 1.6

class Page extends Model
{   
  use Searchable;

  public function toSearchableArray()
  {
    $array = [
      'title' => optional($this->content)->title,
      'extra' => $this->extra,
    ];

    return $array;
  }

  public function content()
  {
    return $this->morphOne(Content::class, 'contentable');
  }
   
  protected $fillable = ['extra'];

  protected $with = ['content'];
}

class Content extends Model
{
    public function contentable()
    {
      return $this->morphTo();
    }

    protected $fillable = ['title'];
   
    protected $touches = ['contentable'];
}

class PageController extends Controller
{
  public function store(Request $request)
  {
    $page = Page::create($request->all());

    $page->content()->create($request->all());
   
    return $page;
  }

  public function update(Request $request, $id)
  {
    $page = $this->findOrFail($id);

    $page->update($request->all());

    $page->content->update($request->all());

    $page = $page->fresh();

    return $page;
  }
}

Scout import of existing instances works fine, all instances are in sync.

On update the sync works fine, $touches does the job.

$array = [
  'title' => 'Title',
  'extra' => 'Extra field',
];

On store the content relation is null and not updated when is created.

$array = [
  'title' => null,
  'extra' => 'Extra field',
];
@nunomaduro nunomaduro added the bug Something isn't working label Jun 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants