Combining Transactions WITH Queues in Laravel
<p><strong>Is there a way to tell the Queue facade to not automatically persist what was pushed to the queue until I specifically tell it to?</strong></p>

<pre><code>Queue::noPersist()
Queue::push()
Queue::push()
Queue::persist()
</code></pre>

<p>See my problem is as follows, my code execution is wrapped in a transaction. </p>

<p><a href="https://i.stack.imgur.com/9m8Tb.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/9m8Tb.jpg" alt="enter image description here"></a></p>

<p>Inside this transaction is some eloquent records being added and right after also a job being pushed to the queue. </p>

<p><strong>queue runs before transaction commits:</strong> The issue is that the queue sometimes starts running before my transaction finishes so it tries to reference a record that hasn't yet been "<em>committed</em>".</p>

<p>Provided my design is ok I'm thinking what I really need is for the queue to only submit after the transaction successfully commits: If the transaction fails then the job should also be rolled back so to speak. Or in other words the jobs shouldn't be pushed until after the transaction succeeds.</p>

<p><strong>queue delay</strong> I saw laravel has a delay I can add to the queue but I didn't want to hack a random number in there as that's a bit fragile I really just want to only submit the queues if transaction succeeds. </p>

<p>Any guidance or built in features that could be useful to help me out here?</p>

<pre><code>DB::transaction(function() {
    // add record A to DB
    // add JOB to QUEUE (this job starts firing before transaction commits and fails finding the record A)
    // more operations
});
</code></pre>