Reputation: 87
In my project ı have three tables for eloquent but i cant figure out how to connect each other.
First table : Cars Second Table : Car Property Category Third Table : Car Properties
Mechanism is that,
Car property categories has many car properties. There is no problem in here.
But Car table has car property category and car properties. So when i try to eloquent with Car property category and properties i got sync error.
I tried with many to many but cant success full.
I created another table for car_id, car_property_category_id and property_id. But in view side there on form submit, car property category and properties are going to controller as array. So i want to sync them as category and property. AS you can see below picture when property checked property category must add to table with property name
My codes are below;
Car Model:
public function properties()
{
return $this->belongsToMany(CarProperty::class,'car_car_properties');
}
Car Property Model
public function category()
{
return $this->belongsTo('App\CarPropertyCategory','category_id','id');
}
public function car()
{
return $this->belongsToMany(Car::class, 'car_car_properties');
}
Car Property Category Model
public function ozellikleri(){
return $this->hasMany('App\CarProperty','category_id','id');
}
My Blade File
@if($propertycategories->count())
@foreach($propertycategoriesas $category)
<div class="form-group">
<label class="control-label">{{$category->name}}</label>
<div class="row">
<input type="hidden" name="categoryname[]" value="{{$category->id}}">
@foreach($category->properties as $property)
<div class="col-md-2 mg-t-20 mg-lg-t-0">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="property{{$property->id}}" name="properties[]" value="{{$property->id}}">
<label class="custom-control-label" for="property{{$property->id}}">{{$property->name}}</label>
</div>
</div>
@endforeach
</div>
</div>
@endforeach
@endif
Controller side Sync Codes
$properties = $request->input('properties ');
$categories= $request->input('categories');
$car = Car::find($id);
$car->properties()->sync([$properties,$categories]);
My pivot table has 3 columns like below;
car_id car_property_id car_property_category_id
But i cant sync categories in this table. Only car_id and property_id can sync
I want that all categories and properties must sync same time.
NOT: I figured it out with array save
If somone needs codes below;
i figured it out. İf some needs;
$data = [];
foreach($request->properties as $property){
$add = [];
$find= CarProperty::where('id',$property)->first();
$add ['car_property_id'] = $find->id;
$add ['car_property_Category_id'] = $find->category->id;
$data[] = $add ;
}
$arac->properties()->sync($data );
Upvotes: 1
Views: 1794
Reputation: 989
I figured it out:
$data = [];
foreach($request->properties as $property) {
$add = [];
$find = CarProperty::where('id', $property)->first();
$add['car_property_id'] = $find->id;
$add['car_property_Category_id'] = $find->category->id;
$data[] = $add ;
}
$arac->properties()->sync($data);
Upvotes: 1