Edit table with many to many relationship in Laravel - php

I have problems in updating a bridge table resulting from a many to many relationship, if I select a new course it adds it without problems but if I leave any course unchecked it does not delete it, how can I proceed?
This is the form
<div class="container">
<div class="row">
<div class="col-lg-12">
<form class="form-group" action="{{route('trainers.update', $trainer->id)}}" method="post" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="form-group">
<label for="name">Nome</label>
<input type="text" class="form-control" name="name" value="{{$trainer->name}}">
</div>
<div class="form-group">
<label for="surname">Cognome</label>
<input type="text" class="form-control" name="surname" value="{{$trainer->surname}}">
</div>
<div class="form-group">
<label for="description">Descrizione</label>
<textarea class="form-control" name="description" rows="8" cols="80">{!! $trainer->description !!}</textarea>
</div>
#foreach ($courses as $course)
<div class="form-check form-check-inline mb-2">
<input name="course_id[]" class="form-check-input" type="checkbox" value="{{$course->id}}">
<label class="form-check-label" for="course_id">{{$course->name_course}}</label>
</div>
#endforeach
<div class="form-group">
<img src="{{asset('storage/'.$trainer->image)}}" alt="">
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" name="image">
<label class="custom-file-label" for="image">Scegli un'immagine</label>
<div class="invalid-feedback"><strong>N.B.</strong> dimensione consigliata 160px x 160px</div>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="MODIFICA ISTRUTTORE">
</div>
</form>
</div>
</div>
</div>
This is the edit function in controller
public function edit($id)
{
$trainer = Trainer::find($id);
$courses = Course::all();
return view('trainers.edit', compact('trainer','courses'));
}
This is the update function in controller
public function update(Request $request, Trainer $trainer)
{
$data = $request->all();
$trainer->update($data);
$trainer->courses()->detach($data['course_id']);
$trainer->courses()->attach($data['course_id']);
return redirect()->route('trainers.admin');
}

try this
public function edit($id)
{
$trainer = Trainer::find($id);
$courses = Course::where('active', 1)->get(); //assume active is there in courses table (or any other column)
// $trainer->courses - retrive trainer courses
return view('trainers.edit', compact('trainer','courses'));
}
public function update(Request $request, Trainer $trainer)
{
$data = $request->all();
$trainer->update($data);
$trainer->courses()->sync($data['course_id']);
return redirect()->route('trainers.admin');
}
sync method accepts an array of IDs to place on the intermediate table.
For more information read: Syncing Associations
You can also refer Filtering Relationships Via Intermediate Table Columns
from above link

Related

Auto calculate total value laravel livewire

