Laravel Function at Scheduler

I have a laravel 5.2 project and i want to run laravel scheduler by specified time from database data, so i create schedule kernel like this :

protected function schedule( Schedule $schedule )
{
    // Run proposal schedule to generate albums
    $proposals = Proposal::whereStatus( true )->whereGenerated( false )->get();
    foreach ( $proposals as $key => $proposal ) {
        $time = date( 'i G ', strtotime( $proposal->duedate ) );
        $send_date = date( $time . 'j n * Y', strtotime( $proposal->created_at . ' + ' . $proposal->timeout . ' days' ) );
        $schedule->call( function() use( $proposal ) {
            $proposal->generateAlbums();
        } )->cron( $send_date );
    }
}

And it's work fine, until i reset my migration and try to migrate from the begining, i got an error like this

SQLSTATE[42P01]: Undefined table: 7 ERROR:  relation "proposals" does not exist

So i think it's not good if i place my code there, right??

where should i place my code for this??

Or it's already right and what i need just to check if database table are exists or not??

I need best solution to code just the way laravel provide..

Upvotes: 0

Views: 66

Answers (1)

Bhavesh B
Bhavesh B

Reputation: 1121

You can check if table exists or not by

if (Schema::hasTable('proposals')) {
    //Your code which needs to execute
}

Upvotes: 1

Related Questions