2024-03-28 14:52:40 +05:30
< ? php
namespace App\Http\Controllers\Frontend ;
use App\Http\Controllers\Controller ;
use App\Models\Admin\Tag ;
use Illuminate\Http\Request ;
use App\Models\Admin\Blog ;
class InsightsController extends Controller
{
//
public function index ()
{
$blogs = $this -> filterData ( request () -> search ? ? '' );
// $blogs = $this->filterData('');
$recent_post = Blog :: where ( 'is_active' , true ) -> latest () -> paginate ( 6 );
// $recent_post = Blog::where('is_active', true)->latest()->take(6)->get();
$categories = Tag :: whereHas ( 'blogs' , function ( $query ) {
$query -> where ( 'is_active' , true );
}) -> withcount ( 'blogs' ) -> get ();
$tags = Tag :: all ();
// dd($recent_post);
return view ( 'Frontend.Pages.resources.insights' , compact ( 'blogs' , 'recent_post' , 'categories' , 'tags' ));
}
public function readMore ( $slug )
{
$blogs = Blog :: where ( 'is_active' , true ) -> where ( 'slug' , $slug ) -> firstorFail ();
// dd($blogs);
// return $blog;
$all_blogs = Blog :: where ( 'is_active' , true ) -> where ( 'slug' , '!=' , $slug ) -> take ( 2 ) -> get ();
$recent_post = Blog :: where ( 'is_active' , true ) -> where ( 'slug' , '!=' , $slug ) -> latest () -> take ( 4 ) -> get ();
$categories = Tag :: whereHas ( 'blogs' , function ( $query ) {
$query -> where ( 'is_active' , true );
}) -> withcount ( 'blogs' ) -> get ();
return view ( 'Frontend.Pages.resources.readmore' , compact ( 'blogs' , 'recent_post' , 'all_blogs' , 'categories' ));
}
public function tagDetail ( Request $request )
{
$id = $request -> get ( 'id' );
$blogs = Blog :: query ()
-> leftJoin ( 'tags' , 'blog.tag_id' , 'tags.id' )
-> when ( request ( 'id' ) !== '0' , function ( $query ) use ( $id ) {
return $query -> where ( 'tags.id' , $id );
})
-> select ( 'blog.*' )
-> get ();
$data = '' ;
foreach ( $blogs as $blog ) {
$date = $blog -> created_at -> format ( 'F jS, Y' );
$route = route ( 'readmore' , $blog -> slug );
$blog_title = mb_strimwidth ( $blog -> blog_title , 0 , 50 , " ... " );
$description = mb_strimwidth ( $blog -> blog_description , 0 , 150 , " ... " );
$data .= " <div class='col-md-6' style='height: 425px; margin-bottom:20px;'>
< div class = 'insight-card h-100' >
< img src = '$blog->blog_image' />
< h2 > $blog_title </ h2 >
< span >
< i class = 'fa fa-calendar-minus-o' aria - hidden = 'true' ></ i >
$date -
</ span >
$description </ p >
< a href = '$route' > Read More </ a >
</ div >
</ div > " ;
}
if ( $data ) {
return $data ;
}
return 'No Data Found' ;
}
public function getAllTags ()
{
$tags = Tag :: has ( 'blogs' ) -> select ( 'id' , 'tag_name' ) -> get ();
return response () -> json ([
" status " => " success " ,
" code " => 200 ,
" data " => $tags
]);
}
public function getAllBlogs ( Request $request )
{
if ( $request -> method () == 'POST' ) {
$search = $request -> search ;
$tag_id = $request -> tag_id ;
}
$blogs = Blog :: query () -> when ( request ( 'search' ), function ( $query ) use ( $search ) {
$query -> where ( 'blog_title' , 'like' , '%' . $search . '%' )
-> orWhere ( 'blog_description' , 'like' , '%' . $search . '%' );
}) -> when ( request ( 'tag_id' ), function ( $query ) use ( $tag_id ) {
$query -> where ( 'tag_id' , $tag_id );
})
-> where ( 'is_active' , true )
-> select ( 'id' , 'blog_image' , 'blog_title' , 'blog_description' , \DB :: raw ( " DATE_FORMAT(created_at, '%M %d,%Y') as date " ), 'blog_description as minutes_to_read' )
2024-04-08 16:12:02 +05:30
-> latest ()
2024-03-28 14:52:40 +05:30
-> get ();
if ( $blogs ) {
return response () -> json ([
" status " => " success " ,
" code " => 200 ,
" data " => $blogs
]);
} else {
return response () -> json ([
" status " => " failed " ,
" code " => 400 ,
]);
}
}
public function singleBlog ( $id )
{
$blog = Blog :: where ( 'is_active' , true ) -> findOrFail ( $id );
return response () -> json ([
" status " => " success " ,
" code " => 200 ,
" data " => $blog
]);
}
function fetchBlog ( Request $request )
{
if ( $request -> ajax ()) {
$blogs = $this -> filterData ( $request -> get ( 'query' ));
// dd($blogs);
return view ( 'Frontend.Pages.resources.insights-component' , compact ( 'blogs' )) -> render ();
}
}
public function filterData ( $query )
{
$blogs = Blog :: where ( 'is_active' , 1 )
// ->count();
// ->orWhere('blog_title', 'like', '%' . $query . '%')
-> where ( function ( $q ) use ( $query ) {
$q -> orWhere ( 'blog_title' , 'like' , '%' . $query . '%' );
$q -> orWhere ( 'blog_description' , 'like' , '%' . $query . '%' );
})
// ->whereAny(['blog_title','blog_description'], 'LIKE',"%' $query%")
// ->orWhere('blog_description', 'like', '%' . $query . '%')
-> latest ()
-> paginate ( 6 );
// return $blogs;
$blogs -> appends ([ 'search' => $query ]);
return $blogs -> withPath ( '/insights' );
}
}