hello i have two tables users and profiles and i have a form to update the user profile, whene i try to update, it's just refresh the page but nothing was updated, and there is no errors
this is my code :
ProfilesControllers:
public function update(Request $request)
{
$this->validate($request,[
'name'=>'required',
'email'=>'required|email',
'about'=>'required',
'facebook'=>'required|url',
'instagram'=>'required|url',
]);
$user = Auth::user();
if($request->hasFile('avatar')){
$avatar = $request->avatar;
$avatar_new_name = time() . $avatar->getClientOriginalName();
$avatar->move('uploads/avatars', $avatar_new_name);
$user->profile->avatar = 'uploads/avatars'.$avatar_new_name;
$user->profile->save();
}
$user->name = $request->name;
$user->email = $request->email;
$user->profile->facebook = $request->facebook;
$user->profile->instagram = $request->instagram;
$user->profile->about = $request->about;
if($request->has('password')){
$user->password=bcrypt($request->password);
$user->save();
}
$user->save();
$user->profile->save($user->profile);
return redirect()->back();
}
profile.blade.php :
<form action="{{ route('user.profile.update')}}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
/// here is my input fields
</form>
and i'm using Laravel : 6.20.5
could someone tell me where is the problem in my code? thanks
you could try this:
instead of $avatar = $request->avatar;
use
$avatar = $request->file('avatar');
and instead of $avatar->move('uploads/avatars', $avatar_new_name);
use
$request->file('avatar')->storeAs('uploads/avatars', $avatar_new_name);
and when using back() there's no need for redirect(). back() generates an HTTP redirect.
so
return back();
Related
After register redirect to this page.
I want enter this code. (Of course mobile number and the code save in database and send a sms to mobile).
How to enter session code number?
Code
web.php
Route::get('/code', 'HomeController#code')->name('code');
Route::post('/send', 'HomeController#send')->name('send');
RegisterController.php
public function register(Request $request, User $user)
{
$code = rand(10000,99999);
$user = \App\User::create([
'first_name' => $request->first_name,
.
.
.
.
return redirect()->route('code',['mobile'=>$request->mobile]);
HomeController.php
public function code()
{
return view('Home.send');
}
public function send(Request $request)
{
$mobile = $request->session()->get('mobile');
$user = User::whereCode($request->code)
->whereMobile($mobile)
->firstOrFail();
if($user){
$user->verification_code = 1;
$user->save();
alert()->success('ok');
return redirect()->back();
} else {
alert()->error('error');
return redirect()->back();
}
}
send.blad,php
<form action="{{ route('send') }}" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="code">Code</label>
<input type="text" class="form-control" name="code" id="code">
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger" id="btn-ok">OK</button>
</div>
</form>
I get this error
404
I have verification_code in users table. I want In case of enter true code and mobile number, verification_code change to 1
Take a look on Retrieving Single Models - Not Found Exceptions.
The firstOrFail method will retrieve the first result of the query;
however, if no result is found, a
Illuminate\Database\Eloquent\ModelNotFoundException will be thrown.
If the exception is not caught, a 404 HTTP response is automatically sent
back to the user. It is not necessary to write explicit checks to
return 404 responses when using these methods.
So, you can try catch the exeption:
try {
$mobile = $request->session()->get('mobile');
$user = User::whereCode($request->code)
->whereMobile($mobile)
->firstOrFail();
$user->verification_code = 1;
$user->save();
} catch (Illuminate\Database\Eloquent\ModelNotFoundException $e) {
return redirect()->back()->with('alert', 'error');
}
return redirect()->back()->with('success', 'ok');
Or otherwise use first instead of firstOrFail, which will return null if there is no record that meets the where conditions:
$mobile = $request->session()->get('mobile');
$user = User::whereCode($request->code)
->whereMobile($mobile)
->first();
if(empty($user)){
return redirect()->back()->with('alert', 'error');
}
$user->verification_code = 1;
$user->save();
return redirect()->back()->with('success', 'ok');
I need to return message but I get an error.
My controller:
$user = User::findOrFail($id) ;
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = $request->input('password');
$user->role_id = $request->input('role_id');
if($user->update()){
return new UserResource($user);
}
I tried:
return new UserResource($user)->with('status', 'Profile updated!');
You have to place the creation of the UserResource instance between brackets so php knows you are using the with function on this new instance.
return (new UserResource($user))->with('status', 'Profile updated!');
I was applying Spatie Laravel-permissions and coding to add permissions to roles.
and faced a error.
public function store(Request $request) {
//Validate name and permissions field
$this->validate($request, [
'name'=>'required|unique:'.config('permission.table_names.roles').'|max:10',
]
);
$name = $request['name'];
$role = new Role();
$role->name = $name;
$permissions = $request['permissions'];
$role->save();
//Looping thru selected permissions
if(count($permissions>0)){
foreach ($permissions as $permission) {
$p = Permission::where('id', '=', $permission)->firstOrFail();
//Fetch the newly created role and assign permission
$role = Role::where('name', '=', $name)->first();
$role->givePermissionTo($p);
}
}
return redirect()->route('roles.index')
->with('flash_message',
'Role'. $role->name.' added!');
}
So if someone please help me solve this problem.
In your submit form use this
<input type="checkbox" name="permissions[]" value="{{ $r->id }}"> {{$r->display_name}} </li>
In controller catch these permissions like this
$permissions = $request->permissions;
I am having trouble understanding and knowing how to create a one to one relationship with CRUD and file upload together, is it possible to give me a simple example on how to do it? Because when I tried to do it, for some reason my id and user_id doesn't match at all
Example, in user_info table (jack with id 1) has one userImage table(image uploaded with a user_id = 1).
This is the part that I am having trouble with in my controller:
public function store1(Request $request){
$this->validate($request, [
'input_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$user_info = Session::get('data');
$UserImage = new UserImage($request->input()) ;
if($file = $request->hasFile('input_img')) {
$file = $request->file('input_img');
$fileName = $file->getClientOriginalExtension() ;
$destinationPath = public_path().'/images' ;
$file->move($destinationPath,$fileName);
$UserImage->userImage = $fileName ;
$UserImage = UserImage::create(['file' => $request->file('input_img')]);
$UserImage->user_infos()->associate($user_info);
}
$UserImage->save() ;
//dd($UserImage);
return redirect('/home');
}
Sample of one to one relationship. I wish it can help you.
Example of table of Column of user table:
id
name
email
password
Example of table of Column of userimage table:
id
user_id
image_link
uploaded
User Model
public function userImage()
{
return $this->hasOne(App\UserImage::class);
}
UserImage Model
public function user()
{
return $this->hasOne(App\User::class);
}
CRUD in controller
public function store(Request $request)
{
$user = new App\User;
$user->name = $request->name;
$user->email = $request->email;
$user->password = $request->password;
$user->save();
$user->userImage->create([
'image_link' => $request->image,
'uploaded' => $request->date,
]);
return back();
}
public function update(Request $request, $id)
{
$user = App\User::find($id);
$user->name = $request->name;
$user->email = $request->email;
$user->password = $request->password;
$user->save();
$user->userImage->image_link = $request->image;
$user->userImage->uploaded = $request->date;
$user->userImage->save();
return back();
}
public function destroy($id)
{
$user = App\User::find($id)->delete();
return back();
}
Create a foreign key in your user_info table referencing userImage table.
Create unique constraint on the foreign key.
My route to load the sign up page is
Route::get('join/{mobileNumber}', 'JoinController#index');
Is there any way to pass the variable 'mobileNumber' when submitting the form ?
I can create a hidden input in the form, but would it be the best way to further pass on the variable ?
Yes, passing the variable from the index method to the view:
routes.php
Route::get('join/{mobileNumber}', 'JoinController#getJoin');
Route::post('join/{mobileNumber}', 'JoinController#postJoin');
JoinController
<?php
# ...
public function getJoin($mobileNumber)
{
# ...
return view('mobile_join', compact('mobileNumber'));
}
public function postJoin($mobileNumber, RegisterRequest $request)
{
$user = new User();
$username = $request->input('username');
$user->password = bcrypt($request->input('password'));
$user->email = $request->input('email');
$user->phone = $mobilePhone;
$user->is_active = 0;
$user->save();
# or
$data = $request->only(['username', 'email']);
$data['phone'] = $mobilePhone;
$data['password'] = bcrypt($data['password']);
$data['is_active'] = 0;
$user = User::create($data);
# ...
redirect()->route('...');
}
User.php - Model
protected $fillable = ['username', 'email', 'phone', 'password', 'is_active'];
mobile_join.blade.php
<form action="/join/{{ $mobileNumber }}" method="post">
{!! csrf_field() !!}
<!-- ... -->
</form>
HTTP 1.1 supports two methods, GET and POST, and we are sending the form data through the POST and passing the mobileNumber through the GET. Laravel uses regular expression to extract the string from the URL and set the variable.