Uploading in laravel 5.6 not working - laravel

I've tried using this link https://www.5balloons.info/example-file-upload-validation-laravel-5-6/
And I manage to get it working by trying to return the FileName but the problem is the file I'm trying to upload is not showing in the folder "storage/app"
Here is my code
Controller
public function letterOfIntent(Request $request)
{
$fileName = "fileName".time().'.'.request()->fileToUpload->getClientOriginalExtension();
$request->fileToUpload->storeAs('logos',$fileName);
return 'success';
}
View
<form method="post" action="/supplier-information/company-officer/letter-of-intent" method="post" enctype="multipart/form-data" data-form-method="create">
{{ csrf_field() }}
<div class="row">
<div class="col-lg-offset-1 col-lg-4">
<label style="color:tomato;">Letter of Intent</label>
<input type="file" class="form-control-file" name="fileToUpload" id="exampleInputFile" aria-describedby="fileHelp">
<small id="fileHelp" class="form-text text-muted">Please upload a valid image file. Size of image should not be more than 2MB.</small>
</div>
<div class="col-lg-3">
<label>Date of Validity</label>
<input class="form-control" type="date" name="upload_file" id="upload_file" class="form-control">
</div>
<div class="col-lg-3">
<br>
<button type="submit" class="btn btn-primary btn-lg btn-flat text-contrail">Upload</button>
<button type="submit" class="btn btn-success btn-lg btn-flat text-contrail">View</button>
</div>
</div>
</form>

Related

Ckeditor 5 classic in textarea in Laravel Liveware, the form will not be submitted

