Profile Not Updating In Laravel Livewire - laravel

I was to create a user profile components that's will allow users to update there account, so i have created the profile update form when I input the new details I'm getting there is no user_id
public $users, $name, $email, $user_id, $creator_name, $creator_bio;
public $updateMode = false;
private function resetInputFields(){
$this->name = '';
$this->email = '';
$this->creator_name = '';
$this->creator_bio = '';
}
public function edit($id)
{
$this->updateMode = true;
$user = User::where('id',$id)->first();
$this->user_id = $id;
$this->name = $user->name;
$this->email = $user->email;
$this->creator_name = $user->creator_name;
$this->creator_bio = $user->creator_bio;
}
public function cancel()
{
$this->updateMode = false;
$this->resetInputFields();
}
public function update()
{
$validatedDate = $this->validate([
'name' => 'required',
'email' => 'required|email',
]);
if ($this->user_id) {
dd('There is a user id');
$user = User::find($this->user_id);
$user->update([
'name' => $this->name,
'email' => $this->email,
]);
$this->updateMode = false;
session()->flash('message', 'Users Updated Successfully.');
$this->resetInputFields();
} else {
dd('There is NOT a user id');
}
}
// public function delete($id)
// {
// if($id){
// User::where('id',$id)->delete();
// session()->flash('message', 'Users Deleted Successfully.');
// }
// }
public function render()
{
return view('livewire.profile-update');
}
here is my profile update form when I click update nothing is happening and I'm passing the livewire script correctly
<div>
<form>
<div class="form-group">
<input type="hidden" wire:model="user_id">
<label for="exampleFormControlInput1">Name</label>
<input type="text" class="form-control" wire:model="name" id="exampleFormControlInput1" placeholder="Enter Name">
#error('name') <span class="text-danger">{{ $message }}</span>#enderror
</div>
<div class="form-group">
<input type="hidden" wire:model="user_id">
<label for="exampleFormControlInput1">Creator Name</label>
<input type="text" class="form-control" wire:model="creator_name" id="exampleFormControlInput1" placeholder="Enter Name">
#error('creator_name') <span class="text-danger">{{ $message }}</span>#enderror
</div>
<div class="form-group">
<input type="hidden" wire:model="user_id">
<label for="exampleFormControlInput1">Creator Bio</label>
<input type="text" class="form-control" wire:model="creator_bio" id="exampleFormControlInput1" placeholder="Enter Name">
#error('creator_bio') <span class="text-danger">{{ $message }}</span>#enderror
</div>
<div class="form-group">
<label for="exampleFormControlInput2">Email address</label>
<input type="email" class="form-control" wire:model="email" id="exampleFormControlInput2" placeholder="Enter Email">
#error('email') <span class="text-danger">{{ $message }}</span>#enderror
</div>
<button wire:click.prevent="update()" class="btn btn-dark">Update</button>
<button wire:click.prevent="cancel()" class="btn btn-danger">Cancel</button>
</form>
</div>

you must implement the fillable or guarded protected property in the User class
protected $fillable = [
'name','email'
];
// or
protected $guarded = [];

Related

Get product id to store attachments accordingly

