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```
Related
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... (
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();
I am working with Laravel 5.6 and mysql in my app. and I am new to the Laravel. I have following table as vehicles,
id name number user_id
1 car 123 1
2 van 258 2
3 lorry 125 5
4 car 896 7
and user table like this,
id name email
1 hyu agt#mail.com
2 opv yyuj#mail.com
3 kio juik#mail.com
4 lop kiuf#mail.com
5 hyg htgd#mail.com
Now I need print email using above vehicles table user_id in my blade file, how can I do this?
Edit
I have following blade file to print vehicles table,
{{$vehicle->name}}
{{$vehicle->number}}
edit
my vehiclecontroller,
public function show($id)
{
$vehicles = Vehicle::find($id);
return view('vehicles.show')->withVehicles($vehicles);
}
and show blade file,
<div class="col-md-5" id="vl">
<div><label >Brand:</label>{{$vehicles->name}}</div>
<br>
<div><label >Model:</label>{{$vehicles->number}}</div>
<br>
If you have a relation in place on your Vehicle model which looks like this:
public function user()
{
return $this->belongsTo(User::class);
}
you can simply access the user property to get the email: {{ $vehicle->user->email }}
For improved performance, it makes sense to eager load the users when you fetch the vehicles from the database:
$vehicles = Vehicle::with('user')->get();
First take a look at this to create model for tables and then you can join it for both the tables and get user email too.
$vehicles = Vehicle::select('vehicles.name', 'vehicles.number', 'users.email')
->leftjoin('users', 'users.id', '=', 'vehicles.user_id')->get();
You can also use Laravel relations to achieve this.
$userEmails= DB::table('vehicles')
->join('contacts', 'user.id', '=', 'vehicles.user_id')
->select('users.email')
->get();
Try above code.
This is SQL join concept.
For your reference https://laravel.com/docs/5.7/queries#joins
I am working with Laravel 5.6 and MySQL. in My app I have some data edit form and its updated values saving with 3 different tables. called vehicles, cars and upload My edit form url as following,
http://localhost:8000/myads/1/edit
number (1) is vehicle table id number, like this,
id name owner
1 toyota dane
2 BMW peter
3 Benz samantha
4 volvo Alex
and car table is like this,
id fuel vehicle_id
1 P 1
2 D 3
3 E 2
I need update cars table data with regarding to vehicle_id , My VehicleController vehicle table data update function is like this
public function update(Request $request, $id)
{
$vehicle = Vehicle::find($id);
$vehicle->name = $request->input('name');
$vehicle->owner = $request->input('owner');
$vehicle->save();
}
now I need update cars table fuel values with same form optional values input. how can I do this in the Controller?
My fuel selection input is like this,
<div class="form-group">
<label for="exampleFormControlSelect1">Fuel</label>
<select class="form-control" id="fuel" name="fuel">
#foreach($vehicles->cars as $car)
<option value="{!! $car->fuel !!}">{!! $car->fuel !!}</option>
#endforeach
<option value="P">Petrol</option>
<option value="D">Diesel</option>
<option value="H">Hybrid</option>
<option value="E">Electric</option>
</select>
</div>
relationship among Vehicle and Car Models are
Vehicle Model
public function cars()
{
return $this->hasMany(Car::class);
}
Car Model
public function vehicle()
{
return $this->belongsTo(Vehicle::class);
}
How can do it?
Use a hidden Input field (load car id to the input using $car->id ) in form and name it as 'car_id'.
In the same update function,
$carId = $request->input('car_id');
$car= Car::find($carId);
//Update details here
$car->save();
Note : when you are working with multiple table, do not forget to use DB Transaction.
got success with following codes,
$vehicle->cars()->where('vehicle_id', $id)->update(['fuel' => $request->input('fuel')]);
The problem is that i have 3 database tables
1st table of users with user_id
2nd table is attribute with attribute_id and attribute_name
And 3rd one is Rating table having user_id and attribute_id
i want to get attribute_name from 2nd table using attribute_id after click on the specific user? Is it possible? If answer is Yes then please give me a suggestion how yo do this? thanks sir in advance?
Here i'm upload the image that after i click on specific user for edit display its record from user table and attribute_id from attribute table using many to many relation and problem is that we get attribute_name of specific id from Rating table using the attribute_id?? Is It Possible?
Its Edit form and below submit button get id from attribute table but we want to display attribute_name from rating table
Users model:
public function rating()
{
return $this->belongsTo(Rating::class);
}
Rating model:
public function attribute()
{
return $this->hasMany(Attribute::class, 'attribute_id');
}
Then call it in controller:
dd(Users::with('rating.attribute')->where('user_id', 1));
Query::
$records = DB::table('attributes')->join('ratings','attributes.id','=','ratings.attribute_id')->where('user_id',$id)->get();
return view('employee.edit',compact('records'));
Your EDIT VIEW PAGE
#foreach($records as $record)
<h4>Attributes ==>{{$record->attribute}}</h4>
#endforeach