I use Ckeditor 5 classic in textarea in Laravel Liveware, the form will not be submitted.
How can I solve this problem?
This form is bootstrap modal form. when i use ckeditor code in app.blade.php then the form are not submitted.
** Form Code**
<form wire:submit.prevent="createAppointment">
<div class="modal-body">
<div class="form-group">
<label for="client_id">Client</label>
<select wire:model.defer="state.client_id" id="client_id" class="form-control">
#foreach($clients as $client)
<option value="{{$client->id}}">{{$client->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Date</label>
<div wire:ignore class="input-group date" id="datetimepicker4" data-target-input="nearest" data-appointmentdate = "#this">
<input type="text" class="form-control datetimepicker-input" id="appointmentDateInput" data-target="#datetimepicker4" />
<div class="input-group-append" data-target="#datetimepicker4" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</div>
<div class="form-group">
<label for="">Time</label>
<div class="input-group date" id="datetimepicker3" data-target-input="nearest" data-appointmenttime = "#this">
<input type="text" id="appointmentTimeInput" class="form-control datetimepicker-input" data-target="#datetimepicker3" />
<div class="input-group-append" data-target="#datetimepicker3" data-toggle="datetimepicker">
<div class="input-group-text"><i class="far fa-clock"></i></div>
</div>
</div>
</div>
<div class="form-group" wire:ignore>
<label for="note">Note</label>
<textarea wire.model.defer="state.note" id="note" class="form-control" cols="30" rows="4"></textarea>
</div>
</div>
<div class="modal-footer">
<button
type="button"
class="btn btn-secondary"
data-dismiss="modal"
>
<i class="mr-1 fa fa-times"></i>
Cancel
</button>
<button type="submit" id="submit" class="btn btn-primary">
<i class="mr-1 fa fa-save"></i>
{{ $showEditModal == true ? "Update" : "Save" }}
</button>
</div>
</form>
javascript Code
<script src="https://cdn.ckeditor.com/ckeditor5/29.2.0/classic/ckeditor.js"></script>
<script>
ClassicEditor
.create( document.querySelector( '#note' ) )
.then( editor => {
console.log( editor );
} )
.catch( error => {
console.error( error );
} );
</script>

how to validate Unique database field on Controller in laravel

Good Day, I am new in Laravel environment. I am developing a simple school enrollment registration website but having trouble in validation on my controller..
My plan is, when the user will register in the website, username textbox should be validated if the username is already used by other student. i tried every possible tutorial i found in the net but i have no luck...
Here is my page, red circle should be validated if the input username already exist in the database..
Whole code in .blade
<!--Registration start here -->
<form method="Post" action="{{url('store_account_Registration')}}">
#csrf
#if(Session::get('success'))
<div class="alert alert-danger">
{{ Session::get('success')}}
</div>
#endif
#if(Session::get('fail'))
<div class="alert alert-danger">
{{ Session::get('fail')}}
</div>
#endif
<div class="col-sm-12 form-group">
<input type="hidden" class="form-control" name="uniqID3" id="uid" placeholder="Enrollment Registration Number" value="{{ $regid3 }}" >
</div>
<div class="col-sm-12 form-group">
<input type="hidden" class="form-control" name="pfulname3" id="name-f" placeholder="Enrollment Registration Number" required value="{{ $fulname4 }}" >
</div>
<div class="container-fluid" style="margin-bottom: 2em">
<div class="row justify-content-center align-items-center" style="padding: 10px">
<div class="card col-md-5" style="transform: none;>
<div class="card-body">
<div class="alert alert-primary" role="alert" style="margin-top: 1em">
<p>Name: <strong>{{ $fulname4 }}</strong></p>
<p>Enrollment No.: <strong>{{ $regid3 }}</strong></p>
</div>
<input type="hidden" class="hide" id="csrf_token" name="csrf_token" value="C8nPqbqTxzcML7Hw0jLRu41ry5b9a10a0e2bc2">
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Username</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-user" aria-hidden="true"></i></span>
</div>
<input type="text" class="form-control" name="username" id="username" placeholder="Username" required value="{{ old('username')}}" >
</div>
<div class="help-block with-errors text-danger">
<span style="color:red">#error('username'){{ $message}}#enderror</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Password</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-lock" aria-hidden="true"></i></span>
</div>
<input type="password" id="password" name="password" pattern="^\S{6,}$" onchange="this.setCustomValidity(this.validity.patternMismatch ? 'Must have at least 6 characters' : ''); if(this.checkValidity()) form.password_two.pattern = this.value;" class="form-control" title="Password is needed" required placeholder="Password" >
</div>
<div class="help-block with-errors text-danger">
<span style="color:red">#error('password'){{ $message}}#enderror</span>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Confirm Password</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-lock" aria-hidden="true"></i></span>
</div>
<input id="password_two" type="password" name="password_two" pattern="^\S{6,}$" onchange="this.setCustomValidity(this.validity.patternMismatch ? 'Please enter the same Password as above' : '');" placeholder="Confirm Password" class="form-control" title="Confirm Password is needed" required placeholder="Confirm Password" >
</div>
<div class="help-block with-errors text-danger">
<span style="color:red">#error('password_two'){{ $message}}#enderror</span>
</div>
</div>
</div>
</div>
<div class="row" style="margin-top: 1em" >
<div class="col-md-12" style="margin-bottom: 2em">
<input type="hidden" name="redirect" value="">
<!-- <button type="submit" class="btn btn-primary btn-lg btn-block" name="submit">Save OASIS Account</button> -->
<!-- < <p class="btn btn-primary btn-lg btn-block">Save OASIS Account</p> -->
<button type="submit" class="btn btn-primary btn-lg btn-block">Save OASIS Account</button>
</div>
</div>
</div>
</div>
</div>
</div>
<!--Registration start here -->
</div>
</form>
enter code here
I always got this error..
Here is my code..
my validation in my controller
public function store_account_Registration(Request $request)
{
request()->validate([
'username' => 'required|min:6|unique:TempAccount,AccountName,'
]);
// $this->validator($request->all())->validate();
$current_date = date('Y-m-d H:i:s');
$query = DB::table('TempAccount')->insert([
'RegID'=>$request->input('uniqID3'),
'FullName'=>$request->input('pfulname3'),
'AccountName'=>$request->input('username'),
'Pass'=>$request->input('password'),
'created_at'=> $current_date,
'updated_at'=> $current_date,
]);
if($query){
return back()->with('success', 'Data has been Successfyll inserted');
// return view('pages.enrollment_success');
// return redirect()->route('pages.enrollment_GradeLevelSchoolInfo', ['uniqIDd' => 1]);
// return redirect('enrollment_GradeLevelSchoolInfo');
}
else
{
return back()->with('fail', 'something went wrong');
}
}
my Route in web.php
Route::get('pages',[accountregistration::class, 'index']);
Route::Post('store_account_Registration', [accountregistration::class, 'store_account_Registration']);
my action in form..
The issue is in your form. You are not reaching the correct route at all.
Also you are missing the #csrf token or at least not shown in your print screen
<form action="{{url('store_account_Registration')}}" method="POST">
#csrf
In your validation you can remove the trailing coma after AccountName as well.

How to pass data to a form in laravel 8

This is how I fill my data in a table body:
<tbody>
#foreach($ArrayProducts as $product)
<tr>
<td>{{$product['name']}}</td>
<td>{{$product['description']}}</td>
<td>{{$product['price']}}</td>
<td>{{$product['stock']}}</td>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">Edit</td>
</tr>
#endforeach
</tbody>
But how I can pass the object that is selected in the button Edit of the product to a form to update the product:
<form method="PUT" action="productos">
#csrf
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" id="nombre" name="name" required>
</div>
<div class="form-group">
<label >Descripcion</label>
<input type="text" class="form-control" id="description" name="description" required>
</div>
<div class="form-group">
<label>price</label>
<input id="price" type="number" class="form-control" name="price" required>
</div>
<div class="form-group">
<label>Stock</label>
<input id="stock" type="number" class="form-control" name="stock" required >
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
You will also want to modify this so you know the ID of the product you are editing and do error handling.
<tbody>
#foreach($ArrayProducts as $product)
<tr>
<td>{{$product['name']}}</td>
<td>{{$product['description']}}</td>
<td>{{$product['price']}}</td>
<td>{{$product['stock']}}</td>
<td><button type="button" class="btn btn-primary" data-toggle="modal" data-target="#product-model-{{ $product['id'] }}">Edit</button></td>
</tr>
#endforeach
</tbody>
#foreach($ArrayProducts as $product)
<div class="model" id="product-model-{{ $product['id'] }}">
<form method="PUT" action="productos">
#csrf
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" id="nombre" name="name" required value="{{ $product['name'] }}">
</div>
<div class="form-group">
<label >Descripcion</label>
<input type="text" class="form-control" id="description" name="description" required value="{{ $product['description'] }}">
</div>
<div class="form-group">
<label>price</label>
<input id="price" type="number" class="form-control" name="price" required value="{{ $product['price'] }}">
</div>
<div class="form-group">
<label>Stock</label>
<input id="stock" type="number" class="form-control" name="stock" required value="{{ $product['stock'] }}">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
#endforeach

How to update multiple record using Laravel?

I want to update multiple record related to project and I have already made logic for store data in controller; it's perfectly adding multiple record but I have no idea how to do so.
I want to also update multiple record that already exist in database.
https://ibb.co/sjNKMzJ
Model Issue
public function project()
{
return $this->hasOne('App\Project','project_id',id)
}
Model Project
public function issue()
{
return $this->hasMany('App\Issue','id','project_id')
}
return response
// http://localhost/hourlog/cms/public/projects/43/issues/update
{
"_token": "S6typbw0hywPqaUdxCTqWeJNieyl3VieQhCHqDZ7",
"date": [
"2020-07-06",
"2020-07-22",
"2020-07-11"
],
"issue": [
"edit issue",
"delete issue on web",
"update issue on cms"
]
}
Controller
public function issuesUpdate(Request $request, Project $project)
{
$issues =Issue::where('project_id',$project->id)->get();
//for add new record
foreach($request->date as $key2 => $val){
$issue = new Issue;
$issue->date = $val;
$issue->issue = $request->issue[$key2];
$issue->project_id = $project->id;
$issue->save();
}
return redirect()->route('project');
}
html view
<form action="{{ route('project.issues.update',[$project->id])}}" method="POST">
#csrf
<div class="portlet">
<div class="portlet-heading bg-light-theme">
<h3 class="portlet-title">
<span class="ti-user mr-2">
</span>Add Issues
</h3>
<div class="portlet-widgets">
<span class="divider">
</span>
<button type="submit" class="btn btn-white waves-effect btn-rounded">
<span class="btn-label">
<i class="fa fa-save">
</i>
</span> Save
</button>
</div>
<div class="clearfix">
</div>
</div>
<div id="bg-inverse" class="panel-collapse collapse show" style="">
<div class="portlet-body">
<div class="card-box">
<div class="row">
<div class="col-md-12 mt20">
<div class="addMore">
<div class="addmore_cont">
<div class="addMore_btn">
<div class="mt12 pull-right">
<button type="button" title="Add More" class="btn btn-success waves-effect
waves-light btn-sm add_more" data-key="">
<i class="fa fa-plus">
</i>
</button>
</div>
<div class="row addmore_issues">
<div class="col-md-12">
#if(count($issues))
#foreach($issues as $key => $details)
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input required type="date" value="{{$details->date}}"
name="date[]" class="form-control"
aria-describedby="emailHelp" >
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<input required type="text" value="{{$details->issue}}"
name="issue[]" class="form-control"
aria-describedby="emailHelp" placeholder="Issue...">
</div>
</div>
<div class="col-md-1 mt12">
<button type="button" class="btn btn-danger waves-effect waves-
light btn-sm delete">
<i
class="fa fa-times">
</i>
</button>
</div>
</div>
#endforeach
#else
<div class="row">
<div class="col-md-5">
<div class="form-group">
<input required type="date" value="" name="date[]" class="form-control"
aria-describedby="emailHelp" >
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<input required type="text" value="" name="issue[]" class="form-control"
aria-describedby="emailHelp" placeholder="Issue...">
</div>
</div>
</div>
#endif
</div>
</div>
<!--end row-->
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
Route
Route::post('/projects/{project}/issues/update', "ProjectController#issuesUpdate")-
>name('project.issues.update');
First change your data format from
"date": [
"2020-07-06",
"2020-07-22",
"2020-07-11"
],
"issue": [
"edit issue",
"delete issue on web",
"update issue on cms"
]
To
[
["date"=>"2020-07-06","issue"=>"edit issue"],
["date"=>"2020-07-22","issue"=>"delete issue on web"],
["date"=>"2020-07-11","issue"=>"update issue on cms"]
]
And you can make it by using like below html format
<input required type="date" value="{{$details->date}}"
name="issues[{{$key}}][date]" class="form-control"
aria-describedby="emailHelp" >
Inside Controller
public function issuesUpdate(Request $request, Project $project)
{
$issues = $request->issues;
$insertData = [];
foreach($issues as $key=>$issue){
$issue["project_id"] = $project->id;
$insertData[] = $issue;
}
Issue::where('project_id',$project->id)->delete();// to not insert duplicate issues for the same project
Issue::insert($insertData);
return redirect()->route('project');
}

Error when trying to get property 'name' of non-object

hi i am trying to show the value of name which is stored in database but i am getting this error can anyone please guide me how can i rectify my error .
<form action="Store-data" method="POST">
#csrf
<div class="form-group my-2">
<input type="text" name="name" value="{{ $Task ->name }}">
<div class="form-group my-2">
<input type="email" name="email" placeholder="Enter your email" value="{{ $Task ->email }}" >
<div class="form-group my-2 text-center">
<button type="submit" class="btn btn-success"> Add user</button>
</div>
</div>
</div>
</form>
here is my edit controller
public function edit($Taskid)
{
//
$Edit= Superior::find($Taskid);
return view('Myview.edit')->with('Task','$Edit');
}
here is my route for edit
Route::get('Superior/{Task}/edit','SuperiorController#edit');
i am using Task as a key but it is giving me an error
i also tried but didn't work
{{$Task['name']}}
You should remove ' that wraps $Edit.
return view('Myview.edit')->with('Task', $Edit);
Change your code like this.
Form Code
<form action="Store-data" method="POST">
#csrf
<div class="form-group my-2">
<input type="text" name="name" value="{{isset($Edit) ? $Edit->name : '' }}">
<div class="form-group my-2">
<input type="email" name="email" placeholder="Enter your email" value="{{ isset($Edit) ? $Edit->email : '' }}">
<div class="form-group my-2 text-center">
<button type="submit" class="btn btn-success"> Add user</button>
</div>
</div>
</div>
</form>
Route
Route::get('Superior/edit/{id}','SuperiorController#edit');
Controller Code
public function edit($id){
$Edit= Superior::find($id);
return view('Myview.edit',compact('Edit'));
}
Hopefully it'll solve the problem
Create function like this. if Taskid in your database column.
public function edit(Request $Request, $Taskid)
{
//
$Edit= Superior::where('Taskid', $Taskid)->first();
return view('Myview.edit', compact(Edit));
}
In your html it will like this
{{ $Edit->name }}
In your Route
Route::get('Superior/{Taskid}/edit','SuperiorController#edit');

Resources