Laravel Controller if conditions depends on inputs fileds - php

I am trying to make a edit function for my user's where they can edit their own data, but i want it to depends on what the user will change and if the other field is not changed will be at is. you will see the code below and its not working if only one is changed but if i changed all of them the data will be changed.
Myprofile // my users view
<form class="form-group" method="POST" action="{{ route('updateprofilemany', ['id' => auth()->user()->id]) }}" >
#csrf
//username
<input class="col-md-3 container justify-content-center form-control {{$errors->has('username') ? 'has-error' : ''}}" type="text" name="username" id="username" placeholder="{{ Auth::user()->username }}" Value="{{ Request::old('username') }}" />
//bio
<textarea class="border-info form-control {{$errors->has('bio') ? 'has-error' : ''}}" type="text" name="bio" id="bio" placeholder="{{ Auth::user()->bio }}" Value="{{ Request::old('bio') }}"></textarea> <br />
<button id="btn-login" class="btn btn-md r btn-primary" type="submit" > <i class="fa fa-cog"> </i> Save Changes </button>
</form>
StudentController.php // my users controller
public function updateprofilemany(Request $request, $id)
{
// Validation
$this->validate($request, [
'username' => 'max:15|unique:Students',
'bio' => 'max:50',
]);
if ($request->has('username'))
{
// if there is a new username value
$username = $request->input('username');
} else {
$username = Auth::user()->username;
}
if ($request->has('bio'))
{
// if there is a new username value
$bio = $request->input('bio');
} else {
$bio = Auth::user()->bio;
}
// Find the user and inject the new data if there is then go back to
myprofile
$students = User::find($id);
$students->username = $username;
$students->bio = $bio;
$students->save();
//redirect
return redirect()->route('myprofile');
}
it can read the data but if I add it inside the if else statements it requires the both fields.
I tried passing default data like this
myprofile // users view
<form class="form-group" method="POST" action="{{ route('updateprofilemany', ['id' => auth()->user()->id, 'username' => auth()->user()->username, 'bio' => auth()->user()->bio]) }}" >
#csrf
//username
<input class="col-md-3 container justify-content-center form-control {{$errors->has('username') ? 'has-error' : ''}}" type="text" name="username" id="username" placeholder="{{ Auth::user()->username }}" Value="{{ Request::old('username') }}" />
//bio
<textarea class="border-info form-control {{$errors->has('bio') ? 'has-error' : ''}}" type="text" name="bio" id="bio" placeholder="{{ Auth::user()->bio }}" Value="{{ Request::old('bio') }}"></textarea> <br />
<button id="btn-login" class="btn btn-md r btn-primary" type="submit" > <i class="fa fa-cog"> </i> Save Changes </button>
</form>
Web.php // routes
// calls user update profile many function
Route::post('/updateprofilemany/{id}', [
'uses' => 'StudentController#updateprofilemany',
'as' => 'updateprofilemany',
'name' => 'updateprofilemany'
]);
Student Controller
public function updateprofilemany(Request $request, $id, $username ,$bio)
{
//functions like above
}
and add it the specific function like
public function Functionname(Request $request, $id, $username, $bio)
can you guys help me with this thank you!

When you use Value="{{ Request::old('username') }}" in your username field, it may be empty because it doesn't have any value in the first time you open that page.
And in the controller when you use if ($request->has('username')) , it returns true because the username field is sent but with empty value.
You should check the username field for empty value and then continue ...
for example :
if ($request->filled('username')) {
// do something
}

If you only want to change the data when it is in the request you can just pass a default value to the input function. This will use the value if the key is not present in the request. If the key exists but it is an empty string then you can check with a ternary statement to see if there is a value.
This is probably the most simple way to write it:
public function updateprofilemany(Request $request, $id)
{
$this->validate($request, [
'username' => 'max:15|unique:Students',
'bio' => 'max:50'
]);
// get the user
$student = User::find($id);
// just pass the default value as the old username if the key is not supplied
// and if it is an empty string it will also use the old username
$student->username = $request->input('username', $student->username)
?: $student->username;
// also pass the default here as the old bio
$student->bio = $request->input('bio', $student->bio)
?: $student->bio;
$student->save();
}

