Laravel 5.2 cannot update record - laravel

I cannot seem to update my record.
My controller
public function add()
{
return view('cars.add');
}
public function edit($id)
{
$car = Cars::whereId($id)->firstOrFail();
return view('cars.edit', compact('car'));
}
public function store(CarFormRequest $request)
{
$car = new Cars(array(
'name' => $request->get('name'),
'color_id' => $request->get('color')
));
$car->save();
$car->position_id = $car->id;
$car->save();
session()->flash('status', 'Successfully Added a Car!');
return view('cars.add');
}
public function update($id, CarFormRequest $request)
{
$car = car::whereId($id)->firstOrFail();
$car->name = $request->get('name');
$car->color_id = $request->get('color');
if($request->get('status') != null) {
$car->status = 0;
} else {
$car->status = 1;
}
$car->save();
return redirect(action('CarController#edit', $car->id))->with('status', 'The ticket '.$id.' has been updated!');
}
my routes:
Route::get('/', 'PagesController#home');
Route::get('/about', 'PagesController#about');
Route::get('/contact', 'PagesController#contact');
Route::get('/cars', 'CarsController#index');
Route::get('/cars/edit/{id?}', 'CarsController#edit');
Route::post('/cars/edit/{id?}', 'CarsController#update');
Route::get('/cars/add', 'CarsController#add');
Route::post('/cars/add', 'CarsController#store');
here is my view:
<div class="container col-md-8 col-md-offset-2">
<div class="well well bs-component">
<form class="form-horizontal" method="post">
<input type="hidden" name="_token" value="{!! csrf_token() !!}">
<input type="text" id="color_id" name="color_id" value="{!! $car->color_id !!}">
<fieldset>
<legend>Edit Car Information</legend>
<div class="form-group">
<label for="title" class="col-lg-2 control-label">Car Name</label>
<div class="col-lg-10">
<input type="text" value="{{ $car->name }}" class="form-control" id="name" placeholder="Car Name">
</div>
</div>
<div class="form-group">
<label for="title" class="col-lg-2 control-label">Car Color</label>
<div class="col-lg-10">
<div class="btn-group" data-toggle="buttons">
<label id="opt1" class="btn btn-primary">
<input type="radio" name="color" id="option1" autocomplete="off"> Red
</label>
<label id="opt2" class="btn btn-primary">
<input type="radio" name="color" id="option2" autocomplete="off"> Blue
</label>
<label id="opt3" class="btn btn-primary">
<input type="radio" name="color" id="option3" autocomplete="off"> Yellow
</label>
<label id="opt4" class="btn btn-primary">
<input type="radio" name="color" id="option4" autocomplete="off"> Green
</label>
<label id="opt5" class="btn btn-primary">
<input type="radio" name="color" id="option5" autocomplete="off"> Black
</label>
<label id="opt6" class="btn btn-primary">
<input type="radio" name="color" id="option6" autocomplete="off"> White
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<button class="btn btn-default">Cancel</button>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>

The $id variable in whereIn must be array and you need to specify the database column too. This should be like -
public function edit($id)
{
$car = Cars::whereId('id', [$id])->firstOrFail();
return view('cars.edit', compact('car'));
}
Change all occurrence of
$car = car::whereId($id)->firstOrFail();

Related

Once the password has been reset, I cannot log into my application