i have a problem where the value isn't calculated.
I have function where the user able to update Weight/size and quantity(if the input isn't in readonly). When the user enter a new weight/size it will be auto-calculated in the Order Total. Apparently, there is a value already in the Order Total (since it is an update) so i want it to auto update the value in the order total when the user update the new input in weight/size or quantity.
pls click this picture
here is my updates.blade.php
<div class="col-sm-12 col-md-6" >
<div class="card">
<div class="card-body">
<div class="form-actions">
<div class="container" style="padding: 0;margin: 0;">
<div class="row">
<div class="col-sm-6" >
<span class="float-sm-left">
<h3 class="card-title">Update Order</h3>
</span>
</div>
</div>
</div>
</div>
</br>
<form action="" method="POST" >
#csrf
#method('PUT')
<div class="form-row">
<div class="col-md-4 mb-3">
<label for="validationTooltip01">Order ID</label>
<input type="text" class="form-control" wire:model="orderID" value="" readonly>
</div>
<div class="col-md-4 mb-3">
<label for="validationTooltip02">Order Date</label>
<input type="text" class="form-control" wire:model="orderDate" value="" readonly>
</div>
<div class="col-md-4 mb-3">
<label for="validationTooltipUsername">Order Status</label>
<input type="text" class="form-control" wire:model="orderStatus" value="" readonly>
</div>
</div>
<div class="form-row">
<div class="col-md-6 mb-3">
<label class="form-control-label" >Customer Phone Number</label>
<input type="text" name="" value="" class="form-control" maxlength="11" wire:model="custPhone" readonly>
</div>
<div class="col-md-6 mb-3">
<label class="form-control-label" >Customer Name</label>
<input type="text" name="" value="" class="form-control" maxlength="11" wire:model="custName" readonly>
</div>
</div>
<div class="form-row">
#foreach($serviceOrder as $o)
<div class="col-md-5 mb-3">
<label class="form-control-label" >Service {{ $loop->iteration }} </label>
<input type="text" wire:model="serviceOrder.{{ $loop->index }}.serv.serviceName" class="form-control " readonly>
</div>
<input type="hidden" wire:model="serviceOrder.{{ $loop->index }}.serv.servicePrice" class="form-control col-sm-1 mb-3" readonly>
<div class="col-md-3 mb-3">
<label class="form-control-label" >Weight/Size</label>
<input type="text" name="" value="" wire:model="serviceOrder.{{ $loop->index }}.weightsize" class="form-control">
</div>
<div class="col-md-3 mb-3">
<label class="form-control-label" >Quantity*opt</label>
<input type="text" name="" value="" wire:model="serviceOrder.{{ $loop->index }}.quantity" class="form-control" #if(!$serviceOrder[$loop->index]['quantity']) readonly #endif >
</div>
#endforeach
</div>
<label class="form-control-label" >Order Total RM</label>
<input type="number" wire:model="orderTotal" value="" class="form-control" readonly>
</br>
<div class="form-actions">
<div class="container" style="padding: 0;margin: 0;">
<div class="row">
<div class="col-sm-6" >
<span class="float-sm-left">
<a class="btn btn-primary" href="{{ route('orders.indexInProcess') }}"> Back</a>
</span>
</div>
<div class="col-sm-6">
<span class="float-sm-right">
<div class="text-right">
<button type="submit" class="btn btn-info mr-2">Update</button>
<button type="reset" class="btn btn-dark float-right">Reset</button>
</div>
</span>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
here is the livewire updates.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Customer;
use App\Service;
use App\Order;
use App\ServiceOrder;
class Updates extends Component
{
public $orders;
public $orderID;
public $orderDate;
public $orderStatus;
public $orderTotal;
public $custName;
public $custPhone;
public $serviceName;
public $servicePrice;
public $weightsize;
public $quantity;
public $serviceOrder;
public function mount($order,$so)
{
//$orders = Order::with('customer')->get();
//ServiceOrder::with('serv')->where('order_id', $order->id)->get();
$this->orderID = $order->id;
$this->orderDate = $order->orderDate;
$this->orderStatus = $order->orderStatus;
$this->orderTotal = $order->orderTotal;
$this->custPhone = $order->customer->custPhone;
$this->custName = $order->customer->custName;
$this->serviceOrder = $so->toArray();
//dd($this->serviceOrder);
//$so->serviceName,
//$so->weightsize,
//$so->quantity
}
public function render()
{
//$so = ServiceOrder::with('serv')->where('order_id', $order->id)->get();
return view('livewire.updates');
}
public function updatedInputs($name)
{
$array = explode('.', $name);
if ($array[1] == 'serviceName') {
$this->inputs[$array[0]]['servicePrice'] = $this->services->find($value)->servicePrice;
}
try {
$this->calculateTotal();
} catch (\Exception $e) {
}
}
// perform calculation here.
public function calculateTotal(){
$this->orderTotal = $this->orderTotal;
foreach ($this->serviceOrder as $item) {
if($item['quantity'] == ''){
$item['optQuantity']= 1;
$this->orderTotal += ($item['servicePrice'] * $item['weightsize']) * ($item['quantity']);
}else{
$this->orderTotal += $item['servicePrice'] * ($item['weightsize']) * ($item['quantity']);
}
//$this->total *= $item['optQuantity']; // * price;
}
}
}
please help me out here T.T
You can fire event from component (for example) and then register listener in class and call a method for updating total. Check documentation for that.
"Apparently, there is a value already in the Order Total (since it is an update) so i want it to auto update the value in the order total when the user update the new input in weight/size or quantity."
So, the problem is $orderTotal only update once? it doesn't update when user update new input?. Livewire should render everytime there is action or any input.
First of all, i advice u to check what happen when to your function when user update the new input with dd function.
public function calculateTotal(){
dd('is this func work?');
$this->orderTotal = $this->orderTotal;
foreach ($this->serviceOrder as $item) {
if($item['quantity'] == ''){
$item['optQuantity']= 1;
$this->orderTotal += ($item['servicePrice'] * $item['weightsize']) * ($item['quantity']);
}else{
$this->orderTotal += $item['servicePrice'] * ($item['weightsize']) * ($item['quantity']);
}
//$this->total *= $item['optQuantity']; // * price;
}
}
After you debug your function, maybe you already found the answer you looking for or you can come back and ask me anything. ^^

I want to update values in two tables

I created an edit form to update values in my database and then show it on the main page. The problem is that it doesn't save the data to DB.
the request passes: Status Code: 302 Found
with all the values that I want to change (for example adding phone and mail):
effective_date_practitioner: 2019-01-01
expiry_date_practitioner:
phone_number_practitioner: 918273645
mobile_number_practitioner:
email_practitioner: test#test.pl
practitioner_specialty_id_update: 1
effective_date_specialty: 2019-01-01
expiry_date_specialty:
Edit blade
<div class="edit-practitioner" style="display: none;">
<form style="box-shadow: none;" action="/practitioner/update/{{$practitioner->practitioner_id}}" method="post"
class="j-pro" id="update-practitioner-form">
#csrf
<div class="j-content">
<div id="j-row-id" class="j-row">
<div class="row">
<div class="col-sm-12 col-lg-12 col-xl-5">
<div class=" j-unit">
<div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
<span>Practitioner Information</span>
</div>
<label class="j-label">{{trans('personalData.effectiveDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner->effective_date}}"
name="effective_date_practitioner">
</div>
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner->expiry_date}}"
name="expiry_date_practitioner">
</div>
<label class="j-label">{{trans('personalData.phoneNumber')}}</label>
<div class="j-input">
</label>
<input type="tel" value="{{$practitioner->phone}}"
name="phone_number_practitioner">
</div>
<label class="j-label">{{trans('personalData.mobileNumber')}}</label>
<div class="j-input">
<input type="tel" value="{{$practitioner->mobile}}"
name="mobile_number_practitioner">
</div>
<label class="j-label">{{trans('personalData.email')}}</label>
<div class="j-input">
<input type="email" value="{{$practitioner->email}}"
name="email_practitioner">
</div>
</div>
</div>
<div class="col-xl-1 j-unit"></div>
<div class="col-sm-12 col-lg-12 col-xl-6">
<div class="j-divider-text j-gap-top-20 j-gap-bottom-45">
<span>{{trans('settings.specialty')}}</span>
</div>
<select name="practitioner_specialty_id_update"
id="practitioner_specialty_id_update"
class="form-control-practitioner required">
#foreach($specialties as $specialty)
<option
value="{{$specialty->specialty_id}}">{{$specialty->name}}</option>
#endforeach
</select>
<label class="j-label">{{trans('personalData.effectiveDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner_specialty->effective_date}}"
name="effective_date_specialty">
</div>
<label class="j-label">{{trans('personalData.expiryDate')}}</label>
<div class="j-input">
<input type="date" value="{{$practitioner_specialty->expiry_date}}"
name="expiry_date_specialty">
</div>
</div>
</div>
</div>
<div class="j-divider j-gap-bottom-45 j-gap-top-10"></div>
<button type="submit"
class="btn btn-editpanel btn-success btn-round">Save changes
</button>
<!-- end /.footer -->
<button id="update-cancel-button-practitioner" type="button"
class="btn btn-editpanel btn-danger btn-round">Cancel
</button>
</div>
</form>
</div>
web.php
Route::post('/practitioner/update/{id}', 'Crud\Settings\PractitionerController#updatePractitioner')->name('updatePractitioner');
Controller:
<?php
namespace App\Http\Controllers\CRUD\Settings;
use App\Models\Practitioner;
use App\Models\PractitionerCompetenceLevel;
use App\Models\PractitionerSpecialty;
use App\Repositories\PractitionerRepository;
use Illuminate\Http\Request;
class PractitionerController extends CrudController
{
protected $repository;
public function __construct(PractitionerRepository $repository)
{
$this->middleware('auth');
$this->repository = $repository;
}
public function updatePractitioner($id, Request $request)
{
$this->validate($request, [
'effective_date_practitioner' => 'required',
'effective_date_specialty' => 'required',
]
);
$input = $request->all();
$input['data'][$this->repository->getIdName()] = $id;
$this->repository->update($input['data']);
return back()->with('successUpdate', 'Practitioner has been updated!');
}
}
My guess is that the data I want to update belongs to two different tables in DB one called practitioner and the other one called practitioner_specialty
As per your code you are trying to do mass assignment to your model
You may do this using the $fillable property on the model
protected $fillable = ['effective_date_practitioner','effective_date_specialty',......];
And you can use attach() method to updated data in related tables

How can i get data from intermediate table (many to many relationship) in laravel

I would like to read the contents of the intermediate table between the trainers and courses table to be able to flag the chackboxes of the active courses for a given trainer, how can I do?
Here you can look the trainers table
http://prntscr.com/nhcrl4
Here you can look the courses table
http://prntscr.com/nhcrvp
Here you can look the course_trainer (intermediate) table
http://prntscr.com/nhcsek
I thought of using the ternary, you advise me?
thanks a lot
this is the form
<div class="container">
<div class="row">
<div class="col-lg-12">
<form class="form-group" action="{{route('trainers.update', $trainer->id)}}" method="post" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="form-group">
<label for="name">Nome</label>
<input type="text" class="form-control" name="name" value="{{$trainer->name}}">
</div>
<div class="form-group">
<label for="surname">Cognome</label>
<input type="text" class="form-control" name="surname" value="{{$trainer->surname}}">
</div>
<div class="form-group">
<label for="description">Descrizione</label>
<textarea class="form-control" name="description" rows="8" cols="80">{!! $trainer->description !!}</textarea>
</div>
#foreach ($courses as $course)
<div class="form-check form-check-inline mb-2">
<input name="course_id[]" class="form-check-input" type="checkbox" value="{{$course->id}}">
<label class="form-check-label" for="course_id">{{$course->name_course}}</label>
</div>
#endforeach
<div class="form-group">
<img src="{{asset('storage/'.$trainer->image)}}" alt="">
</div>
<div class="custom-file">
<input type="file" class="custom-file-input" name="image">
<label class="custom-file-label" for="image">Scegli un'immagine</label>
<div class="invalid-feedback"><strong>N.B.</strong> dimensione consigliata 160px x 160px</div>
</div>
<div class="form-group">
<input type="submit" class="form-control" value="MODIFICA ISTRUTTORE">
</div>
</form>
</div>
</div>
</div>
this is the edit function in controller
public function edit($id)
{
$trainer = Trainer::find($id);
$courses = Course::all();
return view('trainers.edit', compact('trainer','courses'));
}
this is the update function in controller
public function update(Request $request, Trainer $trainer)
{
$data = $request->all();
$trainer->update($data);
$trainer->courses()->sync($data['course_id']);
return redirect()->route('trainers.admin');
}
This is Trainer Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Trainer extends Model
{
protected $fillable = ['name','surname','description','image'];
public function courses(){
return $this->belongsToMany(Course::class)->withTimestamps();
}
}
This is Course Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Course extends Model
{
protected $fillable = ['name_course','description_course','logo_course'];
public function trainers(){
return $this->belongsToMany(Trainer::class)->withTimestamps();
}
}
This is Trainer Model
public function courses(){
return $this->belongsToMany('App\Course', 'course_trainer ', 'trainer_id', 'course_id')->withTimestamps();
}
This is Course Model
public function trainers(){
return $this->belongsToMany('App\Trainer', 'course_trainer ', 'course_id', 'trainer_id')->withTimestamps();
}
belongsToMany() method 4 parameters, the first one is the location of your model to link, the second is the name of the pivot table, the third one is the current model foreign key and the fourth one is the other's model foreign key.
now you can do something like this in the form
<input name="course_id[]" class="form-check-input" type="checkbox" #if($course->pivot->trainer_id == $trainer->id) "checked='' " #endif value="{{$course->id}}">
I solved the problem without changing the model ... it was enough to do this
#foreach ($courses as $course)
<div class="form-check form-check-inline mb-2">
<input name="course_id[]" class="form-check-input" type="checkbox" value="{{$course->id}}" #foreach ($trainer->courses as $value)
{{($course->id == $value->pivot->course_id) ? 'checked' : ''}}
#endforeach>
<label class="form-check-label" for="course_id">{{$course->name_course}}</label>
</div>
#endforeach

How to edit one column in a row of the database in laravel

How to edit one column in a row of the database in laravel
I can't update one column of row has multiple columns by laravel
My edit :
public function edit($id)
{
$addremark=bookappoitment::findOrFail($id);
return view('admin.ManageTime.addremarks', compact('addremark'));
}
My update:
public function update(Request $request, $id)
{
$request->validate([
'Remarks'=>'required'
]);
$data=bookappoitment::find($id);
$data->Remarks = $request->get('Remarks');
$data->save();
return view('/home');
}
link to update:
Update
form:
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label>Remarks :</label>
<textarea class="form-control" rows="10" name="Remarks" placeholder="Remarks">{{ $addremark->Remarks }}</textarea>
</div>
<a class="btn btn-success btn-mini deleteRecord " href="{{route('BookAppoint.update',$addremark->id)}}">Update</a>
</div>
</div>
</div>
You can update 1 (or more, just add to the array) column through the DB class.
DB::table('yourTable')->where('id', $id)->update(['remarks' => $request->input('Remarks')]);
You can also do it for a model like so:
bookappoitment::where('id', $id)->update(['remarks' => $request->input('Remarks')]);
It's in the Laravel documentation here.
<form class="" action="index.html" method="{{ route('BookAppoint.update', $addremark->id) }}">
#method('PATCH')
#csrf
<div class="panel-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="Remarks">Remarks :</label>
<textarea name="remarks" rows="10" placeholder="Remarks">{{$addremarks->Remarks}}</textarea>
</div>
</div>
<input type="submit" name="submit" value="Update">
</div>
</div>
</form>

Laravel - Edit product with polymorphic relation

I have a system where to user needs to be able to change a product. But the problem is, This product has a polymoprhic relation with theme. This is the build-up
Products
- ID
- productable_id (For instance "1")
- productable_type (Is either "App\Theme"or "App\Plugin")
- name (Just a name)
- description (Just a description)
- etc
Theme
- id (1)
- composer_package
Now I need the user to be able to change a product. Currently, I have it done like this in the controller
public function update(Request $request, $id)
{
$product = Product::find($id);
$product->fill($request->all());
$product->save();
return redirect('/products')->with('flash', $product->name . ' is succesvol bewerkt');
}
But since its a polymorphic relation I don't know how to change for example the "composer_package" that is associated with the selected product. Say, for instance, I want to change the product with the ID of 1. Then I want to also be able to change the composer_package with the id 1 because that one is associated with that selected product. Also, How can I change for instance, the type of product. The selected product is an App\Theme and I want to change that to App\Plugin. How can I achieve the above two things with this polymorphic relation
This is the form that is collecting the data. It's quite large
<div class="modal fade-scale" id="createProduct" tabindex="-1" role="dialog" aria-labelledby="IetsAnders" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="m-0">Product aanmaken</h4>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<form method="POST" action="{{ route('addProduct') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="name">Naam</label>
<input type="text" name="name" class="form-control" placeholder="Naam">
</div>
<div class="form-group">
<label for="description">Omschrijving</label>
<textarea name="description" id="" class="form-control" cols="30" rows="5"></textarea>
</div>
<div class="form-group">
<label for="productable_type">Product type</label>
<select name="type" id="productable_type" class="form-control">
<option selected>Kies een type...</option>
</select>
</div>
<div class="form-group">
<label for="price">Prijs</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="currency" style="background-color: white; border-right: none">€</span>
</div>
<input type="number" step="0.01" name="price" class="form-control" value="" aria-describedby="currency" style="border-left: none" placeholder="00.00">
</div>
</div>
<hr>
<div class="form-group">
<label for="period_id">Betalings periode</label>
<select name="period_id" id="" class="form-control">
<option selected>Kies een periode</option>
#foreach($plans as $plan)
<option>{{ $plan->name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="periodically_price">Prijs per gekozen periode</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text" id="currency" style="background-color: white; border-right: none">€</span>
</div>
<input type="text" name="periodically_price" class="form-control" value="" aria-describedby="currency" style="border-left: none" placeholder="00.00">
</div>
</div>
<div class="form-group mb-3">
<label for="thumbnail">Foto van product</label>
<input type="file" name="thumbnail" class="form-control">
</div>
<div class="form-group mb-3">
<label for="composer_package">Composer package</label>
<input type="text" name="composer_package" class="form-control" placeholder="Composer package">
</div>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Sluiten</button>
<button type="submit" class="btn btn-orange">Bewerken</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
Thanks in advance!
EDIT
Product model
class Product extends Model
{
protected $fillable = [
'productable_type', 'productable_id', 'name', 'description', 'thumbnail', 'price', 'periodically_price', 'period_id'
];
public function productable()
{
return $this->morphTo();
}
public function order_items()
{
return $this->hasMany(Orderitems::class);
}
public function plan() {
return $this->belongsTo(Plan::class, 'period_id');
}
}
Theme Model
class Theme extends Model
{
protected $fillable = [
];
public function webshops()
{
return $this->hasMany(Webshop::class);
}
public function products()
{
return $this->morphMany(Product::class, 'productable');
}
}
Add composer_package to Theme::$fillable.
Adjust the HTML form: name="productable[composer_package]"
Then you can update the theme like this:
$product->productable->update($request->get('productable'))

Categories