I currently have the add attachment button for each product on the product list page. After clicking the button, will proceed to add attachment form. How do I get the current product ID from the product table in order to store the attachment data into the attachment table?
Route
Route::post('/store/{product}', 'AttachmentController#store')->name('attachment.store');
Product Model
public function attachment()
{
return $this->hasMany(Attachment::class, 'product_id', 'id');
}
Attachment Model
public function product()
{
return $this->belongsTo(Product::class, 'product_id');
}
Controller
public function create()
{
return view('document.create', ['prod' => Product::select('id', 'name')->get()]);
}
public function store(Request $request, Product $product) {
$data['product_id'] = $product->id;
$data = $request->validate([
'file' => 'required',
'file.*' => 'mimes:csv,xlsx,pdf,docx',
]);
$attachmentData = [];
if($request->hasFile('file'))
{
foreach($request->file('file') as $file) {
$path = public_path('storage/attachments/'.$request->product_id);
$fileName = time().'-'.$file->getClientOriginalName();
$file->move($path, $fileName);
$attachmentData[] = $fileName;
}
$data['file'] = json_encode($attachmentData);
}
$attachment = Attachment::create($data);
return redirect()->route('product.index')->with('success','Attachment added successfully');
}
Blade View
<form method="POST" action="{{route('attachment.store')}}" enctype="multipart/form-data">
#csrf
<h3><b>Add Attachment</b></h3>
<input type="submit" class="btn btn-primary mr-2" value="Save">
<div class="row">
<h4 class="card-title">General</h4>
<input type="text" name="product_id" value="{{ $product->id ?? '' }}" hidden>
<div class="form-group">
<label for="name">Name</label>
<input type="text" id="name" class="form-control" name="name" required>
</div>
<div class="form-group">
<label>Attachment </label>
<div class="input-group-append">
<label for="attachment" class="btn btn-info">Upload</label>
<input id="attachment" type="file" name="file[]" multiple required>
</div>
</div>
</div>
</form>
You have to use form action like below
<form method="POST" action="{{route('attachment.store',['product'=>$product->id])}}" enctype="multipart/form-data">

Laravel - How to update checkbox field value