I'm following the documentation on this link about resetting the password:
Reset Password
So first I create the view containing a form just to request the email and once the email has been received I click on the button to reset the password.
So far everything ok! Once I reset the password I try to log into my app with the new password but I cannot log in with this new password.
Can anyone kindly tell me where the problem lies? Thank you all
Route:
Route::get('/forgot-password', [Controller::class,'passwordRequest'])->middleware('guest')->name('password.request');
Route::post('/forgot-password', [Controller::class,'passwordEmail'])->middleware('guest')->name('password.email');
Route::get('/reset-password/{token}', [Controller::class,'passwordReset'])->middleware('guest')->name('password.reset');
Route::post('/reset-password', [Controller::class,'passwordUpdate'])->middleware('guest')->name('password.update');
Controller:
public function passwordRequest() {
return view('auth.forgot-password');
}
public function passwordEmail(Request $request) {
$request->validate(['email' => 'required|email']);
$status = Password::sendResetLink(
$request->only('email')
);
return $status === Password::RESET_LINK_SENT
? back()->with(['status' => __($status)])
: back()->withErrors(['email' => __($status)]);
}
public function passwordReset($token) {
return view('auth.reset-password', ['token' => $token]);
}
public function passwordUpdate(Request $request) {
$request->validate([
'token' => 'required',
'email' => 'required|email',
'password' => 'required|min:8|confirmed',
]);
$status = Password::reset(
$request->only('email', 'password', 'password_confirmation', 'token'),
function ($user, $password) {
$user->forceFill([
'password' => Hash::make($password)
])->setRememberToken(Str::random(60));
$user->save();
event(new PasswordReset($user));
}
);
return $status === Password::PASSWORD_RESET
? redirect()->route('login')->with('status', __($status))
: back()->withErrors(['email' => [__($status)]]);
}
View:
ForgotPassword
<div class="row mt-3 mx-5">
<div class="col-12 col-md-6 namelabel">
<form action="{{route('password.email')}}" method="post">
#csrf
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email">
</div>
<button type="submit" class="btn btn-primary mb-5">Invia</button>
</form>
</div>
</div>
ResetPassword
<div class="row mt-3 mx-5">
<div class="col-12 col-md-6 namelabel">
<form action="{{route('password.email')}}" method="post">
#csrf
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
</div>
<div class="mb-3">
<label for="password_confirmation" class="form-label">Conferma password</label>
<input type="password" class="form-control" name="password_confirmation">
</div>
<div class="mb-3">
<input type="hidden" class="form-control" name="token" value="{{$token}}" >
</div>
<button type="submit" class="btn btn-primary mb-5">Invia</button>
</form>
</div>
</div>
You're almost done! In your auth.reset-password view, you must send the request to the password.update route, not the password.email route.
The password.update route will run the passwordUpdate method to update the User's password.
https://laravel.com/docs/9.x/passwords#password-reset-handling-the-form-submission
<div class="row mt-3 mx-5">
<div class="col-12 col-md-6 namelabel">
<form action="{{ route('password.update') }}" method="post">
#csrf
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" name="email">
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" name="password">
</div>
<div class="mb-3">
<label for="password_confirmation" class="form-label">Conferma password</label>
<input type="password" class="form-control" name="password_confirmation">
</div>
<div class="mb-3">
<input type="hidden" class="form-control" name="token" value="{{$token}}" >
</div>
<button type="submit" class="btn btn-primary mb-5">Invia</button>
</form>
</div>
</div>

Insert data from modal, Codeigniter

