Pass id from public function to other public function - php

I got a public function call header:
public function header() {
$link = Link::with('page', 'tag')->orderBy('clicks', 'desc')->first();
$link_id = $link->id;
return view('site.templates.linksheader', compact('link'));
}
And I got a public function call index2:
public function index2() {
$links = Link::where('status', '=', 1)
->orderBy('clicks', 'desc')
->with('page', 'tag')
//->whereNotIn('id', $id);
->take(12)
->get();
}
In the ->whereNotIn of index2, I want to get the $id of function header()...

You can do
public function header() {
$link = $this->getFirstLink();
return view('site.templates.linksheader', compact('link'));
}
And
public function index2() {
$links = Link::where('status', '=', 1)
->orderBy('clicks', 'desc')
->with('page', 'tag')
->whereNotIn('id', $this->getFirstLink()->id);
->take(12)
->get();
}
And
private function getFirstLink() {
return Link::with('page', 'tag')->orderBy('clicks', 'desc')->first();
}

Related

How to show total products number category wise?

I need a little help. I want to show how many products are published category wise. Here I am using this code to show how many products published by seller.
{{ \App\Product::where('published', 1)->where('added_by', 'seller')->get()->count() }}
Please help me regarding this issue. I had share my category and product DB table with Screenshot.
Category DB
Product DB
Here is categorycontroller
public function index(Request $request)
{
$sort_search =null;
$categories = Category::orderBy('order_level', 'desc');
if ($request->has('search')){
$sort_search = $request->search;
$categories = $categories->where('name', 'like', '%'.$sort_search.'%');
}
$categories = $categories->paginate(15);
return view('backend.product.categories.index', compact('categories', 'sort_search', products));
}
Here is ProductController
public function admin_products(Request $request)
{
//CoreComponentRepository::instantiateShopRepository();
$type = 'In House';
$col_name = null;
$query = null;
$sort_search = null;
$products = Product::where('added_by', 'admin');
if ($request->type != null){
$var = explode(",", $request->type);
$col_name = $var[0];
$query = $var[1];
$products = $products->orderBy($col_name, $query);
$sort_type = $request->type;
}
if ($request->search != null){
$products = $products
->where('name', 'like', '%'.$request->search.'%');
$sort_search = $request->search;
}
$products = $products->where('digital', 0)->orderBy('created_at', 'desc')->paginate(15);
return view('backend.product.products.index', compact('products','type', 'col_name', 'query', 'sort_search'));
}
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function seller_products(Request $request)
{
$col_name = null;
$query = null;
$seller_id = null;
$sort_search = null;
$products = Product::where('added_by', 'seller');
if ($request->has('user_id') && $request->user_id != null) {
$products = $products->where('user_id', $request->user_id);
$seller_id = $request->user_id;
}
if ($request->search != null){
$products = $products
->where('name', 'like', '%'.$request->search.'%');
$sort_search = $request->search;
}
if ($request->type != null){
$var = explode(",", $request->type);
$col_name = $var[0];
$query = $var[1];
$products = $products->orderBy($col_name, $query);
$sort_type = $request->type;
}
$products = $products->where('digital', 0)->orderBy('created_at', 'desc')->paginate(15);
$type = 'Seller';
return view('backend.product.products.index', compact('products','type', 'col_name', 'query', 'seller_id', 'sort_search'));
}
public function all_products(Request $request)
{
$col_name = null;
$query = null;
$seller_id = null;
$sort_search = null;
$products = Product::orderBy('created_at', 'desc');
if ($request->has('user_id') && $request->user_id != null) {
$products = $products->where('user_id', $request->user_id);
$seller_id = $request->user_id;
}
if ($request->search != null){
$products = $products
->where('name', 'like', '%'.$request->search.'%');
$sort_search = $request->search;
}
if ($request->type != null){
$var = explode(",", $request->type);
$col_name = $var[0];
$query = $var[1];
$products = $products->orderBy($col_name, $query);
$sort_type = $request->type;
}
$products = $products->paginate(15);
$type = 'All';
return view('backend.product.products.index', compact('products','type', 'col_name', 'query', 'seller_id', 'sort_search'));
}
Here is Category Model
class Category extends Model
{
protected static function boot()
{
parent::boot();
static::addGlobalScope('alphabetical', function (Builder $builder) {
$builder->orderBy('name', 'asc');
});
}
public function subCategories()
{
return $this->hasMany(SubCategory::class);
}
public function subSubCategories()
{
return $this->hasMany(SubSubCategory::class);
}
public function products()
{
return $this->hasMany(Product::class);
}
}
Here is Product Model
class Product extends Model
{
// protected $fillable = ['current_stock', 'variations', 'num_of_sale'];
protected $fillable = ['variations', 'num_of_sale'];
protected static function boot()
{
parent::boot();
static::addGlobalScope('published', function (Builder $builder) {
$builder->where('published', 1);
});
}
public function user()
{
return $this->belongsTo(User::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function brand()
{
return $this->belongsTo(Brand::class);
}
public function stocks()
{
return $this->hasMany(ProductStock::class);
}
public function wishlists() {
return $this->hasMany(Wishlist::class);
}
public function taxes() {
return $this->hasMany(ProductTax::class);
}
public function flash_deal_product() {
return $this->hasOne(FlashDealProduct::class);
}
}
You need to use groupBy() on the category_id column. to give you and idea, you could write something like:
{{ \App\Product::selectRaw('category_id', 'count(*)')->where('published', 1)->where('added_by', 'seller')->groupBy('category_id')->get() }}

what will be laravel query to sort article with maximum like

table:-
1.articles
column:= id, title, description
2.article_likes
column:= id, user_id, article_id
ArticleController.php
public function index (Request $request)
{
$articles = Article::join('article_likes', 'articles.id', '=','article_likes.article_id');
if ($request->sort == "newest") {
$articles->orderBy('id', 'DESC');
}
if ($request->sort == "popular") {
??
}
$articles = $articles->get();
dd($articles);
}
public function index (Request $request) {
$articles = Article::join('article_likes', 'articles.id', '=','article_likes.article_id');
if ($request->sort == "newest") {
$articles->orderBy('id', 'DESC');
}
if ($request->sort == "popular") {
$articles->max('like_column_name')
}
$articles = $articles->get();
dd($articles);
}
Let me know if it helps
public function index(Request $request)
{
$articles = Article::with('articleLike');
if ($request->sort == "newest") {
$articles->orderBy('id', 'DESC');
}
if ($request->sort == "popular") {
$articles->withCount('articleLike')
->orderBy('article_like_count', 'desc');
}
$articles = $articles->get();
return $this->sendResponse("Article Sorted", $articles);
}

Laravel output model ordered by related table column

I have this query filter which works fine to query out the result. For example, if I need to find books which have the Author name something:
Book::with('user', 'author')
->whereHas('author', function ($query) use($filters) {
$query->filter($filters);
})->paginate(30);
But the thing is if I want to order the Books by the author names. I this case nothing happens.
How can I sort books by author name?
QueryFilter looks like this:
public function __construct(Request $request) {
$this->request = $request;
$this->filterService = new FilterService();
}
public function apply(Builder $builder) {
$this->builder = $builder;
foreach ($this->filters() as $name => $value) {
if (method_exists($this, $name)) {
call_user_func_array([$this, $name], array_filter([$value]));
}
}
return $this->builder;
}
public function filters() {
return $this->filterService->removeEmptyFieldsFromRequest($this->request);
}
And the book filter looks like this:
public function date($date) {
return $this->builder->where('date', $date);
}
public function name_sort($order = 'asc') {
return $this->builder->orderBy('name', $order);
}
You can use class join statement :
$books = Book::select('author.name AS author_name, countries.*')
->with('user')
->join('author', 'author.id', '=', 'book.author_id')
->orderBy('author.name', 'ASC')
->paginate(10);

Merge laravel 4 multiple relashionships

I have two relashionships in laravel v4.2. When I merge these two relashion into third function and then I call third function then I receive Uncought exception. Below is my code
public function following_friend() {
return $this->hasOne('Friend', 'following_id', 'id');
}
public function follower_friend() {
return $this->hasOne('Friend', 'follower_id', 'id');
}
public function mutual_friends() {
return $this->following_friend->merge($this->follower_friend);
}
public static function get_users_infomation_by_ids($login_id, $users_arr = array()) {
$users = User::where(function($sql) use($login_id, $users_arr) {
$sql->whereIn('id', $users_arr);
})
->with('mutual_friends')
->get(array('id', 'username', 'full_name', 'is_live', 'message_privacy', 'picture'));
return (!empty($users) && count($users) > 0) ? $users->toArray() : array();
}
I don't know that where is the problem in merging these two relashionships.
Try to write like below :-
public static function get_users_infomation_by_ids($login_id, $users_arr = array()) {
$users = User::with('mutual_friends')
->where(function($sql) use($login_id, $users_arr) {
$sql->whereIn('id', $users_arr);
})
->get(array('id', 'username', 'full_name', 'is_live', 'message_privacy', 'picture'));
return (!empty($users) && count($users) > 0) ? $users->toArray() : array();
}
Since I have used like this for one of my Project :-
public function getShipmentDetails($shipmentId = null) {
$response = Shipment::with('pdDetails','shipmentDocuments')
->where('id', $shipmentId)
->first();
if($response) {
return $response->toArray();
}
}
It works for me.

Pass parameter from one function to other function in the same Controller Laravel

I have two functions in the same controller. How can i pass $id from search($id) function to searchcond() function.
My First function:
public function search($id)
{
return view('frontend.studentinfoajax', compact('data_dep', 'data_branch_dep'));
}
My Second Function:
public function searchcond()
{
return DB::table('studentinfo')
->where('branchID', '=', $id)
->orderby('fullname')
->get();
}
How can i pass $id from search($id) function to searchcond() function.
I dont know why you need like that but still if you need you can use class properties
private $id; //define property to the class
public function search($id)
{
$this->id=$id;
return view('frontend.studentinfoajax',
compact('data_dep',
'data_branch_dep'));
}
public function searchcond()
{
$id=$this->id;
return DB::table('studentinfo')
->where('branchID', '=', $test)
->orderby('fullname')
->get();
}
After seeing the code snippets you have provided what i understood that you want to acheive something like this :
public function search($id)
{
$data = $this->searchcond($id);
return view('frontend.studentinfoajax', compact('data_dep', 'data_branch_dep'));
}
private function searchcond($id)
{
return DB::table('studentinfo')
->where('branchID', '=', $id)
->orderby('fullname')
->get();
}

Categories