Create select box in laravel - php

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.

Related

Getting user categories sub categories

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... (

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();

İn add product page listing categories from database

Product
id
name
price
picture
categoryId
Category
id
name
CategoryId and Category_id are foreign key. I want to list all categories in add product page with select box using foreach. Can anyone help me? ı am new in laravel
In the controller, you are using for Product:-
public function create(){
$data['categories'] = Category::pluck('name', 'id');
return view('your_add_product_page', $data);
}
To show all the categories in the dropdown list, in the "your_add_product_page.blade.php":-
{{ Form::select('categories', $categories, null, ['placeholder' => 'Select Category']) }}
Hope, this will help.

Laravel sort child relationship collection

I have the following 3 tables:
Movie
- id
Series
- id
- status_id
- movie_id
Status
- id
- order_number
this is the code on the controller:
$movie = Movie::where('slug', $slug)->with(['series'])->first();
this is the code for view:
#foreach ($movie->series as $series)
{{ $series->name}}
#endforeach
how to sort $movie->series based on status->order_number? if it can be written on the model, so every order is only written once the controller?
is there a code that I can use for example like this:
$movie->series->sortBy('status.order_number');
Yes, but you will need to join status with series:
$movie = Movie::where('slug', $slug)->with([
'series' => function ($query) {
// Subquery on `series` table
return $query
// select whatever you'll need
->select('series.*')
// join with status
->join('status', 'series.status_id', '=', 'status.id')
// order by order number
->orderBy('status.order_number')
// * you can drop this if you select all the fields you need and use those
->with('status');
},
])->first();
Edit this ^ method will sort on SQL level, but you could also do this with collections:
#foreach ($movie->series->sortBy('status.order_number') as $series)
{{ $series->name}}
#endforeach
In that case also add .status to your with to avoid n + 1 problem: ->with(['series.status'])
The reason your attempt didn't work is because ->sortBy(..) doesn't mutate the collection, it just returns a new sorted one. This would work:
$movie->series = $movie->series->sortBy('status.order_number');

Building query to select from 3 tables in Laravel

Normally I know how to query and join three tables but here I can't figured it how to happen. The thing is that I have followed 3 tables:
category -> columns
id
name
image
sub-category -> columns
id
table1_id
name
image
Sub-sub-category -> columns
id
table2_id
name
image
So image can be added only to Category or to Category->Sub-Category or to third level Category->Sub-Category->Sub-Sub-Category
What I'm trying to do is when I show ALL images on page to show also to which category they are added.
I've made all relations in my models but I can't figured it out how to query exactly. Currently only querying all images like
SubSubCategories::all();
Example:
I have main category(Country) which has sub-category (district) which has sub-sub-category(city).
image1 is added to Sub-Sub-Category with name city. When images are listed I want to show
images1 added to Country->District->City.
Can someone show me example query or maybe my relations eg. columns in tables are wrong?
This is what I've tried so far
$subsubcategories = DB::table('sub_sub_category')
->join('sub_category', 'sub_sub_category.sub_cat_id', '=', 'sub_category.sub_cat_id')
->join('category', 'category.category_id', '=', 'sub_category.sub_cat_id')
->select('sib_sub_category.*', 'sub_category.*', 'category.*')
->get();
Then in my view
{{ $subsubcategory->sub_category->sub_cat_name }} > {{ $subsubcategory->sub_sub_cat_name }}
error
Undefined property: stdClass::$sub_category
$subsubcategories = DB::table('category')
->join('category', 'category.id', '=', 'sub_category.table1_id')
->join('sub_category', 'sub_category.id', '=', 'sub_sub_category.table2_id')
->select('category.*', 'sub_category.*', 'sub_sub_category.*')
->get();
Actually you are almost there. Just remove the sub_category from the view and will work.
{{ $subsubcategory->sub_cat_name }} > {{ $subsubcategory->sub_sub_cat_name }}
This is happen because your array currently is looks like
object(stdClass) {
["id"]=> int(1)
["table2_id"]=> int(1)
["table1_id"]=> int(1)
...
// all other columns
}
So actually they belongs to $subsubcategory and no need to bring sub_category and category in the view. Just display them in your $subsubcategory.

Categories