Related

Cant show old value and edit value in PHP Laravel

How to show an old value / query field from database in mysql, and edit value in Laravel. I'm using Laravel 9x and PHP 8x
Controller.php :
public function edit(Business $business)
{
return view('dashboard.bisnis.edit', [
'item' => $business
]);
}
public function update(Request $request, Business $business)
{
$rules = [
'deskripsi' => 'required|max:255',
'pemilik' => 'required|max:255'
];
$validateData = $request->validate($rules);
Business::where('id', $business->id)->update($validateData);
return redirect('/dashboard/bisnis')->with('success', 'Item has been updated !');
}
Blade.php:
#extends('dashboard.index')
#section('container')
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Edit Data Bisnis</h1>
</div>
<div class="col-lg-8">
<form method="post" action="/dashboard/bisnis/{{ $item->id }}" class="mb-5" enctype="multipart/form-data">
#method('put')
#csrf
<div class="mb-3">
<label for="deskripsi" class="form-label">Deskripsi</label>
<input type="text" class="form-control #error('deskripsi') is-invalid #enderror" id="deskripsi" name="deskripsi" required autofocus
value="{{ old('deskripsi', $item->deskripsi) }}">
#error('deskripsi')
<div class="invalid-feedback">
{{ $message }}
</div>
#enderror
</div>
<div class="mb-3">
<label for="pemilik" class="form-label">Pemilik</label>
<input type="text" class="form-control #error('pemilik') is-invalid #enderror" id="pemilik" name="pemilik" required autofocus
value="{{ old('pemilik', $item->pemilik) }}">
#error('pemilik')
<div class="invalid-feedback">
{{ $message }}
</div>
#enderror
</div>
<button type="submit" class="btn btn-primary">Simpan Perubahan</button>
</form>
</div>
<script>
const deskripsi = document.querySelector('#deskripsi');
const pemilik = document.querySelector('#pemilik');
</script>
#endsection
Also when navigating through my menu such as Business, the sidebar seems cant to be clicked, nor use. Thank you so much
Please try like this:
{{ old('deskripsi') ? old('deskripsi') :$item->deskripsi }}
Please replace this:
return redirect('/dashboard/bisnis')->with('success', 'Item has been updated !');
to
return redirect()->back()->with('success', 'Item has been updated !');
I assume you use Laravel 9
Referring to Repopulating Forms - Validation
Controller.php:
you should use
$deskripsi = $request->old('deskripsi');
$pemilik = $request->old('pemilik');
before
$validateData = $request->validate($rules);
Blade.php:
you should use this on input
value="{{ old('deskripsi') }}"
value="{{ old('pemilik') }}"
By default old will return null if no input exists so we don't need to use nullcheck like
{{old('deskripsi') ?? ''}}
To repopulate value using old() in Laravel you need to return a response withInput(). Not just response.
The return code should
return redirect('/dashboard/bisnis')->with('success', 'Item has been updated !');
change to this
return redirect('/dashboard/bisnis')->with('success', 'Item has been updated !')->withInput();
The solution is i forgot to pass my $id on my controller and route (web.php). Here's my route
Route::controller(GroupServiceController::class)->middleware('auth')->group(function () {
Route::get('/dashboard/gruplayanan', 'index');
Route::get('/dashboard/gruplayanan/create', 'create')->name('gruplayanan.create');
Route::post('/dashboard/gruplayanan', 'store')->name('gruplayanan.store');
Route::get('/dashboard/gruplayanan/edit/{id}', 'edit')->name('gruplayanan.edit');
Route::post('/dashboard/gruplayanan/update/{id}', 'update')->name('gruplayanan.update');
Route::post('/dashboard/gruplayanan/delete/{id}', 'destroy')->name('gruplayanan.delete');
});
and my controller :
public function edit(GroupService $groupService, $id)
{
$groupService = $groupService->findOrFail($id);
return view('dashboard.gruplayanan.edit', [
'item' => $groupService
]);
}

