UserController.php
public function edit_profile(){
$input = Input::all();
$res = array();
$_m = "";
$rules = array(
'userName' => 'required',
'fullName' => 'required',
'shopName' => 'required',
'userType' => 'required',
'email' => 'required',
'aboutMe' =>'required',
'address' =>'required',
'city' =>'required',
'country' =>'required',
'contactNo' =>'required|regex:/[0-9]{10,11}/',
);
$validator = Validator::make($input, $rules);
if (!$validator->fails())
{
$user = User::find(Auth::user()->get()->id);
$user->fullname = $input('fullName');
$user->username = $input('userName');
$user->businessname = $input('shopName');
$user->usertype = $input('userType');
$user->email = $input('email');
$user->aboutme = $input('aboutMe');
$user->address = $input('address');
$user->city = $input('city');
$user->country = $input('country');
$user->phone = $input('contactNo');
$user->save();
$res['success'] = true;
$res['message'] = "NO";
return Redirect::to('user.profile');
}
else
{
return Redirect::to('login');
}
}
profile.blade.php
<div class="tab-pane fade" id="p2">
<h2>Account details</h2>
<div class="hr hr-12 hr-double"></div>
{{ Form::open(array('url'=>'profile','class' =>'form-horizontal')) }}
<div class="form-group">
<label class="col-sm-3 control-label">Full Name:</label>
<div class="col-sm-3">
{{ Form::text('fullName',ucwords(Auth::user()->fullname),array('class'=>'form-control', 'placeholder'=> 'Full Name Here' )) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">User Name:</label>
<div class="col-sm-3">
{{ Form::text('userName',ucwords(Auth::user()->username),array('class'=>'form-control', 'placeholder'=>'e.g ali.ali')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Shop Name:</label>
<div class="col-sm-5">
{{ Form::text('shopName',ucwords(Auth::user()->businessname),array('class'=>'form-control', 'placeholder'=>'e.g Niazi Traders')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Email</label>
<div class="col-sm-4">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
{{ Form::email('email',ucwords(Auth::user()->email),array('class'=>'form-control', 'placeholder'=>'e.g john.smith#example.com')) }}
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">User Type:</label>
<div class="col-sm-4">
<div class="input-group">
<span class="input-group-addon"></span>
{{ Form::select('userType',array('D'=>'Distributor', 'W'=>'Whole Saler','R' =>'Retailer'),ucwords(Auth::user()->usertype)) }}
</div>
</div>
</div>
<hr class="separator">
<div class="form-group">
<label class="col-sm-3 control-label">About Me:</label>
<div class="col-sm-9">
{{ Form::textarea('aboutMe',ucwords(Auth::user()->aboutme),array('class'=>'form-control','id'=>'about-editor','placeholder'=>'Some thing about yourself......')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Address:</label>
<div class="col-sm-7">
{{ Form::text('address',ucwords(Auth::user()->address),array('class'=>'form-control','placeholder'=>'')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">City:</label>
<div class="col-sm-3">
{{ Form::text('city',ucwords(Auth::user()->city),array('class'=>'form-control','placeholder'=>'e.g Islamabad')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Country:</label>
<div class="col-sm-4">
{{ Form::text('country',ucwords(Auth::user()->country),array('class'=>'form-control','placeholder'=>'e.g Pakistan')) }}
</div>
</div>
<div class="form-group">
<label class="col-sm-3 control-label">Phone Number:</label>
<div class="col-sm-3">
{{ Form::text('contactNo',ucwords(Auth::user()->phone),array('class'=>'form-control','placeholder'=>'e.g 0300xxxxxx')) }}
</div>
</div>
<div class="form-actions">
<div class="form-group">
<div class="col-sm-offset-3 col-sm-9">
{{ Form::submit('Submit',array('class'=>'btn btn-primary')) }}
{{ Form::submit('Cancel',array('class'=>'btn btn-inverse')) }}
</div>
</div>
</div>
{{ Form::close() }}
</div>
Routes.php
Route::get('logout','UserController#get_logout');
Route::get('profile', 'UserController#get_profile');
Route::post('profile', 'UserController#edit_profile');
User.php
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password', 'remember_token');
}
Error lies in this line
$user = User::find(Auth::user()->get()->id);
get is a function of Builder class used for getting a collection of record, Auth::user() returns a single user so you just need to access its id property
change above line to
$user = User::find(Auth::user()->id);
but Auth::user you give the current user so you dont need to find it again, you can access its property directly
$user = Auth::user();
Related
i'm working on a Laravel/Livewire project and my validation don't let my form to submit but errors are not shown in my blade .
i implemented my validation system same as Livewire documentation but it didn't work for me
even i tested some other ways like validator::make() but my bug didn't solved
my controller :
class Index extends Component
{
use WithPagination;
public $title, $en_title, $parent;
protected $rules = [
'title' => 'required',
'en_title' => 'required',
];
protected $paginationTheme = "bootstrap";
public function store()
{
$validate = $this->validate();
Category::create([
'title' => $this->title,
'en_title' => $this->en_title,
'parent_id' => $this->parent,
]);
$this->reset(['title', 'en_title', 'parent']);
session()->flash('add_category', 'دسته بندی با موفقیت اضافه شد');
}
}
my blade :
<form wire:submit.prevent="store" class="col-md-6">
<div>
<div class="form-group">
<label class="form-label">عنوان دسته بندی</label>
<input wire:model.defer="title" type="text" class="form-control" placeholder="نام دسته بندی">
#error('title') <div class="invalid-feedback">{{$message}}</div> #enderror
</div>
<div class="form-group">
<label class="form-label">title</label>
<input wire:model.defer="en_title" type="text" class="form-control" placeholder="عنوان انگلیسی دسته بندی">
#error('en_title') <div class="invalid-feedback">{{$message}}</div> #enderror
</div>
<div class="form-group">
<label class="form-label">دسته بندی والد</label>
<select wire:model.defer="parent" id="select-countries" class="form-control custom-select">
<option value="">بدون والد</option>
#foreach($categoriesCreate as $category)
<option value="{{$category->id}}">{{$category->title}}</option>
#if (count($category->childrenRecursive) > 0)
#include('layouts.partials', ['categories' => $category->childrenRecursive, 'level'=> 1, 'create' => 1])
#endif
#endforeach
</select>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success mt-1 mb-1">افزودن</button>
<div wire:loading wire:target="store">
<div class="loader-wrapper d-flex justify-content-center align-items-center">
<div class="loader">
<div class="ball-pulse">
<div></div>
<div></div>
<div></div>
</div>
</div>
</div>
</div>
</div>
</form>
i don't know why but i removed invalid_feedback class from my #error() in my blade that related to bootstrap and my errors are shown
i am using MacsiDigital/laravel-zoom package but the problem is Call to a member function get() on null i dont know why and i made everything correct
i am using MacsiDigital/laravel-zoom package but the problem is Call to a member function get() on null i dont know why and i made everything correct
here is my code
this is my create blade
<form method="post" action="{{ route('online_classes.store') }}" autocomplete="off">
#csrf
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="Grade_id">{{ trans('Students_trans.Grade') }} : <span
class="text-danger">*</span></label>
<select class="custom-select mr-sm-2" name="grade">
<option selected disabled>{{ trans('Parent_trans.Choose') }}...</option>
#foreach ($Grades as $Grade)
<option value="{{ $Grade->id }}">{{ $Grade->name }}</option>
#endforeach
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="Classroom_id">{{ trans('Students_trans.classrooms') }} : <span
class="text-danger">*</span></label>
<select class="custom-select mr-sm-2" name="class">
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="section_id">{{ trans('Students_trans.section') }} : </label>
<select class="custom-select mr-sm-2" name="section_id">
</select>
</div>
</div>
</div><br>
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label>عنوان الحصة : <span class="text-danger">*</span></label>
<input class="form-control" name="topic" type="text">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>تاريخ ووقت الحصة : <span class="text-danger">*</span></label>
<input class="form-control" type="datetime-local" name="start_time">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label>مدة الحصة بالدقائق : <span class="text-danger">*</span></label>
<input class="form-control" name="duration" type="text">
</div>
</div>
</div>
<button class="btn btn-success btn-sm nextBtn btn-lg pull-right"
type="submit">{{ trans('Students_trans.submit') }}</button>
</form>
and this is my fillable model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class online_class extends Model
{
protected $fillable = [
'grade_id',
'classroom_id',
'section_id',
'topic',
'start_at',
'duration',
'user_id',
'meeting_id',
'start_url',
'join_url',
'password',
];
public function user(){
return $this->belongsTo(User::class,'user_id');
}
}
and this is my store method in controller
public function store(Request $request)
{
$user = Zoom::user()->first();
$meeting = Zoom::meeting()->make([
'topic' => $request->topic,
'duration' => $request->duration,
'password' => $request->password,
'start_time' => $request->start_time,
'timezone' => 'Africa/Cairo',
]);
$meeting->settings()->make([
'join_before_host' => false,
'approval_type' => 1,
'registration_type' => 2,
'enforce_login' => false,
'waiting_room' => false,
]);
online_class::create([
'grade_id' => $request->grade,
'classroom_id' => $request->class,
'section_id' => $request->section_id,
'topic' => $request->topic,
'start_at' => $request->start_time,
'duration' => $meeting->duration,
'user_id' => Auth::user()->id,
'meeting_id' => $meeting->id,
'start_url' => $meeting->start_url,
'join_url' => $meeting->join_url,
'password' => $meeting->password,
]);
return $user->meetings()->save($meeting);
}
I got error Missing required parameter for [Route: battersecondinnings.update] [URI: battersecondinnings/{battersecondinning}] [Missing parameter: battersecondinning]. (View: C:\xampp\htdocs\ContentBaseApp - 1.0.2\resources\views\battersecondinnings\edit.blade.php)
This is my C:\xampp\htdocs\ContentBaseApp - 1.0.2\resources\views\battersecondinnings\edit.blade.php
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Edit Batter</h2>
</div>
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('battersecondinnings.index') }}"> Back</a>
</div>
</div>
</div>
#if ($errors->any())
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('battersecondinnings.update', $battersecondinnings->id) }}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
<div class="row">
<div class="form-group row">
<div class="col-md-6">
<strong>ব্যাটসম্যান:</strong>
<input type="text" name="name" value="{{ $battersecondinnings->name }}" class="form-control" placeholder="ব্যাটসম্যান">
</div>
</div>
<div class="form-group row">
<div class="col-md-6">
<strong>রান:</strong>
<input type="number" name="runs" value="{{ $battersecondinnings->runs }}" class="form-control" placeholder="রান">
</div>
</div>
<div class="form-group row">
<div class="col-md-6">
<strong>বল:</strong>
<input type="number" name="balls" value="{{ $battersecondinnings->balls }}" class="form-control" placeholder="বল">
</div>
</div>
<div class="form-group row">
<div class="col-md-6">
<strong>ছক্কা:</strong>
<input type="number" name="sixs" value="{{ $battersecondinnings->sixs }}" class="form-control" placeholder="ছক্কা">
</div>
</div>
<div class="form-group row">
<div class="col-md-6">
<strong>চার:</strong>
<input type="number" name="fours" value="{{ $battersecondinnings->fours }}" class="form-control" placeholder="চার">
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
#endsection
This is my BattersecondiningsController.php
public function edit(Battersecondinnings $battersecondinnings)
{
return view('battersecondinnings.edit',compact('battersecondinnings'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param \App\Models\Battersecondinnings $battersecondinnings
* #return \Illuminate\Http\Response
*/
public function update(Request $request, Battersecondinnings $battersecondinnings)
{
$request->validate([
'name' => 'required',
'runs' => 'required',
'balls' => 'required',
'sixs' => 'required',
'fours' => 'required',
]);
$battersecondinnings->update($request->all());
return redirect()->route('battersecondinnings.index')
->with('success','Batter second innings updated successfully');
}
But same things is working in products/edit.blade.php
<form action="{{ route('products.update', $product->id) }}" method="POST" enctype="multipart/form-data">
#csrf
#method('PUT')
This is ProductController.php
public function edit(Product $product)
{
return view('products.edit',compact('product'));
}
public function update(Request $request, Product $product)
{
$request->validate([
'name' => 'required',
'runs' => 'required',
'balls' => 'required',
'sixs' => 'required',
'fours' => 'required',
]);
$product->update($request->all());
return redirect()->route('products.index')
->with('success','Product updated successfully');
}
This is web.php
Route::resource('battersecondinnings', BattersecondinningsController::class);
In the Route 'battersecondinnings.update' what is missing is enctype="multipart/form-data". Add it and try again.
Please ckeck this answer in stackoverflow multipart/form-data meaning
Here is the code for controller:
public function save_product(Request $request){
$request->validate([
'product_name' => 'required',
'product_price' => 'required',
'product_category' => 'required',
'description' => 'nullable',
'image1' => 'required',
'image2' => 'nullable',
'image3' => 'nullable',
], [
'product_name.required' => 'Product name field required',
'product_price.required' => 'Asking price field required',
'product_category.required' => 'Product category field required',
'image1.required' => 'You need a upload image-1 field',
]);
if($request->hasFile('image1') || $request->hasFile('image2') || $request->hasFile('image3')){
$file1 = $request->file('image1');
$file2 = $request->file('image2');
$file3 = $request->file('image3');
$text1 = $file1->getClientOriginalExtension();
$text2 = $file2->getClientOriginalExtension();
$text3 = $file3->getClientOriginalExtension();
$fileName1 = time().'.'.$text1;
$fileName2 = time().'.'.$text2;
$fileName3 = time().'.'.$text3;
$file1->move('uploads/product_image', $fileName1);
$file2->move('uploads/product_image', $fileName2);
$file3->move('uploads/product_image', $fileName3);
$product = Product::create([
'image1'=>$fileName1,
'image2'=>$fileName2,
'image3'=>$fileName3,
'product_category' => trim($request->input('product_category')),
'product_name' => trim($request->input('product_name')),
'product_price' => trim($request->input('product_price')),
'description' => trim($request->input('description')),
]);
}
return redirect()->route('add_product')
->with('success','Successfully added Product!');
}
Here is the code for blade template:
#extends('admin.layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-12">
#include('pages.partials.flash_message')
</div>
</div>
</div>
<div class="card card-default">
<div class="card-header card-header-border-bottom">
<h2>Add Product</h2>
</div>
<div class="card-body">
<form action="{{route('save_product')}}" method="POST">
#csrf
<div class="form-group">
<label for="exampleFormControlSelect12">Select Category</label>
<input type="text" class="form-control" placeholder="Enter Arrival Time"
id="exampleFormControlSelect12" name="product_category" list="product_category"
autocomplete="off">
<datalist class="form-control" id="product_category" style="display: none" >
<option value="Women Clothes"></option>
<option value="Jewellery"></option>
<option value="Shoes"></option>
<option value="Sun Glass"></option>
<option value="Hair Band"></option>
</datalist>
</div>
<div class="form-group">
<label for="inputGroupFile02">Upload Product Image-1</label>
</div>
#if ($errors->has('image1'))
<span class="text-danger" style="font-weight: bold">{{ $errors->first('image1') }}
</span>
#endif
<div class="input-group my-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupFileAddon01">Upload Image</span>
</div>
<div class="custom-file">
<input type="file" name="image1" class="custom-file-input" id="inputGroupFile01"
aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" for="inputGroupFile01">Choose file</label>
</div>
</div>
<div class="form-group">
<label for="inputGroupFile02">Upload Product Image-2</label>
<small>*Optional</small>
</div>
#if ($errors->has('image2'))
<span class="text-danger" style="font-weight: bold">{{ $errors->first('image2') }}
</span>
#endif
<div class="input-group my-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupFileAddon01">Upload Image</span>
</div>
<div class="custom-file">
<input type="file" name="image2" class="custom-file-input" id="inputGroupFile02"
aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" for="inputGroupFile01">Choose file</label>
</div>
</div>
<div class="form-group">
<label for="inputGroupFile02">Upload Product Image-3</label>
<small>*Optional</small>
</div>
#if ($errors->has('image3'))
<span class="text-danger" style="font-weight: bold">{{ $errors->first('image3') }}
</span>
#endif
<div class="input-group my-3">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroupFileAddon01">Upload Image</span>
</div>
<div class="custom-file">
<input type="file" name="image3" class="custom-file-input" id="inputGroupFile03"
aria-describedby="inputGroupFileAddon01">
<label class="custom-file-label" for="inputGroupFile01">Choose file</label>
</div>
</div>
<div class="form-group">
<label for="exampleFormControlInput1">Product Name or Title</label>
<input type="text" name="product_name" class="form-control"
id="exampleFormControlInput1" placeholder="Enter Product Name or Title">
#if ($errors->has('product_name'))
<span class="text-danger" style="font-weight: bold">{{ $errors-
>first('product_name') }}</span>
#endif
</div>
<div class="form-group">
<label for="exampleFormControlInput2">Asking Price</label>
<input type="number" name="product_price" class="form-control"
id="exampleFormControlInput2" placeholder="Enter Asking Price">
#if ($errors->has('product_price'))
<span class="text-danger" style="font-weight: bold">{{ $errors-
>first('product_price') }}</span>
#endif
</div>
<div class="form-group">
<label for="exampleFormControlTextarea1">Product Description</label>
<textarea class="form-control" name="product_description"
id="exampleFormControlTextarea1" rows="3"></textarea>
</div>
<div class="form-footer pt-4 pt-5 mt-4 border-top">
<button type="submit" class="btn btn-primary btn-default">Submit</button>
</div>
</form>
</div>
</div>
#endsection
Route:
Route::post('/admin/add_product','App\Http\Controllers\Admin\AdminController#save_product')
->name('save_product')->middleware('admin');
Model:
protected $fillable = [
'product_name',
'product_category',
'product_price',
'description',
'image1',
'image2',
'image3',
];
Migration:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('product_name');
$table->string('product_category');
$table->string('product_price');
$table->text('description')->nullable();
$table->string('image1');
$table->string('image2')->nullable();
$table->string('image3')->nullable();
$table->timestamps();
});
}
I can't find any error in my code please help me to find out. Thanks in advance.
You have to make separate if conditions for each image. Try this:
$fileName1='';
$fileName2='';
$fileName3='';
if($request->hasFile('image1')) {
$file1 = $request->file('image1');
$text1 = $file1->getClientOriginalExtension();
$fileName1 = time() . '.' . $text1;
$file1->move('uploads/product_image', $fileName1);
}
if($request->hasFile('image2')) {
$file2 = $request->file('image2');
$text2 = $file2->getClientOriginalExtension();
$fileName2 = time() . '.' . $text2;
$file2->move('uploads/product_image', $fileName2);
}
if($request->hasFile('image3')) {
$file3 = $request->file('image3');
$text3 = $file3->getClientOriginalExtension();
$fileName3 = time() . '.' . $text3;
$file3->move('uploads/product_image', $fileName3);
}
$product = Product::create([
'image1'=>$fileName1,
'image2'=>$fileName2,
'image3'=>$fileName3,
'product_category' => trim($request->input('product_category')),
'product_name' => trim($request->input('product_name')),
'product_price' => trim($request->input('product_price')),
'description' => trim($request->input('description')),
]);
if($product){
return redirect()->route('add_product')->with('success','Successfully added Product!');
}
else{
return redirect()->back()->with('error','Error!');
}
using enctype="multipart/form-data" inside form tag .
I have overridden postReset method in PassowrdController:
public function postReset(Request $request)
{
$data = Input::all();
$rules = array(
'token' => 'required',
'current_password' => 'required|currentpasscheck',
'password' => 'required|confirmed',
);
$messages = array(
'currentpasscheck' => 'Your old password was incorrect',
);
Validator::extend('currentpasscheck', function ($attribute, $value, $parameters)
{
return Hash::check($value, Auth::user()->getAuthPassword());
});
$validation = Validator::make($data, $rules, $messages);
if ($validation->fails())
{
// Validation has failed.
return Redirect::to('password-reset')
->withErrors($validation)
->withInput();
}
$user = Auth::user();
$user->password = bcrypt($data['password']);
$user->save();
$this->auth->logout();
return Redirect::to('/');
}
route:
Route::post('/password/reset', 'Auth\PasswordController#postReset');//this can be accessed by auth user
reset.blade.php
#extends('app')
#section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Reset Password</div>
<div class="panel-body">
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form class="form-horizontal" role="form" method="POST" action="{{ url('/password/reset') }}">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="token" value="{{ $token }}">
<div class="form-group">
<!-- <label class="col-md-4 control-label">E-Mail Address</label>-->
<label class="col-md-4 control-label">Current Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="current_password" value="{{ old('current_password') }}">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">Confirm Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password_confirmation">
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Reset Password
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
but after submit change password form I got unauthorized page.