Getting user categories sub categories - php

When user registers then he haves 2 steps.
He must choose categories what is he interest of and then in step two he must choose sub categories of categories what he chose in step one.
In step one he can chose and i save data but in step two i can't get sub categories what he chose.
In my categories table i'm having parent_id column so i for example when he chose category_id one in step two it must show categories where parent_id is 2
I have pivot table CategoryUser where he saves step one user_id and category_id
In controller i don't have nothing ))
public function stepTwoIndex()
{
$users = User::all();
return view('auth/student/step-two')->with('users', $users);
}
In blade
#foreach($users as $user)
#foreach($user->categories()->where('parent_id', $user->course_category_id)->get() as $category)
<div class="subcategory-checkboxs row">
<div class="main-sub col-lg-3">
<input class="main-category-checkbox" type="checkbox" id="main-subcategory">
<label class="main-category-checkbox" for="main-subcategory">{{ $category->name }}</label>
</div>
</div>
#endforeach
#endforeach
I'm new in this so it's very hard to me to do this i'm searching all day how to do this but... (

Related

How to exclude certains columns while using eloquent | Laravel

i have an group table. My main table. So i have group_hotels table this table is a pivot table. (id, group_id, hotel_id)
Now in my blade group edit page, i want to list before added pivot table like this code:
My Group model:
public function hotels(){
return $this ->belongsToMany('App\Models\Hotel','group_hotels');
}
My controller:
$group = Group::find($id);
$hotels=Hotel::all();
My blade hotel select2 field:
#foreach($hotels as $hotel)
<option value="{{$hotel->id}}">{{$hotel->name}}</option>
#foreach ($group->hotels as $item)
<option value="{{$item->id}}">{{$item->name}}</option>
#endforeach
#endforeach
The output like this: http://prntscr.com/10v13ed
Now what i want to do, in my group_hotels pivot table, if hotel id exist in group_hotels except the in the pivot table id nothing come hotel table.
In short: If the hotel_id part in the group_hotels table is present in my hotel table, I do not want to call it in the hotel variable.
One option you have is to exclude the Hotel's you don't want in your list at the query level:
Hotel::whereNotKey($group->hotels->modelKeys())->get();

How to use whereNotNull in controller while using with (' Many-to-many relationships') to show on the blade view?

I try to show the 'categories' from Many to Many relationships and use
whereNotNull at the same time but when I use whereNotNull to check that the users have approved then the categories from the relationships are disappeared in the blade view.
If I have not put whereNotNull. The list of users who are vendors still appears (approved and waiting for approval both of them are showing in the view with the category relationships). When I use whereNotNull the blade view will show only approved users without category relationships.
The explanation pictures.
When use whereNotNull click!
When not use whereNotNull click!
This is the view that I want to show the categories from the relationships.
#if(count($user->categories) > 0)
#foreach ($user->categories as $category)
<span class="badge badge-pill badge-info p-2 m-1">{{ $category->name }}</span>
#endforeach
#endif
The UserController
public function index()
{
$users = User::with('categories') // Eager loading
->where('role', 'vendor')
->whereNotNull('approved_at') //If I put whereNotNull the categories will now showing
->get(); but If I delete it, the relationships will appear in the
view again.
return view('admin.vendorDashboard')
->with('categories', Categories::all())
->with('users', $users);
}
}
User Model
public function categories()
{
return $this->BelongsToMany(Categories::class);
}
Categories Model
public function users()
{
return $this->BelongsToMany(User::class);
}
User Table
Id
User
Approved_at
Role
1
A
2020-12-24 18:40:59
Vendor
2
B
2020-12-24 18:39:59
Vendor
Category Table
Id
Category
1
C
2
D
user_category table
Id
user_id
category_id
1
1
1
2
1
2
Thank you in advance.
p.s. sorry for my bad English.

Problem with retrieving selected checkboxes from database in Laravel