update checkbox value in laravel 8 (blade/ controller )

I'm beginner in laravel and I want to update multiple checkboxes in database ..
when I click at update button automatically my inputs show old value also my permissions are checked by old value to update it ..
relation between user and permission is manytomany .. I have another table named userpermissions who has id_user and id_permission
this is my update form in ( edit.blade.php)
<form action="{{ url('users/'.$user->id) }}" method="POST">
#csrf
#method('PUT')
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" id="name" required class="form-control" value="{{ $user->name }}">
#error('name')
<ul class="alert"><li class="text-danger">{{ $message }}</li></ul>
#enderror
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Email</label>
<input type="email" name="email" id="email" required class="form-control" value="{{ $user->email }}">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
#foreach($permissions as $permission)
<input type="checkbox" name="data[]" value="{{ $permission->id }}"
<?php if( in_array($permission->id, $user->userPermissions->pluck('permission_id')->toArray())){ echo 'checked="checked"'; } ?>/>
{{ $permission->name }}
#if($loop->iteration % 3 == 0 ) <br> #else #endif
#endforeach
</div>
</div>
</div>
<div class="text-right mt-4">
<button type="submit" class="btn btn-primary"> Add</button>
</div>
</form>
and this is my controller where I think have a problem with methods :
edit function
public function edit(User $user)
{
$permissions = Permission::get();
return view('users.edit', compact('user','permissions'));
}
update function :
public function update(UserRequest $request,User $user)
{
$user->update(
$request->only('name', 'email')
);
$user->userPermissions()->save($request->input('data'));
return redirect()->back()->with('status','user updated !');
}
and this is my functio store :
public function store(UserRequest $request)
{
$this->validate($request, [
'name' => 'required',
'email'=>'required|email',
'password' => 'required|confirmed|min:6',
]);
$user = User::create(
$request->only('name', 'email', 'password')
);
$user->userPermissions()->createMany($request->input('data'));
return redirect()->back()->with('status','Utilisateur ajouté !');
}
Thanks for advance !
$user->userPermissions()->save($request->input('data'));
One important thing to understand here, is that save() on relation doesn't remove old values from pivot table, it just add more values to it(no distinction check). You need something like refresh functionality. Look at attaching\detaching or sync, second one is more convenient.
In first case before saving permissions you can do this
// remove all old permissions
$user->userPermissions()->detach();
// update them with new one
$user->userPermissions()->attach($request->input('data'));
In second case, which is less verbose then first one you just need to pass and array of permissions to user object.
// this will do both things which we did before
$user->userPermissions()->sync($request->input('data'))
But i encourage you to read the docs and ask questions after ;)
Another thing which i saw and its not related to the current topic is
$user->userPermissions->pluck('permission_id')->toArray()
you are using lazy load inside of foreach loop which means that on each iteration of the loop you are making a query to the database(N + 1 problem). You can preload/eager load userPermissions instead of loading them on a fly by declaring with relation in your User model like this
class User extends Model
{
/**
* The relationships that should always be loaded.
*
* #var array
*/
protected $with = ['userPermissions'];
...
}
and then in your User object will have userPermissions property which you can compare to permissions.
Hope that you get main idea and info was useful for you!

Autofill the title of website via link in a form

