⚡
Laravel Haystack
View Code On GithubEdit Docs
  • 🎯Getting Started
    • Introduction
    • How It Works
    • Installation
    • Usage
    • Configuration
    • Callback Events
  • 🛰️Cool Features
    • Shared Data
    • Shared Models
    • Long Delays & Pauses
    • Appending & Prepending Jobs
    • Chunking Jobs
  • 🗺️Next Up
    • Allowing Failed Jobs
    • Global Middleware
    • Connection, Queue & Delay
    • Naming Haystacks
    • Before Saving Hook
    • Custom Options
  • 🧼Cleanup
    • Deleting Stale Haystacks
    • Deleting Specific Haystacks
    • Clearing All Haystacks
  • 🛣️Finish
    • Support
    • Star On Github
    • Edit Documentation
Powered by GitBook
On this page
  1. Cool Features

Shared Models

You can also provide models when creating Haystacks which will be accessible to every job. This is extremely useful as you don't have to pass the model into every job. Just use the withModel method when building your Haystack to store a model.

<?php

$user = Auth::user();

Haystack::build()
    ->addJob(new RecordPodcast)
    ->withModel($user)
    ->dispatch();

You can also provide an optional $key as the second argument.

<?php

$user = Auth::user();

Haystack::build()
    ->addJob(new RecordPodcast)
    ->withModel($user, 'admin')
    ->dispatch();

Then, inside your jobs you can call the getHaystackModel method. If you did not provide a key, you can just pass in the model's class name.

$user = $this->getHaystackModel(User::class);

// Or if you specified a key...

$this->getHaystackModel('admin');

Laravel Haystack will serialize your models including their loaded relationships. It will only retrieve the model and the relationships when you attempt to get the model.

PreviousShared DataNextLong Delays & Pauses

Last updated 2 years ago

🛰️