I am trying to retrieve all checkbox values from database and those values that are selected need to be checked. I have many to many relationship between models and I currently am able to retrieve all values from database and to write them in blade or those values that are just selected (it is one OR the other) but I can't seem to find solution to retrieve them all that exist in database and those that are selected to be checked. Any help is appreciated. Here is my code and database tables.
user_profiles table
id user_id first_name
1 1 Mike
sport table
id name
1 Football
2 Basketaball
3 Handball
4 Etc...
user_sport table
id user_id sport_id
1 1 1
2 1 2
UserProfile.php
public function sports()
{
return $this->belongsToMany(UserSport::class, 'user_sport', 'user_id', 'sport_id');
}
UserSport.php
public function userProfiles()
{
return $this->belongsToMany(UserProfile::class, 'user_sport', 'sport_id', 'user_id');
}
UserProfileController.php
public function showProfile($username, Request $request)
{
$profileId = User::getIdFromUsername($username);
$sportsOptions = UserSport::get();
$userSportsOptions = UserProfile::findOrFail($profileId)->sports()->get();
return view('profile.show', compact('sportsOptions', 'userSportsOptions'));
}
show.blade.php
#foreach($sportsOptions as $sportsOption)
<label class="checkbox-inline">
<input type="checkbox" id="sport" name="sport[]" value="{{$userSportsOptions->id}}"> {{$userSportsOptions->name}}
</label>
#endforeach
So, I know that naming conventions aren't great but relationship works and with foreach and $sportsOptions in blade in this case and example it displays all four values from sport table and with $userSportsOptions it displays those two values (selected values). I need help on how to achieve that displays in blade all values and those that are selected to be selected.
#foreach($userSportsOptions as $userSportsOptionRow)
$selectedIds[] =$userSportsOptionRow->sport_id;
#endforeach
$userSportsOptions->id
#foreach($sportsOptions as $sportsOption)
<label class="checkbox-inline">
<input type="checkbox" id="sport" name="sport[]" value="{{$sportsOption->id}}" #if(in_array($sportsOption->id,$selectedIds))> {{$sportsOption->name}}
</label>
#endforeach```

count post with all categories in laravel 5

hello I have two tables one is categories another is ads_listings. In categories table there are 4 columns id, parent_category_id, category_slug, category_title. Here parent_category_id 0 indicates main category and rest of the thing indicates sub category. user post store in ads_listings table. Now I want to find post with specific categories like this
vehicles(3)
cars(2)
motorbike(1)
cycle(0)
here the problem is my code found only subcategories which have posts. my controller code is
public function countListingsByCategories()
{
return DB::table("ads_listings")
->select("categories.category_title",DB::raw("COUNT(ads_listings.category_id) as num_listings"))
->join("categories", "categories.id","=","ads_listings.category_id")
->groupBy("ads_listings.category_id")
->get();
}
views code is:
<ul class="row catelist">
#if(isset($categoriesNumListings) && count($categoriesNumListings))
#foreach($categoriesNumListings as $categoriesNumListings)
<li class="col-md-12">{{ $categoriesNumListings->category_title }} <span>({{ $categoriesNumListings->num_listings }}Listings )</span></li>
#endforeach
#endif
</ul>
my Category table is:
ads_listing table is:
I think the problem is that you are counting ad_listings when you primarily want to count categories and get the number of ad_listings, I would do something like:
Second shot :)
public function countListingsByCategories()
{
return DB::table("categories")
->select("categories.category_title",
DB::raw("categories.title,
CASE
WHEN (categories.parent_id = 0)
THEN (SELECT count(ads_listings.category_id)
FROM ads_listings
WHERE ads_listings.category_id in
(SELECT subcategory.id
FROM categories subcategory
WHERE subcategory.parent_id = categories.id))
ELSE (SELECT COUNT(ads_listings.category_id)
FROM ads_listings
WHERE ads_listings.category_id = categories.id)
END as numberOfPosts,
CASE
WHEN (categories.parent_id = 0)
THEN (select 'Category')
ELSE (select 'subCategory')
END as type
FROM categories"))
->get();
}

Create select box in laravel

I had a category table.It was include the id, name and remark fields.
I want to create a categories select box.So,I create a select box in laravel with foreach loop.
#foreach ($categories as $category)
{{ Form::select('category_id', array($category->id => $category->name))}}
#endforeach
But I got two select box. What I was wrong? I want one select box.
You need to pass an array of categories to Form::select():
{{ Form::select('category_id', $categories) }}
If you put it in a loop, it will create as many selects as categories you have.
Build that array in your model, repository or (even!) controller before passing it to your view.

Categories