When I click save on my form, the page reloads. Which is correct until then. Unfortunately, it does not save the data for me in the database. So I tried to find the error with dd($request->all()). Unfortunately, I haven't found a real error as far as the correct data is loaded on the form. However, they never end up in the database. I currently only have one guess that it is due to the wrong format of DateTime_local input. But can't say for sure.
I don't get any error messages or anything like that.
Here is a small snippet of my code:
app/Http/Controllers/TasksController
public function store(Request $request)
{
// validate all required Data
$request->validate([
'title' => 'required',
'user' => 'required',
'labels' => 'required',
'work' => 'required',
'start_work' => 'required',
'problems' => 'required',
'stop_work' => 'required',
]);
$input = $request->all();
Task::create($input);
return back()->with('success', 'Successfully saved.');
}
app/Models/Task.php
class Task extends Model
{
use HasFactory;
public $fillable = ['title', 'user', 'labels', 'work', 'start_work', 'problems', 'stop_work'];
}
Migration
public function up()
{
Schema::create('task', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('user');
$table->string('label');
$table->string('work');
$table->string('problems');
$table->dateTime('start_work');
$table->dateTime('stop_work');
$table->timestamps();
});
}
View snippet
#extends('layouts.app')
#section('content')
<!-- Success message -->
#if(Session::has('success'))
<div class="alert alert-success">
{{Session::get('success')}}
</div>
#endif
<form method="post" action="{{ route('screate') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<section class="person">
<div class="container-fluid">
<div class="card card-default">
<div class="card-header">
<h3 class="card-title">Daten</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div>
<!-- /.card-header -->
<div class="card-body">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="title">title</label>
<input type="text" class="form-control {{ $errors->has('title') ? 'error' : '' }}" id="title" name="title" required>
</div>
<!-- /.form-group -->
</div>
<div class="col-md-4">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control {{ $errors->has('username') ? 'error' : '' }}" id="username" name="username">
</div>
</div>
<!-- /.form-group -->
<div class="col-md-4">
<div class="form-group">
<label for="labels">Labels</label>
<select id="labels" name="label" class="select2 select2-hidden-accessible {{ $errors->has('label') ? 'error' : '' }}" multiple="" name="label[]" style="width: 100%;" data-select2-id="5" tabindex="-1" aria-hidden="true">
#foreach($labels as $label)
<option value="{{ $label->id }}">{{ $label->name }}</option>
#endforeach
</select>
</div>
</div>
<!-- /.form-group -->
<div class="col-md-4">
<div class="form-group">
<label for="work">work</label>
<input type="text" class="form-control {{ $errors->has('work') ? 'error' : '' }}" id="work" name="work">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="start_work">Start-Work</label>
<input type="datetime-local" class="form-control {{ $errors->has('start_work') ? 'error' : '' }}" id="start_work" name="start_work">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="problems">Problems</label>
<input type="text" class="form-control {{ $errors->has('problems') ? 'error' : '' }}" id="problems" name="problems">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="stop_work">Stop-Work</label>
<input type="datetime-local" class="form-control {{ $errors->has('start_work') ? 'error' : '' }}" id="stop_work" name="stop_work">
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div><!-- /.container-fluid -->
</section>
<section class="options">
<div class="row">
<div class="col-md-2 centered">
<button class="btn btn-danger" href="{{ route('home') }}">
<i class="fas fa-times">
</i>
Abbrechen
</button>
</div>
<div class="col-md-2">
<button class="btn btn-success" type="submit" name="submit">
<i class="fas fa-plus">
</i>
Speichern
</button>
</div>
</div>
</section>
</form>
#endsection
I hope you can help me with this small problem.
edit:
Here is my dd output:
dd() output
Related
I created a table called "directors" using migrations.Then i created the model, view, controller.
In "DirectorController" i wrote crud operations logic for my table. It works until i try to edit a existing record. To be clear it update all informations except the image, it remains the same. Can someone tell me what is wrong?
*Folder "images" exists in public.
enter image description here
//Migration
public function up()
{
Schema::create('directors', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->datetime('birth');
$table->string('town');
$table->string('country');
$table->string('image');
$table->timestamps();
});
}
//DirectorController
public function update(Request $request, Director $director)
{
$request->validate([
'first_name' => 'required',
'last_name' => 'required',
'birth' => 'required',
'town' => 'required',
'country' => 'required'
]);
$input = $request->all();
if ($image = $request->file('image')) {
$destinationPath = 'images/';
$profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
$image->move($destinationPath, $profileImage);
$input['image'] = "$profileImage";
}else{
unset($input['image']);
}
$director->update($input);
return redirect()->route('directors.index')->with('success', 'Director updated successfully.');
}
//DirectorModel
protected $fillable = [
'first_name',
'last_name',
'birth',
'town',
'country',
'image'];
public function movies() {
return $this->hasMany(Movie::class);
}
// edit.blade.php
#extends('directors.layout')
#section('content')
<div class="row">
<div class="col-lg-12 d-flex justify-content-between mt-5 mb-4">
<div class="pull-left">
<h2>Edit Director</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('directors.index') }}"><i class="fa-solid fa-delete-left"></i> Back</a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<div class="first d-flex justify-content-between">
<strong>Whoops!</strong>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
There were some problems with your input. <br><br>
<ul>
#foreach ( $errors->all() as $error )
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('directors.update', $director->id) }}" method="POST">
#csrf
#method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>First Name:</strong>
<input type="text" name="first_name" value="{{ $director->first_name }}" class="form-control" placeholder="First Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Last Name:</strong>
<input type="text" name="last_name" value="{{ $director->last_name }}" class="form-control" placeholder="Last Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Birth:</strong>
<input type="text" name="birth" value="{{ $director->birth }}" class="form-control" placeholder="Birth">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Town:</strong>
<input type="text" name="town" value="{{ $director->town }}" class="form-control" placeholder="Town">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Country:</strong>
<input type="text" name="country" value="{{ $director->country }}" class="form-control" placeholder="Country">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Image:</strong>
<input type="file" name="image" class="form-control" placeholder="image">
<img src="/images/{{ $director->image }}" width="300px">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md12 text-center">
<button type="submit" class="btn btn-primary"><i class="fa-solid fa-square-plus"></i> Submit</button>
</div>
</div>
</form>
#endsection
You need to change your form as:
<form action="{{ route('directors.update', $director->id) }}" method="POST" enctype="multipart/form-data">
I am currently making laravel project using internet references and now I'm stuck at CRUD operation.
I use Laravel 8 with jetstream for auth, now ui template assets, bootstrap.
I tried several CRUD codes from different sources and everything worked except for edit page.
Index page:
Edit page:
This is my route:
Route::resource('projects', ProjectController::class);
My controller:
public function edit(Project $project)
{
return view('projects.edit', compact('project'));
}
public function update(Request $request, Project $project)
{
$request->validate([
'name' => 'required',
'introduction' => 'required',
'location' => 'required',
'cost' => 'required'
]);
$project->update($request->all());
return redirect()->route('projects.index')
->with('success', 'Project updated successfully');
}
The blade file:
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Product</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('projects.index') }}" title="Go back"> <i class="fas fa-backward "></i> </a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('projects.update', $project->id) }}" method="POST">
#csrf
#method('PUT')
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
<input type="text" name="name" value="{{ $project->name }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Introduction:</strong>
<textarea class="form-control" style="height:50px" name="introduction"
placeholder="Introduction">{{ $project->introduction }}</textarea>
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Location:</strong>
<input type="text" name="location" class="form-control" placeholder="{{ $project->location }}"
value="{{ $project->location }}">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Cost:</strong>
<input type="number" name="cost" class="form-control" placeholder="{{ $project->cost }}"
value="{{ $project->location }}">
</div>
</div>
<div class="text-center col-xs-12 col-sm-12 col-md-12">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
#endsection
I tried changing the edit blade file codes to different ones but still same. I tried different method from online for the edit function in controller but nothing change. I also tried completely different coding, different model controllers and all, still the same. Only edit page turns out that way. Anyone have any clue?
Please help me out. I'm having the missing required parameter's error. This is the form script:
<div class="container">
<div class="row">
<!--first column-->
<div class="col-md-6 col-sm-6 ">
<!-- Default form contact -->
<form class="text-center border border-light p-5" action="{{route('updating', $update->id)}}" enctype="multipart/form-data" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}" >
#method('PATCH')
<div class="well">
<p class="h4 mb-4">Edit Updates</p>
</div>
<!-- Name -->
<input type="text" name="title" id="defaultContactFormName" class="form-control mb-2" value=" {{ old('title') ?? $update->title }} ">
<!-- Message -->
<div class="form-group">
<textarea class="form-control rounded-0" name="body" id="exampleFormControlTextarea2" rows="5" value="{{ old('title') ?? $update->body }}"></textarea>
</div>
<!-- Send button -->
<button class="btn btn-success btn-block" type="submit">Publish</button>
</form>
</div>
</div>
</div>
And this is my controller and edit function:
public function update(Request $request, $title)
{
$this->validate($request, [
'title'=>'required',
'body'=>'required'
]);
//Publishing Updates
$update = Update::findOrFail($title);
$update->title = $request->input('title');
$update->body = $request->input('body');
$update->save();
return redirect('admin.updates')->with('success', 'Saved');
}
And this is my route:
Route::patch('/updates/{title}/update', 'UpdatesController#update')->name('updating');
I keep getting the error and i don't know where its coming from.
I create a contact form on the home page of the website.
This is the code:
<section id="contact" class="light-grey-background m-top-30">
<div class="container">
<header>
<h2 class="title-contact text-center">
<b>CONTATTACI</b>
</h2>
</header>
#if (count($errors) > 0)
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">x</button>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form id="formEmail" class="m-top-40" method="post" action="{{ url('/send') }}">
{{ csrf_field() }}
<div class="form-row">
<div class="col-sm-12">
<div class="form-group">
<input id="oggetto" name="oggetto" type="text" placeholder="Oggetto" class="form-control">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input id="nome" name="nome" type="text" placeholder="Nome" class="form-control">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input id="cognome" name="cognome" type="text" placeholder="Cognome" class="form-control">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input id="email" name="email" type="email" placeholder="Email" class="form-control">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<textarea id="messaggio" rows="6" placeholder="Scrivi qui il tuo messaggio..." name="messaggio" class="form-control"></textarea>
</div>
<div class="form-group text-center">
<input type="submit" name="send" class="btn btn-primary contact m-bottom-70 text-center" value="Send" />
</div>
</div>
</div>
</form>
</div>
</section>
This is the HomeController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
function index()
{
return view('home');
}
function send(Request $request)
{
$this->validate($request, [
'oggetto' => 'required',
'nome' => 'required',
'cognome' => 'required',
'email' => 'required|email',
'messaggio' => 'required'
]);
}
}
And this is the route for the home page:
Route::get('/', 'HomeController#index');
Route::post('/send', 'HomeController#send');
The problem is that when you click on the send button the page is completely updated and to know the status of the form you have to scroll the page. Is there a way to avoid the problem and update only the section that contains this?
I have a form in my Laravel 4 web app that is refusing to submit to the database. Each time i try to submit, the page just reloads and i see no error messages even in the laravel log. I have spent two days trying to figure out what the problem is as I can't seem to see anything wrong with the code.
Any help will be appreciated.
/**** THE FORM VIEW ***/
<div class="container">
<div class="row">
#if(Session::has('success'))
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
{{Session::get('success')}}
</div>
#elseif(Session::has('fail'))
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
{{Session::get('fail')}}
</div>
#endif
</div>
#include('partials.admin-navbar')
<div class="admin_profile_content">
<form class="" method="post" action=" {{URL::route('postSubmitCompetition')}}">
<div class="" style="width:80%; margin:auto;">
<div class="panel panel-default">
<div class="panel-heading">Organization </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">Select the Organization hosting this competition </label>
<div class="col-sm-10">
{{ Form::select('organization', $organizations, null, ['class' => 'form-control']) }}
<p class="text-danger">
#if($errors->has('organization'))
{{ $errors->first('organization') }}
#endif
</p>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Competition name </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">Competition name </label>
<div class="col-sm-10">
<input type="text" class="form-control" id="" name="competition_name">
<p class="text-danger">
#if($errors->has('competition_name'))
{{ $errors->first('competition_name') }}
#endif
</p>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Prizes </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">Total prize pool</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="" name="total_prize">
</div>
</div>
<div class="form-group" style="padding-top:41px;">
<label for="" class="col-sm-2 control-label">Number of prize-winning places</label>
<div class="col-sm-1">
Top
</div>
<div class="col-sm-2">
<input type="text" class="form-control" id="" name="number_of_winning_places">
<p class="text-danger">
#if($errors->has('number_of_winning_places'))
{{ $errors->first('number_of_winning_places') }}
#endif
</p>
</div>
<div class="col-sm-3">
competitors will win a prize
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Timeline </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Start and end date </label>
<div class="col-sm-3">
<input type="text" class="form-control" id="competition_start_date" name="competition_start_date">
<p class="text-danger">
#if($errors->has('competition_start_date'))
{{ $errors->first('competition_start_date') }}
#endif
</p>
<input type="hidden" name="hidden_start_date" id="hidden_start_date">
</div>
<div class="col-sm-2">
to
</div>
<div class="col-sm-3">
<input type="text" class="form-control" id="competition_end_date" name="competition_end_date">
<p class="text-danger">
#if($errors->has('competition_end_date'))
{{ $errors->first('competition_end_date') }}
#endif
</p>
<input type="hidden" name="hidden_end_date" id="hidden_end_date">
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Competition details </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Competition details </label>
<div class="col-sm-10">
<textarea class="form-control" name="competition_details" id="competition_details" rows="10"> </textarea>
<p class="text-danger">
#if($errors->has('competition_details'))
{{ $errors->first('competition_details') }}
#endif
</p>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Competition Status </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Set competition status </label>
<div class="col-sm-10">
<select class="form-control" name="competition_status" id="competition_status">
<option value="0">Coming Soon</option>
<option value="1">Live </option>
</select>
</div>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">Competition data </div>
<div class="panel-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label"> Upload the data for the competition </label>
<div class="col-sm-10">
<input type="file" id="competition_data_1" name="competition_data_1">
<p class="help-block"> Data file/folder 1</p>
</div>
</div>
</div>
</div>
{{ Form::token() }}
<div class="panel panel-default">
<div class="panel-heading">Upload Competition </div>
<div class="panel-body">
<div class="form-group">
<div class="col-sm-10">
<input type="submit" class="btn btn-primary btn-lg btn-block" id="submit_competition" value="Submit">
</div>
</div>
</div>
</div>
</div>
</form>
<hr>
/* the controller method */
public function postSubmitCompetition()
{
$validator = Validator::make(Input::all(), array(
'organization' => 'required',
'competition_name' => 'required',
'prize_winning_places' => 'required',
'competition_start_date' => 'required',
'competition_end_date' => 'required',
'competition_details' => 'required',
'status' => 'required'
));
if($validator->fails())
{
return Redirect::route('getSubmitCompetition')->withErrors($validator)->withInput();
}
else
{
$competition = new Competition();
$competition->hosting_organization_id = Input::get('organization');
$competition->competition_name = Input::get('competition_name');
$competition->total_prize_pool = Input::get('total_prize');
$competition->prize_winning_places = Input::get('number_of_winning_places');
$competition->start_date = Input::get('competition_start_date');
$competition->end_date = Input::get('competition_end_date');
$competition->competition_details = Input::get('competition_details');
$competition->status = Input::get('competition_status');
if($competition->save())
{
return Redirect::route('getSubmitCompetition')->with('success', 'You have successfully created this competition');
}
else
{
return Redirect::route('getSubmitCompetition')->with('fail', 'An error occurred while creating that competition. Please contact sys admin');
}
}
}
/* the route */
Route::post('/admin/submit-a-competition', array('uses' => 'CompetitionController#postSubmitCompetition', 'as' => 'postSubmitCompetition'));
Problem
Laravel never saves your data because the validator fails.
$validator = Validator::make(Input::all(), array(
'organization' => 'required',
'competition_name' => 'required',
'prize_winning_places' => 'required',
'competition_start_date' => 'required',
'competition_end_date' => 'required',
'competition_details' => 'required',
'status' => 'required'
));
You require prize_winning_places and status but there are no input fields for these two in the view.
The missing input fields:
Field name: status - Problem: You made a select with the name: competition_status instead of: status
<select class="form-control" name="competition_status" id="competition_status">
<option value="0">Coming Soon</option>
<option value="1">Live </option>
</select>
Field name: prize_winning_places - Problem: You made an input field with the name: ** number_of_winning_places **instead of prize_winning_places
<input type="text" class="form-control" id="" name="number_of_winning_places">
Solution
Change the $validator variable to:
$validator = Validator::make(Input::all(), array(
'organization' => 'required',
'competition_name' => 'required',
'number_of_winning_places' => 'required',
'competition_start_date' => 'required',
'competition_end_date' => 'required',
'competition_details' => 'required',
'competition_status' => 'required'
));
Check this line:
<form class="" method="post" action=" {{URL::route('postSubmitCompetition')}}">
here form action is the route name not the function name. Like:
Route::post('/save_data', array('uses' => 'CompetitionController#postSubmitCompetition', 'as' => 'postSubmitCompetition'));
It will route your route to the function.