I am pretty new in Laravel,
actually I am trying to create a crud operation using Laravel 5.6, so I have created create, delete function successfully, but on update function I am getting an error
Please find the attched image for detailed error
Use of undefined constant title - assumed 'title' (this will throw an Error in a future version of PHP)
Controller
public function edit($id){
$blogCategories = BlogCategories::find($id);
if (empty($blogCategories)) {
Flash::error('Category not found');
return redirect(route('categories.index'));
}
return view('cms/BlogCategories/editCategory')->with('blogCategories', $blogCategories);
}
public function update(Request $request, $id){
$blogCategories = BlogCategories::find($id);
$blogCategories->title = $request->get(title);
$blogCategories->slug = $request->get(slug);
$blogCategories->description = $request->get(description);
$blogCategories->featured_image = $request->get(featured_image);
$blogCategories->save();
return redirect()->back();
}
Model
class BlogCategories extends Model{
protected $fillable = ['title', 'slug', 'description', 'featured_image'];
protected $guarded = [];
}
Form
<form action="{{route('categories.update', $blogCategories->id)}}" method="post" class="m-form m-form--fit m-form--label-align-right">
#csrf
#method('put')
<div class="m-portlet__body">
<div class="form-group m-form__group">
<label>Title</label>
<input type="text" class="form-control m-input" name="title" id="title" value="{{$blogCategories->title}}" aria-describedby="emailHelp" placeholder="Muhammad Owais">
</div>
<div class="form-group m-form__group">
<label>slug</label>
<input type="text" class="form-control m-input" name="slug" id="slug" value="{{$blogCategories->slug}}" aria-describedby="emailHelp" placeholder="mail#domain.com">
</div>
<div class="form-group m-form__group">
<label>Description</label>
<textarea class="form-control" name="description" id="description" value="{{$blogCategories->description}}" placeholder="Enter Description"></textarea>
</div>
<div class="form-group m-form__group">
<label>Featured Image</label>
<input type="text" class="form-control m-input" name="featured_image" id="featured_image" value="{{$blogCategories->featured_image}}" aria-describedby="emailHelp" placeholder="Enter Amazon S3 URL">
</div>
</div>
<div class="m-portlet__foot m-portlet__foot--fit">
<div class="m-form__actions">
<button type="submit" class="btn btn-primary">
Submit
</button>
<button type="reset" class="btn btn-secondary">
Cancel
</button>
</div>
</div>
</form>
Use quotes for your all HTTP requests like :
$request->get('title');
$request->get('slug');
Related
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'm trying to update my data with Laravel. I'm able to create, read, delete the data but somehow i cannot update my data. I already checked my controller,model,route and view but i don't think there's any typo or anything. It only redirects to it's index page without being updated although i have entered new input. There's no error message at all so i checked where is the problem. So i checked my update function in my controller and tried to show the request by echo "$request->kode_kontak"; and echo $request->kode_kontak; but it shows nothing which i assume that it's null/empty but when i echo "yes" it showed on the screen "yes" i tested this because i want to know if the function itself is working so the problem here is that the request contains null, no wonder i cannot update it. Why is the request isn't passed? why is it like this? and how to fix it?
Route for edit and update
Route::get('contact/{contact}/edit', 'ContactController#edit')->name('contact.edit');
Route::patch('contact/{contact}','ContactController#update')->name('contact.update');
Controller with edit and update function
use Illuminate\Http\Request;
use App\Contact;
use DB;
public function edit($kode_kontak){
$contact = DB::table('contact')->where('kode_kontak',$kode_kontak)->get();
return view('contact.edit',['contact' => $contact]);
}
public function update(Request $request){
DB::table('contact')->where('kode_kontak',$request->kode_kontak)->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
Model
class Contact extends Model
{
public $timestamps = false;
protected $table = 'contact';
protected $fillable = [
'kode_kontak',
'kode_pegawai',
'email',
'telepon'
];
protected $primaryKey = 'kode_kontak';
}
View of edit.blade.php
<div id="contact">
<h2>Edit Contact</h2>
#foreach($contact as $p)
<form action="{{ route('contact.update', ['kode_pegawai' => $p->kode_pegawai]) }}" method="POST">
#csrf
#method('patch')
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" name="kode_kontak" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" name="kode_pegawai" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="text" name="email" id="email" class="form-control" value="{{ $p->email}}">
</div>
<div class="form-group">
<label for="telepon" class="control-label">Telepon</label>
<input type="text" name="telepon" id="telepon" class="form-control" value="{{ $p->telepon}}">
</div>
<div class="form-group">
<input class="btn btn-primary form-control" type="submit" value="Simpan">
</div>
</form>
#endforeach
</div>
Your issue is that you have disabled those inputs. Disabled inputs will not be submitted.
If you want to display the disabled inputs, but still PATCH the values, you will need to add hidden inputs with those values like:
<div class="form-group">
<label for="kode_contact" class="control-label">Kode Kontak</label>
<input type="text" id="kode_kontak" class="form-control" value="{{ $p->kode_kontak}}" disabled>
<input type="hidden" name="kode_kontak" value="{{ $p->kode_kontak}}">
</div>
<div class="form-group">
<label for="kode_pegawai" class="control-label">Kode Pegawai</label>
<input type="text" id="kode_pegawai" class="form-control" value="{{ $p->kode_pegawai}}" disabled>
<input type="hidden" name="kode_pegawai" value="{{ $p->kode_pegawai}}">
</div>
Hope that helps!
$request->kode_kontak is $contact here, $request->kode_kontak is not available in $request, change $contact instead :
public function update(Request $request, $contact){
DB::table('contact')->where('kode_kontak',$contact)->update([
'email' => $request->email,
'telepon' => $request->telepon,
]);
return redirect('contact');
}
I am a newbie in Laravel.
My problem is, when I tried to update my form, it kept saying that the tablename not found eventhough I already mentioned it inside my Model. But when I debugged, I found that the request is not the same from what I put inside my form.
But my form actually doesn't have that.
Any idea how this is happening, guys? Pretty sure I missed something but unsure what is it.
Event Model
class Event extends Model
{
//
protected $fillable = ['title', 'objective', 'date', 'venue', 'description', 'slug'];
public function getRouteKeyName()
{
return 'slug';
}
}
Event Controller
class EventController extends Controller
{
public function update(Request $request, Event $event)
{
//
$validated = $request->validate([
'title' => 'required|string|unique:event|min:5|max:100',
'objective' => 'required|string|min:5|max:2000',
'date' => 'required|string|min:5|max:2000',
'venue' => 'required|string|min:5|max:2000',
'description' => 'required|string|min:5|max:2000'
]);
// Create slug from title
$validated['slug'] = Str::slug($validated['title'], '-');
// Update Post with validated data
$event->update($validated);
// Redirect the user to the created post woth an updated notification
return redirect(route('events.edit', [$event->slug]))->with('notification', 'Event updated!');
}
Edit Blade Page
<form method="post" action="{{ route('events.update', [$event->slug]) }}">
#csrf
#method('patch')
#include('partials.errors')
<div class="field">
<label class="label">Title</label>
<div class="control">
<input type="text" name="title" value="{{ $event->title }}" class="input" placeholder="Title" minlength="5" maxlength="100" required />
</div>
</div>
<div class="field">
<label class="label">Objective</label>
<div class="control">
<textarea name="content" class="textarea" placeholder="Content" minlength="5" maxlength="2000" required rows="10">{{ $event->objective }}</textarea>
</div>
</div>
<div class="field">
<label class="label">Date</label>
<div class="control">
<input type="text" name="title" value="{{ $event->date }}" class="input" placeholder="Title" minlength="5" maxlength="100" required />
</div>
</div>
<div class="field">
<label class="label">Venue</label>
<div class="control">
<input type="text" name="title" value="{{ $event->venue }}" class="input" placeholder="Title" minlength="5" maxlength="100" required />
</div>
</div>
<div class="field">
<label class="label">Description</label>
<div class="control">
<textarea name="content" class="textarea" placeholder="Content" minlength="5" maxlength="2000" required rows="10">{{ $event->description }}</textarea>
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-link is-outlined">Update</button>
</div>
</div>
</form>
Thank you for your time!
You need to change the name attribute of Date, Venue and Description and Objective.
On your edit blade page, the input names are alternating 'title' and 'content'
can you rename the input names so that they can be unique
Controllers/HomeController.php
public function edit(Task $task)
{
return view('edit', compact('task'));
}
public function update(Request $request, Task $task)
{
$request->validate(['title' => 'required|min:3', 'description' => 'required', ]);
$task->title = $request->title;
$task->description = $request->description;
$task->save();
$request->session()
->flash('message', 'Successfully modified the task!');
return redirect('viewalltask');
}
routes/web.php
Route::post('/{task}/', 'HomeController#update')->name('update');
views/edit.blade.php
<form action="{{url('', [$task->id])}}" method="POST">
<input type="hidden" name="_method" value="PUT">
{{ csrf_field() }}
<div class="row">
<div class="col-md-3" >
<label for="title" >title</label>
<input id="title" type="text" class="form-control" name="title" value="{{$task->title}}" required autofocus>
</div>
<div class="col-md-3">
<label for="description" >description</label>
<input id="description" type="text" class="form-control" name="description" value="{{$task->description}}" required>
</div>
</div>
<br>
<div class="row">
<div class="col-md-12">
<button type="submit" class="btn btn-primary" >
Edit
</button>
</div>
</div>
</form>
Error :
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
In your routes file the you have declared the wrong method . it should be like this.
Route::put('/{task}', 'HomeController#update')->name('update');
You are using wrong method type in routes as compare to method type in form,
Route::post('/{task}/', 'HomeController#update')->name('update');
Should work.
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