select option search in laravel - php

Please I need help, I have 2 select options fields of categories and states and a city input field. I will like to perform a search if category is only selected or if category and state is selected or if category, state and city is selected or if category and city is selected. I am using laravel framework for my project.
public function searchVendor (Request $request) {
$vendors = DB::table('vendors')
->join('categories', 'categories.id', '=', 'vendors.vendor_category_id')
->join('countries', 'countries.id', '=', 'vendors.country_id')
->join('states', 'states.id', '=', 'vendors.state_id')
->join('cities', 'cities.id', '=', 'vendors.city_id')
->select('vendors.*', 'categories.name as category_name', 'countries.name as country_name', 'states.name as state_name', 'cities.name as city_name')
->where([['vendors.vendor_category_id',$request->cid],['vendors.state_id',$request->state],['cities.name','LIKE',"%$request->location%"]])
->orderBy('vendors.featured', 'asc')
->paginate(21);
$venues = Menu::where('parent',8)->get();
$states = State::all();
return view('pages.search')->withVenues($venues)->withVendors($vendors)->withStates($states);
}
That is my search controller so far, but is not giving me what I want. Please help

Related

Nested query in laravel 5.5

I want to get my products info from AppServiceProvider
Logic
Category -> Subcategory -> Product
now I need to get products base on category id
note: you might think it's strange but it's not really, just imagine you want show products in category page from all subcategories of that category. Then you'll need such thing like i do. getting products base on category_id while you only save subcategory_id in products table.
this is my loop in AppServiceProdiver:
View::composer('welcome', function ($view) {
$categories = Category::join('admin_designs', 'admin_designs.category_id', '=', 'categories.id')->get();
foreach($categories as $category){
$subcategory = Subcategory::join('categories', 'categories.id', '=', 'subcategories.category_id')->first();
$designs = Product::where('subcategory_id', $subcategory)->first();
}
$view->with('categories', $categories);
});
result of that loop is:
{"id":2,"title":null,"slug":"laptop","image":"category-1516430091.jpg","imageAlt":null,"status_id":1,"meta_tags":"laptop,tags","meta_description":"laptop description","created_at":"2018-02-01 11:41:30","updated_at":"2018-02-01 11:41:30","category_id":1},
PS: have no idea what is that! is not product/ is not complete
category less title!... :|
anyone can help with fixing that query?
SOLVED
View::composer('welcome', function ($view) {
$category = DB::table('admin_designs')
->join('categories', 'categories.id', '=', 'admin_designs.category_id')
->join('subcategories', 'subcategories.category_id', '=', 'categories.id')
->join('products', 'products.subcategory_id', '=', 'subcategories.id')
->get();
$view->with('category', $category);
});
Hope it helps.

How to get Complex relation in a Laravel using eloquent

I have below tables
Users
id
name
Companies
id
user_id
name
Product Categories
id
company_id
name
Products
id
product_category_id
name
Sales
id
product_id
price
quantity
total
Now i want to get all sales for specified user's products. I have tried with below query. But i cannot use where for this. It is not apply this filter. Also i want to filter it by few other fields like users.user_name, products.product_name ... etc
$sales = Sale::with(['product'=>function($q) use ($user_id) {
$q->with(['product_category' => function($q) use ($user_id) {
$q->with(['company'=> function($q) use ($user_id) {
$sUserName = Input::get('sUserName');
$q->with(['user'=> function($q) use ($sUserName,$user_id) {
$q->where('id', $user_id);
}]);
}]);
}]);
}]);
Is it possible to use Eloquent for these situations ? Or should i use join queries ? Please advice me. thanks
you can use laravel eloqunt like this.
$Sales = DB::table('Sales')
->join('Products', 'Sales.product_id', '=', 'Products.id')
->join('ProductCategories', 'ProductCategories.id', '=', 'Products.product_category_id')
->join('companies', 'ProductCategories.company_id', '=', 'companies.id')
->join('Users', 'companies.user_id', '=', 'Users.id')
->select('Sales.*')
->get();
After madalinivascu advise i could be able to resolve my problem as below. Now i can filter it by Ordered User, Product name & dates.
$sUserName = Input::get('sUserName');
$sProductName = Input::get('sProductName');
$sFrom = Input::get('sFrom');
$sTo = Input::get('sTo');
$user_id = \Auth::User()->id;
// Show sales only for his products
$sales = Sale::with('sold_to_user')
->whereHas('sold_to_user', function($q) use ($sUserName) {
if(!empty($sUserName)) $q->where('name','LIKE', "$sUserName%");
})->with('product')
->whereHas('product', function($q) use ($sProductName) {
if(!empty($sProductName)) $q->where('name','LIKE', "$sProductName%");
});
if(\Auth::User()->privilege!='administrator') $sales = $sales->where('user_id','=',$user_id);
$sales = $sales->orderBy('updated_at','desc')->paginate(100);

