Ad
Object Of Class Closure Could Not Be Converted To String In Laravel
Object of class Closure could not be converted to string in laravel, i am trying get post by month and year
here is controller
public function getPostsByArchive($slug)
{
$archiveposts = \Canvas\Post::whereDate('published_at', date('F-Y'),function ($query) use ($slug) {
$query->where('published_at', date('F-Y'), $slug);
})->published()->orderByDesc('published_at')->get();
return view('posts.archive', compact('archiveposts'));
}
here is route
Route::get('archive/{slug}', '[email protected]')->name('posts.archive');
Ad
Answer
You're using whereDate
wrongly, passing a closure as 3rd arg causes an error. Instead you may use whereRaw
and format your published_at
column using MySQL's date_format
function:
$archiveposts = \Canvas\Post
::whereRaw("date_format(published_at, '%M-%Y') = ?", [$slug])
->published()
->orderByDesc('published_at')
->get();
return view('posts.archive', compact('archiveposts'));
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad