Bleaching the page when submitting the form with livewire - laravel

In login form i used livewire and when i out focus of phone field my webpage its been white
please help me thanks <3
view code:
<form wire:submit.prevent="login">
<fieldset class="form-label-group form-group position-relative has-icon-left">
<input wire:model.debounce.500ms="phone" type="text" class="form-control" id="phone" placeholder="شماره تماس">
<div class="form-control-position">
<i class="feather icon-phone"></i>
</div>
<label for="phone">شماره تماس</label>
</fieldset>
#error('phone') <span class="text-danger">{{ $message }}</span> #enderror
<button type="submit" class="btn btn-primary float-left btn-inline" wire:click.prevent="login">ورود</button>
</form>
controller code:
class Login extends Component
{
public $phone;
protected $rules = [
'phone' => ['required', 'regex:/^09(1[0-9]|3[1-9]|2[1-9])-?[0-9]{3}-?[0-9]{4}$/', 'exists:users,phone']
];
public function updatedPhone($name)
{
$this->validate($name);
}
public function login(Request $request)
{
$validData = $this->validate();
$user = User::wherePhone($validData['phone'])->first();
dd($user);
$request->session()->flash('auth', [
'user_id' => $user->id,
'remember' => $request->has('remember'),
]);
$code = ActiveCode::generateCode($user);
$user->notify(new ActiveCodeNotification($code, $user->phone));
dd('done');
}
public function render()
{
return view('livewire.auth.login')
->layout('livewire.auth.layouts.layouts');
}
}

Your updatePhone method is supposed to receive $value from input binded with wire:model so you have to pass a key-value to validate method:
public function updatePhone($value)
{
$this->validate($this->rules); // protected prop with key-value rules defined
}

Related

Can't upload files using livewire

I can't submit a form with file in order to proced to upload method, the file is selected when I submit it says that the file is required (empty data).
Everything works fine on mylocal Windows machine but I face the problem when using vps for production.
view :
<form wire:submit.prevent="submit" enctype="multipart/form-data">
<div>
#if(session()->has('message'))
<div class="alert alert-success">
{{ session('message') }}
</div>
#endif
</div>
<div class="form-group">
<label for="exampleInputName">Title:</label>
<input type="text" class="form-control" id="exampleInputName" placeholder="Enter title" wire:model="title">
#error('title') <span class="text-danger">{{ $message }}</span> #enderror
</div>
<div class="form-group">
<label for="exampleInputName">File:</label>
<input type="file" class="form-control" id="exampleInputName" wire:model="file">
#error('file') <span class="text-danger">{{ $message }}</span> #enderror
</div>
<button type="submit" class="btn btn-success">Save</button>
</form>
controller :
use WithFileUploads;
public $file, $title;
public function submit()
{
$validatedData = $this->validate([
'title' => 'required',
'file' => 'required',
]);
$validatedData['name'] = $this->file->store('files', 'public');
// File::create($validatedData);
session()->flash('message', 'File successfully Uploaded.');
}
VPS folders :
I tried to change permessions, user group.... no success.
try this
use WithFileUploads;
public $title;
public $file;
protected function rules()
{
return [
'title' => ['required', 'string', 'max:50'],
'file' => ['required', 'file', 'mimes:pdf,doc,docx', 'max:5000']
];
}
public function submit()
{
$this->validate();
$data = [
'title' => $this->title
];
if (!empty($this->file)) {
$url = $this->file->store('files', 'public');
$data['file'] = $url;
}
File::create($data);
session()->flash('message', 'File successfully Uploaded.');
}

laravel 8 Sorry! You have entered invalid credentials

I am using a custom Authentication in Laravel 8 and whenever I try to Enter a valid email/password am getting this error: Sorry! You have entered invalid credentials.
here is my code:
1#Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Session;
use App\Models\User;
use Hash;
use Validator;
class AuthController extends Controller
{
/**
* Write code on Method
*
* #return response()
*/
public function index()
{
return view('auth.login');
}
/**
* Write code on Method
*
* #return response()
*/
public function registration()
{
return view('auth.register');
}
/**
* Write code on Method
*
* #return response()
*/
public function postLogin(Request $request)
{
$request->validate([
'email' => 'required',
'password' => 'required',
]);
$credentials = $request->only('email', 'password');
if (Auth::attempt($credentials)) {
return redirect()->intended('dashboard')
->withSuccess('You have Successfully logged in');
}
return redirect("login")->withSuccess('Sorry! You have entered invalid credentials');
}
/**
* Write code on Method
*
* #return response()
*/
public function postRegistration(Request $request)
{
$request->validate([
'name' => 'required',
'username' => 'required',
'email' => 'required|email|unique:users',
'password' => 'required|min:6',
]);
$data = $request->all();
$check = $this->create($data);
return redirect("login")->withSuccess('Great! please login.');
}
/**
* Write code on Method
*
* #return response()
*/
public function dashboard()
{
if(Auth::check()){
return view('dashboard');
}
return redirect("login")->withSuccess('Opps! You do not have access');
}
/**
* Write code on Method
*
* #return response()
*/
public function create(array $data)
{
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password'])
]);
}
/**
* Write code on Method
*
* #return response()
*/
public function logout() {
Session::flush();
Auth::logout();
return Redirect('login');
}
}
2#Login.blade.php:
#extends('layouts.layout')
#section('content')
<div class="login-form">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Login</div>
<div class="card-body">
#if (Session::get('success'))
<div class="alert alert-success" role="alert">
{{ Session::get('success') }}
</div>
#endif
<form action="{{ route('login.post') }}" method="POST">
#csrf
<div class="form-group row">
<label for="email_address" class="col-md-4 col-form-label text-md-right">Email Address</label>
<div class="col-md-6">
<input type="text" id="email_address" class="form-control" name="email" required />
#if ($errors->has('email'))
<span class="text-danger">{{ $errors->first('email') }}</span>
#endif
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">Password</label>
<div class="col-md-6">
<input type="password" id="password" class="form-control" name="password" required />
#if ($errors->has('password'))
<span class="text-danger">{{ $errors->first('password') }}</span>
#endif
</div>
</div>
<div class="form-group row">
<div class="col-md-6 offset-md-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember"> Remember Me
</label>
</div>
</div>
</div>
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
Login
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
For registration it's working fine, the only problem is in Login form. Hope you can help.
First step: check your user model extends from Authenticable that main user model uses,
Second step:use bcrypt instead of Hash::make,
If solutions doesnt work send your model and config/auth.php for better answer

Profile Not Updating In Laravel Livewire

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 = [];

Livewire doesn't show value from mutator

I'm trying to bind a date field (mydate) with laravel livewire. Basically I did the following:
In the model, I did a cast and created a mutator and an accessor:
<?php
namespace App\Models\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Platform extends Model
{
use HasFactory;
protected $fillable = ['name', 'mydate'];
protected $dates = ['created_at', 'updated_at', 'mydate'];
protected $casts = ['mydate' => 'datetime'];
public function getCreatedAtBrAttribute()
{
return optional($this->created_at)->format('d/m/Y H:i');
}
public function getUpdatedAtBrAttribute()
{
return optional($this->updated_at)->format('d/m/Y H:i');
}
public function getMydateBrAttribute()
{
return optional($this->mydate)->format('Y-m-d\TH:i');
}
public function setMydateBrAttribute($value)
{
$this->mydate = Carbon::parse($value)->format('Y-m-d H:i:s');
}
}
In the component I added the field in rules:
<?php
namespace App\Http\Livewire;
use App\Models\Models\Platform;
use Livewire\Component;
class PlatformForm extends Component
{
public Platform $platform;
protected $listeners = ['recordToEdit' => 'editPlatform', 'newRecord' => 'createPlatform'];
protected $rules = [
'platform.id' => 'sometimes',
'platform.name' => 'sometimes',
'platform.created_at' => 'sometimes',
'platform.updated_at' => 'sometimes',
'platform.mydate_br' => 'sometimes',
];
public function mount() {
$this->platform = $this->makeBlankPlatform();
}
public function makeBlankPlatform()
{
return Platform::make();
}
public function editPlatform(Platform $platform)
{
$this->platform = $platform;
}
public function createPlatform()
{
$this->platform = new Platform();
$this->platform->created_at = now();
}
public function save()
{
$this->platform->id
? $this->platform->updated_at = now()
: $this->platform->updated_at = $this->platform->created_at;
$this->platform->save();
$this->emit('refresh');
}
public function render()
{
return view('livewire.platform-form');
}
}
And in the view I added the wire:model, like this:
<div>
<div class="form-group">
<label for="name">Name</label>
<input wire:model.defer="platform.name" type="text" class="form-control">
</div>
<div class="form-group">
<label for="mydate_br">My date</label>
<input id="mydate_br" wire:model.defer="platform.mydate_br" type="text" class="form-control">
</div>
<div class="form-group">
<label for="created_at">Created at</label>
<input id="created_at" wire:model="platform.created_at_br" type="text" class="form-control" readonly>
</div>
<div class="form-group">
<label for="updated_at">Updated at</label>
<input id="updated_at" wire:model="platform.updated_at_br" type="text" class="form-control" readonly>
</div>
<hr class="border">
<div class="text-right">
<button type="button" class="btn btn-secondary" data-dismiss="modal">
Cancelar
</button>
<button wire:click="save" type="button" class="btn btn-primary" data-dismiss="modal">
Gravar
</button>
</div>
</div>
The problem is that the date stored in the database is not displayed in the input text, however, if I do it in the standard blade way, {{ $platform->mydate_br }}, the value is displayed, but here I lose functionality of livewire binding.
Anyone have a tip to help me resolve this?