Using Laravel-5.8, I have been able to save the data into the database including the check box field.
protected $fillable = [
'appraisal_name',
'is_current',
'appraisal_start',
'appraisal_end',
];
public function rules()
{
return [
'appraisal_name' => 'required|min:5|max:100',
'appraisal_start' => 'required',
'appraisal_end' => 'required|after_or_equal:appraisal_start',
'is_current' => 'nullable|boolean',
];
}
public function create()
{
return view('appraisal.appraisal_identities.create');
}
public function store(StoreAppraisalIdentityRequest $request)
{
$identity = AppraisalIdentity::create([
'appraisal_name' => $request->appraisal_name,
'appraisal_start' => $appraisalStart,
'appraisal_end' => $appraisalEnd,
'is_current' => $request->has('is_current'),
]);
Session::flash('success', 'Appraisal Initialization is created successfully');
return redirect()->route('appraisal.appraisal_identities.index');
}
public function edit($id)
{
abort_unless(\Gate::allows('appraisal_identity_edit'), 403);
$identity = AppraisalIdentity::where('id', $id)->first();
return view('appraisal.appraisal_identities.edit')->with('identity', $identity);
}
public function update(UpdateAppraisalIdentityRequest $request, $id)
{
abort_unless(\Gate::allows('appraisal_identity_edit'), 403);
$appraisalStart = Carbon::parse($request->appraisal_start);
$appraisalEnd = Carbon::parse($request->appraisal_end);
$submissionStart = Carbon::parse($request->submission_start);
$submissionEnd = Carbon::parse($request->submission_end);
$identity = AppraisalIdentity::find($id);
$identity->appraisal_name = $request->appraisal_name;
$identity->appraisal_start = $appraisalStart;
$identity->appraisal_end = $appraisalEnd;
$identity->is_current = $request->has('is_current');
$identity->save();
Session::flash('success', 'Appraisal Initialization is updated successfully');
return redirect()->route('appraisal.appraisal_identities.index');
}
create.blade
<form action="{{route('appraisal.appraisal_identities.store')}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label class="control-label text-right col-md-3">Is Current Appraisal?</label>
<div class="col-md-9">
<input type="checkbox" class="form-control" name="is_current" value="{{old('is_current')}}">
</div>
</div>
</div>
</div>
</div>
<div>
<!--<input class="btn btn-primary" type="submit" value="{{ trans('global.save') }}">-->
<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
<button type="button" onclick="window.location.href='{{route('appraisal.appraisal_identities.index')}}'" class="btn btn-default">Cancel</button>
</div>
</form>
edit.blade
<form action="{{route('appraisal.appraisal_identities.update', ['id'=>$identity->id])}}" method="post" class="form-horizontal" enctype="multipart/form-data">
{{ csrf_field() }}
<input name="_method" type="hidden" value="PUT">
<div class="form-body">
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label class="control-label text-right col-md-3">Is Current Appraisal?</label>
<div class="col-md-9">
<input type="checkbox" class="form-control" name="is_current" value="{{old('is_current')}}">
</div>
</div>
</div>
</div>
</div>
<div>
<!--<input class="btn btn-primary" type="submit" value="{{ trans('global.save') }}">-->
<button type="submit" class="btn btn-primary">{{ trans('global.save') }}</button>
<button type="button" onclick="window.location.href='{{route('appraisal.appraisal_identities.index')}}'" class="btn btn-default">Cancel</button>
</div>
</form>
I have these issues while trying to update the data?
The edit checkbox field (is_current) did not pick the value from the database when loaded. It remains unchecked.
is_current is either 0 or 1. The goal is that, there can only be one is_current field that is set to 1 in the table. From the checkbox, when is_current is checked to 1, it should set any other is_current that is 1 to 0 in the table.
How do I resolve these issues?
Thank you.
In edit blade update your checkbox code as this:
<input type="checkbox" class="form-control" name="is_current" #if($identity->is_current == 1) checked #endif value="{{old('is_current')}}">
In your update function change this:
public function update(UpdateAppraisalIdentityRequest $request, $id)
{
abort_unless(\Gate::allows('appraisal_identity_edit'), 403);
$appraisalStart = Carbon::parse($request->appraisal_start);
$appraisalEnd = Carbon::parse($request->appraisal_end);
$submissionStart = Carbon::parse($request->submission_start);
$submissionEnd = Carbon::parse($request->submission_end);
$identity = AppraisalIdentity::find($id);
$identity->appraisal_name = $request->appraisal_name;
$identity->appraisal_start = $appraisalStart;
$identity->appraisal_end = $appraisalEnd;
$identity->is_current = $request->has('is_current');
$identity->save();
// this line update all column to 0 and leave $id field
AppraisalIdentity::where('id', '!=', $id)->update(['is_current' => 0]);
Session::flash('success', 'Appraisal Initialization is updated successfully');
return redirect()->route('appraisal.appraisal_identities.index');
}
In your store function change this:
public function store(StoreAppraisalIdentityRequest $request)
{
$identity = AppraisalIdentity::create([
'appraisal_name' => $request->appraisal_name,
'appraisal_start' => $appraisalStart,
'appraisal_end' => $appraisalEnd,
'is_current' => $request->has('is_current'),
]);
$id = $identity->id;
// this line update all column to 0 and leave $id field
AppraisalIdentity::where('id', '!=', $id)->update(['is_current' => 0]);
Session::flash('success', 'Appraisal Initialization is created successfully');
return redirect()->route('appraisal.appraisal_identities.index');
}
try this in the edit blade
<
is_current == 1 ? checked : value="{{old('is_current')}}">

I want to send email for more than one user to Mailtrap