How to order my query by most duplicates in Laravel?

On my website, users can post images.
Images can have tags.
There's 4 tables for this, the images table, the images_tag pivot table, the tag table, and of course the users table.
A user can have multiple images with the same tag(s).
I can pull up the tags a user has used across all his images with this query:
$userTags = Tag::whereHas('images', function($q) use($user) {
$q->where('created_by', $user->id);
})->get();
However, I want to make it so that I can order these tags based on how frequently a user uses them. In other words, I want to order by duplicates. Is this possible?
To achieve this, you're going to need to join the images_tags and images tables, count the number of tags, and order by those tags.
$tags = Tag::selectRaw('tags.*, COUNT(images.id) AS total')
->join('images_tags', 'tags.id', '=', 'images_tags.tag_id')
->join('images', 'images.id', '=', 'images_tags.image_id')
->where('images.created_by', $user->id)
->groupBy('tags.id')
->orderBy('total', 'desc')
->get();
The above query will only work in MySQL if the only_full_group_by option is disabled. Otherwise, you're going to need to either rewrite this to use a sub query, or do the ordering in the returned Laravel Collection. For example:
$tags = Tag::selectRaw('tags.*, COUNT(images.id) AS total')
->join('images_tags', 'tags.id', '=', 'images_tags.tag_id')
->join('images', 'images.id', '=', 'images_tags.image_id')
->where('images.created_by', $user->id)
->groupBy('tags.id')
->get();
$tags = $tags->sortByDesc(function ($tag) {
return $tag->total;
});
If you want to add this to your user model, per your comment, create a function similar to the following:
public function getMostUsedTags($limit = 3)
{
return Tag::selectRaw('tags.*, COUNT(images.id) AS total')
->join('images_tags', 'tags.id', '=', 'images_tags.tag_id')
->join('images', 'images.id', '=', 'images_tags.image_id')
->where('images.created_by', $this->id)
->groupBy('tags.id')
->orderBy('total', 'desc')
->limit($limit)
->get();
}

How can I retrieve row from another table in Laravel Eloquent?

I have 3 tables: posts, votes and users. I have written a neat code with Eloquent to retrieve just the posts where the user has not voted yet on.
$posts = Post::whereDoesntHave("votes")
->where('user_id', '=', $user_id)
->whereBetween('posts.created_at', array(Carbon::now()->subHours(48), Carbon::now()))
->take(20)
->get();
return response()->json(['posts' => $posts])->withCallback($request->input('callback'));
But also I want to retrieve user name from table users. I want to pass the data with json.
If I try to do it with query builder, it is hard to eliminate posts that have been voted already by the user.
You can do a join manually to the user table
$posts = Post::join('users', 'users.id', '=', 'posts.user_id')
->whereDoesntHave("votes")
->where('user_id', '=', $user_id)
->whereBetween('posts.created_at', array(Carbon::now()->subHours(48), Carbon::now()))
->take(20)
->get();
Or you could define a relationship in the Post model class.
public function user()
{
return $this->belongsTo('App\User');
}
Then you use with('user') to retrieve data from user table.
$posts = Post::with('user')
->whereDoesntHave("votes")
->where('user_id', '=', $user_id)
->whereBetween('posts.created_at', array(Carbon::now()->subHours(48), Carbon::now()))
->take(20)
->get();

Laravel Eloquent query get users from another model

I have another table called tableb and it has a user relationship defined through the user_id field.
I want to run a query against tableb where a certain date is within a certain range but then I want to grab the user table associated with that row but I only want it to grab the user if it's not been grabbed yet. I'm trying to do this all in 1 DB query. I have most of it done, but I'm having trouble with the unique part of it.
Here's what I have right now:
$tableB = TableB::select('users.*')
->join('users', 'tableb.user_id', '=', 'users.id')
->where('tableb.start_date', '>', date('Y-m-d'))
->get();
So right now I have 3 entries in tableB from the same user, and ideally I'd like to only get 1 entry for that user.
How would I go about doing this?
Since you're selecting only users data, just add a groupBy clause in your query.
$tableB = TableB::select('users.*')
->join('users', 'tableb.user_id', '=', 'users.id')
->where('tableb.start_date', '>', date('Y-m-d'))
->groupBy('users.id')
->get();
You should just add groupBy like this :
$tableB = TableB::select('users.*')
->join('users', 'tableb.user_id', '=', 'users.id')
->where('tableb.start_date', '>', date('Y-m-d'))
->groupBy('users.id')
->get
Try This Code
App/user.php
public function getrelation(){
return $this->hasMany('App\tableB', 'user_id');
}
In Your Controller
Controller.php
use App/user;
public funtion filterByDate(user $user)
{
$date = '2016-02-01';
$result = $user->WhereHas('getrelation', function ($query) use($date) {
$query->whereDate('tableb.start_date', '>', $date)
->first();
});
}

Categories