Updating Item from Pivot Table Fields

I have item_color and item_size pivot tables, and would like to update my size and color fields using their field values, and an not exactly sure where to start. Here's what I have so far.
ItemSize.php
<?php
class ItemSize extends \Eloquent
{
protected $table = 'item_size';
protected $fillable = [];
public function item() {
return $this->belongsTo('Item');
}
}
ItemColor.php
<?php
class ItemColor extends \Eloquent
{
protected $table = 'item_color';
protected $fillable = [];
public function item() {
return $this->belongsTo('Item');
}
}
VendorController
public function postVendorUpdateItems ($id)
{
$input = Input::all();
$items = Item::find($id);
$validator = Validator::make($input,
[ 'item_name' => 'max:50',
'item_id' => 'max:50',
'normalprice' => 'numeric',
'karmaprice' => 'numeric',
'asin' => 'max:50',
]);
if($validator->passes())
{
$items->name = $input['item_name'];
$items->normalprice = $input['normalprice'];
$items->karmaprice = $input['karmaprice'];
$items->asin = $input['asin'];
$items->id = $input['item_id'];
$items->save();
return Redirect::route('account-vendor-index')
->with('global', 'You have updated your item.');
}
return Redirect::route('account-vendor-index')
->withErrors($validator)
->with('global', 'Your item could not be updated.');
}
View
<form class="form-horizontal" role="form" method="post" action="{{url('account/vendor/update/items')}}/{{$item->id}}">
<input type="hidden" id="brand_id" placeholder="brand_id" value="{{$brand->id}}" name="brand_id">
<input type="hidden" id="item_id" placeholder="item_id" value="{{$item->id}}" name="item_id">
<div class="form-group">
<label for="colors" class="col-xs-3 control-label">Colors</label>
<div class="col-xs-6">
<input type="text" class="form-control input-sm" id="colors" name="colors" placeholder="#foreach($item->colors as $color){{$color->color}}#endforeach" value="">
</div>
<button type="" class="btn btn-primary btn-sm">Add color</button>
<div class="clear"></div>
<div class="col-xs-offset-3 showColors">
#foreach($item->colors as $color)
{{$color->color}}
#endforeach
</div>
</div>
<div class="form-group">
<label class="col-xs-3 control-label">Sizes</label>
<div class="col-xs-9">
<select id="selectSizes" multiple="multiple" class="form-control selectSizes" name="sizes">
<option value="XS (0-2)">XS (0-2)</option>
<option value="S (4-6)">S (4-6)</option>
<option value="M (8-10)">M (8-10)</option>
<option value="L (12-14)">L (12-14)</option>
<option value="XL (16-18)">XL (16-18)</option>
<option value="XXL (20-22)">XXL (20-22)</option>
</select>
</div>
</div>
<div class="form-group bot-0">
<div class="col-xs-offset-3 col-xs-9">
<button type="submit" class="btn btn-main btn-sm">Save changes</button>
</div>
</div>
</form>
item_size and item_color are not pivot tables. They are in fact a one to one relationship with your schema. If want you truly want are pivot tables, you need to define a color and a size table, then create a many-to-many relationship. See here: https://laravel.com/docs/master/eloquent-relationships
Then to update related models, you'll use the attach, sync, detach, etc. methods.
Also, what version of Laravel are you running? If you're running 5.1+ look into form request validation. See here: https://laravel.com/docs/master/validation#form-request-validation
That will remove all the validation logic from your controller.

Resources