<form method="POST" action="{{ route('storeCompany') }}">
#csrf
<label>{{ __('Website URL') }}</label>
<input type="text" name="url" value="{{ old('url') }}" placeholder="{{ __('http://example.com') }}" class="form-control" required="required">
<label>{{ __('Website Title') }}</label>
<input type="text" name="name" value="{{ old('name') }}" placeholder="{{ __('Example Ltd') }}" class="form-control" required="required">
<input type="submit" name="sbNewReviewItem" class="btn btn-block btn-primary" value="{{ __('Submit New Company') }}">
</form>
I want the "Website Title" field to be auto-filled when the user types the "Website URL" also "Website Title" must be editable if the user want to. How can I do this? Please help :(
Example: When the user enters https://stackoverflow.com/ URL in the "Website URL" field "Website Title" filed must auto-filled as Stack Overflow - Where Developers Learn, Share, & Build Careers
You have a change event on the url-field element which does a fetch request to an api website that returns the html contents of that website, and then it uses regex to extract the html title and then it adds it to the name field.
Also while the fetching is executing we show a Fetching text to let the user know what's going on. And the form submit event has been stopped with the preventDefault() function. Here you can do what ever you want after the user submits the form.
Instead of using https://api.codetabs.com/v1/proxy/?quest= which will limit your requests and show you a Too many requests error, you should use your own code that fetches the website contents and then return the response. To optimize this you can return just the website title, to save on bandwidth and cpu cycles.
The reason you need an api to fetch the website content is because of CORS. You can't just use fetch resources from any website you want. You can only do this from the current website or from websites that have allowed you to do so.
document.addEventListener('submit', event => {
event.preventDefault()
})
document.addEventListener('change', async (event) => {
if (event.target.classList.contains('url-field')) {
await changeUrl(event.target.closest('form').elements)
}
})
async function changeUrl (fields) {
try {
if (!fields.url.value) return
fields.output.removeAttribute('hidden')
const response = await fetch('https://api.codetabs.com/v1/proxy/?quest=' + encodeURI(fields.url.value))
const html = await response.text()
const match = /<title[\s\S]*?>([\s\S]*?)<\/title>/gi.exec(html)
if (!match || !match[1]) throw new Error('No title found')
fields.name.value = match[1].trim()
fields.output.setAttribute('hidden', '')
} catch (error) {
console.error(error)
}
}
<form method="POST" action="">
<p>
<label>Website URL</label>
<input type="url" name="url" value="" placeholder="http://example.com" class="form-control url-field" required>
<output name="output" hidden>Fetching</output>
</p>
<p>
<label>Website Title</label>
<input type="text" name="name" value="" placeholder="Example Ltd" class="form-control">
</p>
<input type="submit" name="sbNewReviewItem" class="btn btn-block btn-primary" value="Submit New Company">
</form>
Because you're example look like Laravel.
I found it appropriate to write an answer in PHP.
example.blade.php
<title>{{ $title ?? 'Default title' }}</title>
<form method="POST" action="{{ route('storeCompany') }}">
#csrf
<label>{{ __('Website URL') }}</label>
<input type="text" name="url" value="{{ $url ?? old('url') }}" placeholder="{{ __('http://example.com') }}" class="form-control" required="required">
<label>{{ __('Website Title') }} <small>(Leave blank to retrieve from URL)</small></label>
<input type="text" name="name" value="{{ $title ?? old('name') }}" placeholder="{{ __('Example Ltd') }}" class="form-control">
<input type="submit" name="sbNewReviewItem" class="btn btn-block btn-primary" value="{{ __('Submit New Company') }}">
</form>
CompanyController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class CompanyController extends Controller
{
public function index(){
return view('example');
}
public function storeCompany(Request $request){
$title = $request->name;
if (empty($title) && filter_var($request->url, FILTER_VALIDATE_URL)){
$response = HTTP::get($request->url);
if ($response->ok()){
preg_match('/<title>(.*)<\/title>/i', $response->body(), $matches);
$title = $matches[1] ?? null;
}
}
$data = [
'url' => $request->url,
'title' => $title
];
return view('example', $data);
}
}
As soon as user has entered the data.
Get the value using $("#text_field").val();
Have an ajax call and get the value using the below function, pass the url to t
require 'simple_html_dom.php';
function dataParser($url){
$html = new simple_html_dom();
$html->load_file($url);
$title = $html->find('title',0)->innertext;
return $title;
}
// something like $_GET['user_url'];
echo $title = dataParser('https://stackoverflow.com');
this code will output the title, set it to the div where you want to show the title, something like this
var ajaxCallResponse = ""//store the response here
$("#title_div").val(ajaxCallResponse);
Here is how to:
Read the website url.
Send an ajax request to codetabs api to get full html.
Get the title from the html.
Do whatever you need with the title.
function gettitle(website) {
if(website.trim() == ''){
return false;
}
var url = 'https://api.codetabs.com/v1/proxy/?quest=' + website;
$.ajax({
url: url,
success: function(responseHtml) {
var newTitle = $(responseHtml).filter('title').text();
document.getElementById('title').innerHTML = 'Title: ' + newTitle
},
error: function() {
document.getElementById('title').innerHTML = newTitle = 'Sorry that page doesn\'t';
}
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input placeholder='Website' oninput='gettitle(this.value)' type='url' id='url'>
<div id='title'></div>

Password reset for first user in laravel

I am trying to create a reset option in my project. If user is doing their first login, it will redirect to reset page.
This is the view part
<div class="form-group{{ ($errors->has('cpassword')) ? 'has error' : '' }}">
<label for="cpassword">Current Password: </label>
<input id="cpassword" name="cpassword" type="text" class="form-control">
#if($errors->has('cpassword'))
{{ $errors->first('cpassword')}}
#endif
</div>
<div class="form-group{{ ($errors->has('password')) ? 'has error' : '' }}">
<label for="password">New Password: </label>
<input id="password" name="password" type="password" class="form-control">
#if($errors->has('password'))
{{ $errors->first('password')}}
#endif
</div>
<div class="form-group{{ ($errors->has('password2')) ? 'has error' : '' }}">
<label for="password2">Confirm Password: </label>
<input id="password2" name="password2" type="password" class="form-control">
#if($errors->has('password2'))
{{ $errors->first('password2')}}
#endif
</div>
{{ Form::token() }}
<div class="form-group">
<input type="submit" value="Submit" class="btn btn-default">
</div>
in reset page we need to enter old password, new password and confirm password..
Controller part is given below
public function postReset(){
$validator =Validator::make(Input::all(), array(
'cpassword' => 'required',
'password' => 'required|min:8',
'password2' => 'required|min:8|same:password'
));
if ($validator->fails())
{
return Redirect::route('resetPassword')->withErrors($validator)->withInput();
}
else
{
if (Auth::attempt(array('username'=>Auth::user()->username, 'password'=>Hash::make(Input::get('cpassword'))))) {
return 'password is resetted';
}
}
}
But if I tried to verify current password and userpasssword, their hash codes doesn't match. Is there any other methods to reset passord. I need the same view part.
Can anyone help??
The Auth::attempt() method needs the plain password you must not generate a hash yourself. Also Laravel's hashes can't be validated just by comparing them because every hash contains a random salt. To compare a password with it's hash you have to use Hash::check(). Also, while Auth::attempt() works Auth::validate() would be the better choice since your user is already logged in:
$credentials = array(
'username' => Auth::user()->username,
'password' => Input::get('cpassword')
);
if(Auth::validate($credentials)){
return 'password is resetted';
}
Laravel has a "forgot" password feature built in which you can use to let people reset passwords if they have forgot them.
You can find that at: http://laravel.com/docs/5.0/authentication
If you want someone to be able to change their password (and they are able to remember the old one), you can do something like this:
if(Input::get('passwordIs')) {
if (!Hash::check(Input::get('passwordIs'), Auth::user()->password))
return Redirect::to('my-account/settings/')->withErrors(Lang::get('account.password-wrong'));
else {
if(Input::get('passwordIsNew') !== Input::get('passwordIs_confirmation'))
return Redirect::to('my-account/settings/')->withErrors(Lang::get('account.passwords-must-match'));
else{
$password = Hash::make(Input::get('passwordIs_confirmation'));
$customer = Auth::user();
$customer->password = $password;
$customer->save();
}
}
}

How to make a Sign-in function in Laravel 4?

I have 3 user types :
Admin
Distributor
Internal
I have a problem sign in as user type. ( Internal )
I can sign in when my user type is Admin.
I can sign in when my user type is Distributor.
BUT I can’t sign in when my user type is internal. Wired ????
I look through every single line of my code in my sign-in functions in my AccountController.php.
I didn’t notice any bug there. If you guys notice any bugs there -- please kindly let me know.
That will be a huge help for me.
Here is my Sign-In Functions
GET
public function getSignIn(){
return View::make('account.signin');
}
POST
public function postSignIn() {
$validator = Validator::make( Input::only('username','password'),
array(
'username' =>'required',
'password' =>'required'
)
);
if ($validator->fails()) {
return Redirect::route('account-sign-in-post')
->with('error','Username/Password Wrong or account has not been activated !')
->withErrors($validator);
}
// Remember Me
$remember = (Input::has('remember')) ? true : false ;
$auth = Auth::attempt(array(
'username' => strtolower(Input::get('username')),
'password' => Input::get('password'),
'active' => 1),
$remember);
// Keep track on log-in information of the user.
if(Auth::check()){
$user = Auth::user();
$user->last_logged_in = Input::get('created_at');
$user->logged_in_count = $user->logged_in_count + 1 ;
$user->is_online = '1';
$user->save();
}
if($auth) {
return Redirect::to('/')
->with('success','You have been successfully logged in.')
;
}
else {
return Redirect::route('account-sign-in')
->with('error','Username/Password Wrong or account has not been activated !')
->withInput(Input::except('password'))
->withErrors($validator);
}
}
VIEW
#extends ('layouts.form')
#section('content')
<style type="text/css">
.signin{
text-align: center;
}
#forgetpassword{
/*color:#5cb85c;*/
color:silver;
}
</style>
<form class="form-horizontal" role="form" action=" {{ URL::route('account-sign-in-post')}}" method="post" >
#if ($errors->has('username')){{ $errors->first('username')}} #endif
<div class="form-group">
<label for=""> Email </label>
<input placeholder="Email" type="text" class="form-control" required name="username" {{ ( Input::old('username')) ? 'value="'.e(Input::old('username')).'"' : '' }}>
</div><br>
#if ($errors->has('password')){{ $errors->first('password')}} #endif
<div class="form-group">
<label for=""> Password </label>
<input placeholder="Password" type="password" class="form-control" required name="password">
</div><br>
<br>
<button type="submit" class="btn btn-success btn-sm btn-block ">Sign In </button>
{{ Form::token() }}
</form><br>
<div class="col-lg-12 text-center">
<a id="forgetpassword" href="{{ URL::route('account-forgot-password') }}"> Forget Password </a> <br>
</div>
#stop
I am sure that I typed in the right username and password because I double check with my database.
It keep redirecting me back to my sign-in page.
with('error','Username/Password Wrong or account has not been activated !')
Can someone please tell me, if I did anything that I’m not suppose to ?
In your situation, you should check your auth variable in your Sign_In Function.
According to your code,
$auth = Auth::attempt(array(
'username' => strtolower(Input::get('username')),
'password' => Input::get('password'),
'active' => 1),
$remember);
Keep in mind that, these are things need to make sure
username must match the database
password must match the database
user active must be 1
If any of these fail, therefore, it STOP you from signing in.
Since, you're so sure about username and password, what about user active ?
Did you check to if it's 1 ?
If Not
on your set-password function or anywhere, where you normally set your user active.
just do this :
$user->active = '1';
$user->save();
Let me know if this work!!

Categories