I tried to send an email to a user and arrived at Miltrap
Now I put all the users on my system (table users in database) and introduced them to checkboxs and I want to send one email to more than one user or to all users and I did not see the result on the mailtrap
This is the code
MailboxController.php
public function form(Request $request)
{
$users = User::where('id', '!=', auth()->user()->id)->get();
return view('MailBox.form',compact('users'));
}
public function contact(Request $request)
{
//$emails = [];
$this->validate($request, [
'email' => 'required|email',
'subject' => 'min:3',
'message' => 'min:10']);
$data = array(
'email' => $request->email,
'subject' => $request->subject,
'bodyMessage' => $request->message
);
Mail::send('MailBox.contact', $data, function($message) use ($data){
$message->from('info#dev-any.com');
$message->to($data['email']);
$message->subject($data['subject']);
});
Session::flash('success', 'Your Email was Sent!');
return redirect('/dashboard/mailbox');
}
Web.php
Route::get('/mailbox', 'MailboxController#form')->name('mailbox.form');
Route::post('/mailbox/contact', 'MailboxController#contact')->name('contact');
form.blade.php
<form action="{{ route('contact') }}" method="POST">
{{ csrf_field() }}
<div class='form-group col-md-6'><strong>Select User :</strong><br>
<input type="checkbox" id = "chckHead" name="chk_all" value="" onchange="checkall()" id='allcb' /><strong> Check All </strong> </br>
#foreach ($users as $user)
<tr>
<td><input type="checkbox" class = "chcktbl" onchange="check()" name="email" id="email" value={{ $user->id }}></td>
<td>{{ $user->name }}</td></br>
</tr>
#endforeach
</div>
<div class="form-group col-md-6">
<label name="subject">Subject:</label>
<input id="subject" name="subject" class="form-control">
</div>
<div class="form-group col-md-6">
<label name="message">Message:</label>
<textarea id="message" name="message" class="form-control">Type your message here...</textarea>
</div>
<input type="submit" value="Send Message" class="btn btn-success col-md-6">
</form>
</div>
</div>
<script type="text/javascript">
$('.chcktbl').click(function () {
var length = $('.chcktbl:checked').length;
if (length > 3) {
// alert(length);
$('.chcktbl:not(:checked)').attr('enable', true);
}
else {
$('.chcktbl:not(:checked)').attr('enable', false);
}
});
</script>
<script type="text/javascript">
$('#chckHead').click(function () {
if (this.checked == false) {
$('.chcktbl:checked').attr('checked', false);
}
else {
$('.chcktbl:not(:checked)').attr('checked', true);
}
});
$('#chckHead').click(function () {
});
Simply change the email checkbox, and modify the validation
<input type="checkbox" class="chcktbl" onchange="check()" name="email[]" id="email" value={{ $user->id }}>

I want to display data in textfield. when search data have

I want to display data in textfield . Not all data. When search by name and it have data then went to display data in textfield. And search form and want to display form is same form.
<form action="postAuth" method="post" enctype="multipart/form-data">
<div class="input-group">
<input type="text" class="form-control" name="productname" placeholder="Search Product"> <span class="input-group-btn">
<button type="submit" class="btn btn-default" name="search">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div>
<div class="form-group">
<label for="ProductName" >Product Name :</label>
<input type="text" class="form-control" name="ProductName">
</div>
</form>
route
Route::post("postAuth", ['as' => 'search' , 'uses'=> 'ProductController#postAuth']);
That's my controller
public function postAuth(Request $request)
{
//check submit
$update = $request->get('update',false);
if($update){
return $this->update($request);
}
$productname = $request->input('productname');
$product = DB::table('products')
->where('product_name','LIKE','%'.$productname.'%')
->get();
if($product->count() > 0)
return redirect()->to('/update')->withDetails($product)->withQuery($productname);
else
$request->session()->flash('alert-danger','No Data Found!');
return redirect()->to('/update');
}
can anyone help me please
Here what you exactly want : Just for example
<div class="form-group">
{{Form::label('name', trans('admin.venue.fields.name'),['class' => 'col-md-4 control-label'])}}
<div class="col-md-6">
{{Form::text('name',old('name', isset($venue) ? $venue->name : ''),['class' => 'form-control'])}}
</div>
</div>
Model function example :
public function save(Request $request) {
try {
$this->validate($request, Venue::rules(), Venue::messages());
$venue = Venue::saveOrUpdate($request);
if($venue !== false) {
if($request->get('continue', false)) {
return redirect()->route('admin.venue.edit', ['id' => $venue->id])->with('success', trans('admin.venue.save_success'));
} else {
return redirect()->route('admin.venue.index')->with('success', trans('admin.venue.save_success'));
}
} else {
return back()->with('error', "Unable to save venue")->withInput();
}
} catch (\Exception $ex) {
return back()->with('error', "Unable to save venue")->withInput();
}
}
Hope it is useful.
use session() in the value field of ProductName input tag and with some logic, for example
view file:
#if(session()->has('data'))
#if(count(session('data')))
#foreach(session('data') as $data)
<input type="text" class="form-control" name="ProductName" value="{{ $data->prodcut_name }}">
#endforeach
#else
<input type="text" class="form-control" name="ProductName" value="No Data Available">
#endif
#else
<input type="text" class="form-control" name="ProductName">
#endif
and in Controller:
public function postAuth(Request $request) {
$productname = $request->input(productname);
$product_search = Products::where('product_name', $productname)->get();
if($product_search) {
return redirect()->back()->with('data', $product_search);
}
}

