我在Laravel 5.5中运行每日计划,它选择匹配的帖子并运行一个foreach循环,在PostController中调用静态函数,编辑Post模型并在之后保存它。
执行代码时,第一个帖子的第一个循环一切正常,但是在第二个过程中,当函数尝试保存模型时,我得到function name must be a string
错误。
以下是kernel.php
$schedule->call(function () {
$posts = DB::table('post')
->where('finish_time', '<', Carbon::now()->toDateTimeString())
->where('status', '=', '1')
->where('expiry_processed_at', '=', null)
->get()->all();
if(count($posts) > 0){
foreach ($posts as $post){
PostController::processExpiry($post->post_id);
}
}
})->everyMinute();
这是它在PostController中调用的函数:
public static function processExpiry($post_id){
$post = Study::find($post_id);
$latestLicense = $post->licenses->last();
$email = $post->user->email;
$param = ['post' => $post];
\Mail::send(['html' => 'emails.post_expiry', 'text' => 'emails.txt.post_expiry'], $param, function ($message) use ($email) {
$message->to($email)
->subject(trans('email_subject.post_expiry'));
});
// Identify post as processed
$post->expiry_processed_at = Carbon::now()->toDateTimeString();
$validator = \Validator::make(
$post->toArray(),
$post->rules
);
Log::info(print_r($post,true));
try {
$post->saveOrFail(); // This is the line where error occurs on second pass
} catch (\Exception $e) {
// catch is never triggered
$messages = $e->getMessage();
return (Object) array('status' => false, 'error_message' => 'Error saving post to database');
}
// return because same function can be called manually by user
return (Object) array('status' => true);
}
在Log::info()
输出之前$post
的{p> $post->saveOrFail();
:
[2018-02-01 10:25:00] local.INFO: App\Models\Post Object
(
[table:protected] => post
[timestamps] =>
[guarded:protected] => Array
(
[0] => edit_time
)
[incrementing] => 1
[primaryKey] => post_id
[dates:protected] => Array
(
[0] => deleted_at
)
[refundThreshold] => 10
[rules] => Array
(
[user_id] => Array
(
[0] => required
[1] => integer
)
[title] => Array
(
[0] => required
[1] => max:255
[2] => App\Rules\NotUrl Object
(
)
)
[description] => Array
(
[0] => required
)
[finish_time] => Array
(
[0] => sometimes
[1] => max:32
[2] => min:10
)
[status] => Array
(
[0] => integer
)
[create_time] => Array
(
[0] => max:32
)
[create_ip] => Array
(
[0] => ip
)
[edit_time] => Array
(
[0] => max:32
)
[edit_ip] => Array
(
[0] => ip
)
)
[connection:protected] => mysql
[keyType:protected] => int
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[post_id] => 7
[user_id] => 167
[description] => Lorem ipsum
[finish_time] => 2016-07-01 00:00:00
[expiry_processed_at] => 2018-02-01 10:25:00
[status] => 1
[create_time] => 2018-01-30 21:27:11
[create_ip] =>
[edit_time] => 2014-01-31 21:28:27
[edit_ip] =>
[deleted_at] =>
)
[original:protected] => Array
(
[post_id] => 7
[user_id] => 167
[description] => Lorem ipsum
[finish_time] => 2016-07-01 00:00:00
[expiry_processed_at] =>
[status] => 1
[create_time] => 2014-02-05 21:27:11
[create_ip] =>
[edit_time] => 2014-08-11 15:54:22
[edit_ip] =>
[deleted_at] =>
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
[licenses] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
)
[touches:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[fillable:protected] => Array
(
)
[forceDeleting:protected] =>
)
感谢您的帮助!
答案 0 :(得分:0)
以前不能 $后&GT; saveOrFail();
使用var_dump($ post)或simple dd($ post);
调试$ post变量答案 1 :(得分:0)
所以你的代码就像这样
$schedule->call(function () {
$posts = DB::table('post')
->where('finish_time', '<', Carbon::now()->toDateTimeString())
->where('status', '=', '1')
->where('expiry_processed_at', '=', null)
->get()->all();
if(count($posts) > 0){
foreach ($posts as $post){
PostController::processExpiry($post);
}
}
})->everyMinute();
控制器:
public static function processExpiry($post){
$latestLicense = $post->licenses->last();
$email = $post->user->email;
$param = ['post' => $post];
\Mail::send(['html' => 'emails.post_expiry', 'text' => 'emails.txt.post_expiry'], $param, function ($message) use ($email) {
$message->to($email)
->subject(trans('email_subject.post_expiry'));
});
// Identify post as processed
$post->expiry_processed_at = Carbon::now()->toDateTimeString();
$validator = \Validator::make(
$post->toArray(),
$post->rules
);
Log::info(print_r($post,true));
try {
$post->saveOrFail(); // This is the line where error occurs on second pass
} catch (\Exception $e) {
// catch is never triggered
$messages = $e->getMessage();
return (Object) array('status' => false, 'error_message' => 'Error saving post to database');
}
// return because same function can be called manually by user
return (Object) array('status' => true);
}