I'm trying to insert the input data from a modal but I don't know how to achieve this. I'm not really familiar with AJAX(I apologize for that). I did some readings and research but I'm getting confused. Here is my modal content reserve_form.php
<form action="" id="book-form">
<div class="row flex-lg-row-reverse align-items-center pt-1">
<h3>Reservation Details</h3>
<div class="col-10 col-sm-8 col-lg-6">
<input type="hidden" name="package_id" value="<?php ?>">
<div class="form-group">
<label for="date_start" class="control-label">Reserve Date:</label>
<input type="date" name="date_start" id="date_start" class="form-control form-conrtrol-sm rounded-0" min="<?php echo date("Y-m-d") ?>" value="<?php echo date("Y-m-d") ?>" required>
</div>
<div id="msg" class="text-danger"></div>
<div id="check-availability-loader" class="d-none">
<center>
<div class="d-flex align-items-center col-md-6">
<strong>Checking Availability...</strong>
<div class="spinner-border ml-auto" role="status" aria-hidden="true"></div>
</div>
</center>
</div>
<div class="form-group">
<label for="quantity" class="control-label">Number of Person <span><i>(Max. of 15 person)</i></span></label>
<input type="number" class="form-control form" required name="person" value="">
</div>
<div class="form-group">
<label for="amount" class="control-label">Additional Inclusions</label>
<div>
<input type="checkbox" id="inclusion" name="inclusion" value="">
<label for="inclusion"> Unlimited Samgyupsal & Al Fresco dining experience</label>
</div>
</div>
<div class="form-group">
<label for="amount" class="control-label">Total Amount</label>
<input type="number" name="cost" id="amount" class="form-control form-conrtrol-sm rounded-0 text-right" value="<?php echo isset($cost) ? number_format($cost, 2) : 0.00 ?>" required readonly>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label for="name" class="control-label">First Name</label>
<input type="text" class="form-control mb-3" name="firstname" placeholder="Enter First Name" value="">
</div>
<div class="form-group">
<label for="name" class="control-label">Last Name</label>
<input type="text" class="form-control mb-3" name="lastname" placeholder="Enter Last Name" value="">
</div>
<div class="form-group">
<label for="name" class="control-label">Contact</label>
<input type="text" class="form-control mb-3" name="contact" placeholder="09xx-xxx-xxxx" value="">
</div>
<div class="form-group">
<label for="name" class="control-label">Email</label>
<input type="text" class="form-control mb-3" name="email" placeholder="example#email.com" value="">
</div>
</div>
</div>
<div class="modal-footer py-2">
<button class="btn btn-warning" type="submit" name="details">Save</button>
<button class="btn btn-secondary "><span class="close-sm">Close</span></button>
</div>
</form>
Here's my modal in view.php
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header py-1">
<h3><?php echo $packages->title; ?></h3>
</div>
<div class="modal-body py-2">
<?php $this->load->view('forms/reserve_form'); ?>
</div>
</div>
Here' my Controller(page.php)
public function reservation_details()
{
$details = array();
$details2 = array();
if ($this->input->post('details')) {
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
$this->form_validation->set_rules('contact', 'Contact', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('date_start', 'Reserve Date', 'required');
$this->form_validation->set_rules('person', 'Number of Person', 'required|max_length[15]');
if ($this->form_validation->run() == true) {
$details = array(
'firstname' => $this->input->post('firstname'),
'lastname' => $this->input->post('lastname'),
'contact' => $this->input->post('contact'),
'email' => $this->input->post('email'),
);
$this->UI_model->reserve_details($details);
$details2 = array(
'date_start' => $this->input->post('date_start'),
'person' => $this->input->post('person'),
'inclusion' => $this->input->post('inclusion'),
);
$this->UI_model->reserve_add($details2);
}
}
}
Here's the Model
public function reserve_details(){
if (!empty($data)) {
$this->db->insert('clients', $data);
return $this->db->insert_id();
}
return false;
}
public function reserve_add(){
if (!empty($data)) {
$this->db->insert('book_list', $data);
return $this->db->insert_id();
}
return false;
}
Also, I'm trying to insert the data for multiple tables. Any help will be much appreciated. Thanks! Is is also fine to place the submit button at modal footer? or it should be at reserve.php?
You're passing data array through these functions.
$this->UI_model->reserve_details($details);
$this->UI_model->reserve_add($details2);
But in the model, you're not catching those.
public function reserve_details(){
public function reserve_add(){
So it should be
public function reserve_details($data){
public function reserve_add($data){
Since you're checking if (!empty($data)) { its always retuns false, unless Undefixed Varibale error.

Data Not Submitting to Database

I'm making a contact page but the form data is not saving to the database. What's the solution?
ContactController.php
public function contact()
{
if($request->isMethod('post'))
{
$data = $request->all();
}
$contact = new Contact;
$contact->name = $data['contact_name'];
$contact->email = $data['contact_email'];
$contact->subject = $data['contact_subject'];
$contact->body = $data['description'];
$category->save();
return redirect()->back()->with('flash_message_success',
'Your message has been sent successfully');
}
contact.blade.php
<form action="{{ url('/contact') }}" id="main-contact-form" class="contact-form row" name="contact-form" method="post">
{{ csrf_field() }}
<div class="form-group col-md-6">
<input type="text" name="contact_name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group col-md-6">
<input type="email" name="contact_email" class="form-control" required="required" placeholder="Email">
</div>
<div class="form-group col-md-12">
<input type="text" name="contact_subject" class="form-control" required="required" placeholder="Subject">
</div>
<div class="form-group col-md-12">
<textarea name="description" id="message" required="required" class="form-control" rows="8" placeholder="Your Message Here"></textarea>
</div>
<div class="form-group col-md-12">
<input type="submit" name="submit" class="btn btn-primary pull-right" value="Submit">
</div>
</form>
Routes:
Route::get('contact', function(){
return view('contact');
});
Route::post('contact', function(){
return view('contact');
});
Use $contact->save(); not $category->save(); and also remove the if statement (for now): if($request->isMethod('post')) {
Your route should be:
Route::post('contact', 'ContactController#contact')->name('contact');

Hidden type input value not being passed in Laravel gallery Project

I have created an album. I am trying to upload photos to the album and storing the album_id through the 'hidden' type input. When i check the source code, album_id is shown in 'value' attribute but unfortunately the value is not being passed to the query during form submission.
My create method of PhotoController which shows the form
public function create($id)
{
$albums = Album::where('id',$id)->first();
return view('admin.pages.photos',compact('albums', 'id'));
}
Here is the form.
<div class="container">
<div class="row">
<a class="btn btn-success" href="/gallery/{{$albums->slug}}">Back to Gallery</a>
<h4>Upload Photos to <strong>{{$albums-> name}}</strong> Gallery</h4>
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
<img class="thumbnail" src="/images/gallery/{{$albums->cover_pic}}" alt="{{$albums->name}}">
</div>
<div class="col-md-8">
<form class="form-horizontal" action="/photo" method="POST" enctype="multipart/form-data" >
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="col-md-8">Photo Title:</label>
<input type="text" name="photo_name" class="form-control" placeholder="Name of the Photo" value="{{ old('photo_name') }}" >
</div>
<div class="form-group">
<label class="col-md-8">Description</label>
<input type="text" name="desc" class="form-control" placeholder="Write Description" value="{{ old('desc') }}">
</div>
<div class="form-group">
<label class="col-md-8">Upload Pic</label>
<input type="file" name="photo" class="form-control" value="{{old('photo')}}" >
</div>
<input type="hidden" name="album_id" value="{{$albums->id}}">
<button type="submit" name="submit" class="btn btn-success waves-effect waves-light m-r-10">Submit</button>
</form>
and the store method
public function store(Request $request)
{
$this->validate($request, [
'photo_name'=>'required|min:3',
'desc'=>'required',
'photo'=>'required'
]);
$photo = new Photo;
$photo->album_id = $request->album_id;
$photo->photo_name = $request->photo_name;
$str = strtolower($request->photo_name);
$photo->slug = preg_replace('/\s+/', '-', $str);
if($file=$request->file('photo')){
$name = time().'.'.$file->getClientOriginalName();
$file->move('images/gallery', $name);
$photo['photo'] = $name;
}
$photo->desc = $request->desc;
$photo->save();
return redirect()->back()->with('status', 'Photo Successfully Added!');
}

Stripe form not submitting

My stripe form is not submitting. When I press submit it goes to page cannot be displayed. I replaced the route code to resolve to an echo "test"; and the post request shows the echo. Any help would be appreciated. This code is from a tutorial http://www.sitepoint.com/selling-downloads-stripe-laravel/
Route
Route::get('/buy/{id}', function($id)
{
$download = Download::find($id);
return View::make('buy', array('download' => $download));
});
Route::post('/buy/{id}', function($id)
{
Stripe::setApiKey(Config::get('laravel-stripe::stripe.sk_test_aaaaaaaaaaaaaaaaaaaaaaaa'));
$download = Download::find($id);
$token = Input::get('stripeToken');
// Charge the card
try {
$charge = Stripe_Charge::create(array(
"amount" => $download->price,
"currency" => "gbp",
"card" => $token,
"description" => 'Order: ' . $download->name)
);
// If we get this far, we've charged the user successfully
Session::put('purchased_download_id', $download->id);
return Redirect::to('confirmed');
} catch(Stripe_CardError $e) {
// Payment failed
return Redirect::to('buy/'.$id)->with('message', 'Your payment has failed.');
}
});
View
#extends('layouts.default')
#section('content')
<h1>Your Order</h1>
<h2>{{ $download->name }}</h2>
<p>£{{ ($download->price/100) }}</p>
<form action="" method="POST" id="payment-form" role="form">
<input type="hidden" name="did" value="{{ $download->id }}" />
<div class="payment-errors alert alert-danger" style="display:none;"></div>
<div class="form-group">
<label>
<span>Card Number</span>
<input type="text" size="20" data-stripe="number" class="form-control input-lg" />
</label>
</div>
<div class="form-group">
<label>
<span>CVC</span>
<input type="text" size="4" data-stripe="cvc" class="form-control input-lg" />
</label>
</div>
<div class="form-group">
<label>
<span>Expires</span>
</label>
<div class="row">
<div class="col-lg-1 col-md-1 col-sm-2 col-xs-3">
<input type="text" size="2" data-stripe="exp-month" class="input-lg" placeholder="MM" />
</div>
<div class="col-lg-1 col-md-1 col-sm-2 col-xs-3">
<input type="text" size="4" data-stripe="exp-year" class="input-lg" placeholder="YYYY" />
</div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Submit Payment</button>
</div>
</form>
#stop

Resources