How to Manage Accounts in the Laravel 5 FrameWork - MVC - with this code?

How to Manage Accounts in the Laravel 5 FrameWork - MVC - with this code? I got it all for a default presentation but i still get an Undefined Variable request with this code - please your answer will be appreciated:
UserController:
public function account(&$data, $request){
User::get_user(self::$data,$request);
self::$data['title'] = self::$data['title'] . 'Edit Account';
return view('forms.account', self::$data);
}
public function postAccount(AccountRequest $request){
User::edit_user($request);
return redirect('');
}
AccountRequest:
public function rules()
{
return [
'name' => 'required|min:2|max:70',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:6|max:10|confirmed',
];
}
Model:
static public function get_user(&$data,$request){
$sql = "SELECT * FROM users WHERE id = ". Session::get('user_id');
$data['users'] = DB::select($sql);
}
static public function edit_user(&$data,$request) {
$id = $data['id'];
$sql = "SELECT * FROM users WHERE id = ".$id;
$getVal = DB::select($sql);
if($data['name'] || $data['password'] || $data['email']){
if($data['name']){
DB::update("UPDATE users SET name = ? WHERE id = ?",[$data['name'],$id]);
session(['user_name' => $data['name']]);
}
if($data['password']){
DB::update("UPDATE users SET password = ? WHERE id = ?",[bcrypt($data['password']),$id]);
}
if($data['email']){
DB::update("UPDATE users SET email = ? WHERE id = ?",[$data['email'],$id]);
}
}
Session::flash('sm',$request['name'] . '`s Has Been Updated');
}
Web:
Route::get('user/account', 'UserController#account');
Route::post('user/account', 'UserController#postAccount');
HTML:
#extends('master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<h1>Edit Your Account -</h1>
</div>
<div class="row" style="margin-left:30%;">
<div class="col-md-6">
<form action="" method="post">
{{ csrf_field() }}
<input type="hidden" name="user_id" value="{{ $user['id'] }}">
<div class="form-group">
<label for="name"></label>
<input value="{{ $user['name'] }}" type="text" name="name"
class="form-control" id="name" placeholder="Name">
</div>
<div class="form-group">
<label for="email"></label>
<input value="{{ $user['email'] }}" type="text" name="email"
class="form-control" id="email" placeholder="Email">
</div>
<div class="form-group">
<label for="editpassword"></label>
<input type="password" name="password" class="form-control"
id="editpassword" placeholder="Edit Password">
</div>
<div class="form-group">
<label for="editpasswordconf"></label>
<input type="password" name="password_confirmation" class="form-
control" id="editpasswordconf" placeholder="Confirm New Password">
</div>
<div class="form-group text-center">
<input type="submit" name="submit" value="Update Details" class="btn
btn-primary">
</div>
</form>
</div>
</div>
</div>
#endsection
Your AccountController should look like this:
public function edit($request, Account $account){
return view('forms.account', [
'account' => $account,
'title' => 'Edit Account'
]);
}
public function update(AccountRequest $request, Account $account){
$account->update($request->all());
Session::flash('sm', $account->name . ' Has Been Updated');
return redirect()->back();
}
AccountRequest:
public function rules()
{
return [
'name' => 'required|min:2|max:70',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:6|max:10|confirmed',
];
}
That is about as much code as you need for this process... please read the documentation for Eloquent https://laravel.com/docs/5.4/eloquent

Resources