How to insert a row in Laravel given that one field does not have a default value? - laravel

I try to insert the session user id in form but it was show me error this:
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into stocks (product_name, product_code, details, price, cost, quntity, updated_at, created_at) values (basmati Rice, 23432, dskljsdlkks;lk; ;ks;lvk, 40, 75, 100kg, 2019-09-09 08:43:12, 2019-09-09 08:43:12))
I think error on this line of code which I used for store the session id
<input type="hidden" name="user_id"
value="{{ $request->session()->put('user',$request->input('id')) }}"
> class="form-control">
This is my create blade page, which I make a form and try to insert the form fields.
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-md-12">
#if(count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
#if(\Session::has('success'))
<div class="alert alert-success">
<p>{{ \Session::get('success') }}</p>
</div>
#endif
<div class="card">
<div class="card-header">
<h5 class="title">Add New Product on Stock</h5>
</div>
<div class="card-body">
<form method="post" action="{{url('stock')}}">
{{csrf_field()}}
<div class="row">
<div class="col-md-6 pr-1">
<div class="form-group">
<label>Product Name</label>
<input type="text" name="user_id"
value="{{Auth::user()->id }}" class="form-control">
<input type="text" name="product_name" class="form-control" placeholder="Enter Product Name" />
</div>
</div>
<div class="col-md-6 pl-1">
<div class="form-group">
<label>Product Code</label>
<input type="text" name="product_code" class="form-control" placeholder="Enter product_code" />
</div>
</div>
</div>
</br>
<div class="row">
<div class="col-md-4 pr-1">
<div class="form-group">
<label>Price</label>
<input type="text" name="price" class="form-control" placeholder="Enter price" />
</div>
</div>
<div class="col-md-4 px-1">
<div class="form-group">
<label>Cost</label>
<input type="text" name="cost" class="form-control" placeholder="Enter cost" />
</div>
</div>
<div class="col-md-4 pl-1">
<div class="form-group">
<label>Quantity</label>
<input type="text" name="quntity" class="form-control" placeholder="Enter quntity" />
</div>
</div>
</div>
</br>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label>Details</label>
<input type="text" name="details" class="form-control" placeholder="Enter details" />
</div>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" />
</div>
</form>
</div>
</div>
#endsection
this is my controller file of stock
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Stock;
use Auth;
class StockController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$stocks = Stock::all()->toArray();
return view('stock.index', compact('stocks'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('stock.create');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'user_id' =>'required',
'product_name' => 'required',
'product_code' => 'required',
'details' => 'required',
'price' => 'required',
'cost' => 'required',
'quntity' => 'required'
]);
$stock = new Stock([
'user_id' => Auth::user()->user_id,
'product_name' => $request->get('product_name'),
'product_code' => $request->get('product_code'),
'details' => $request->get('details'),
'price' => $request->get('price'),
'cost' => $request->get('cost'),
'quntity' => $request->get('quntity')
]);
$stock->save();
return redirect()->route('stock.index')->with('success', 'Data Added');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$stock = Stock::find($id);
return view('stock.edit', compact('stock', 'id'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'product_name' => 'required',
'product_code' => 'required',
'details' => 'required',
'price' => 'required',
'cost' => 'required',
'quntity' => 'required'
]);
$stock = Stock::find($id);
$stock->product_name = $request->get('product_name');
$stock->product_code = $request->get('product_code');
$stock->details = $request->get('details');
$stock->price = $request->get('price');
$stock->cost = $request->get('cost');
$stock->quntity = $request->get('quntity');
$stock->save();
return redirect()->route('stock.index')->with('success', 'Data Updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$stock = Stock::find($id);
$stock->delete();
return redirect()->route('stock.index')->with('success', 'Data Deleted');
}
}

You don't have to use the $request->input('id') to get the user id .
Simply use
Session::flash(Auth::user()->id);
For your code ,
<input type="hidden" name="user_id"
value="{{Auth::user()->id, Session::flash(auth::user()->id) }}"
> class="form-control">
And in your controller ,
when you are trying to save the user_id variable ,
EDIT ::::
$stock = new Stock([
'user_id' => Auth::user()->id,
'product_name' => $request->get('product_name'),
'product_code' => $request->get('product_code'),
'details' => $request->get('details'),
'price' => $request->get('price'),
'cost' => $request->get('cost'),
'quntity' => $request->get('quntity')
]);
Use the session variable to store ,.
Or you can directly store the user_id using Auth::user()->id in that line ,
'user_id' => Auth::user()->user_id,

change this:
$request->session()->put('user',$request->input('id'))
to like this:
Session::set('user',$request->input('id'));
Make attention, you need pass the $request to your view, so you can use the request data. Otherwise create a variable $user_id and pass it to your view blade. in this case you can do:
Session::set('user',$user_id);

Related

ArgumentCountError Too few arguments

I want to add data in database in Laravel, but I get error.
This is my Error:
ArgumentCountError
Too few arguments to function App\Http\Requests\Admin\Categories\StoreRequest::Illuminate\Foundation\Providers{closure}(), 0 passed in E:\xampp\htdocs\cylinders\cylinders\vendor\laravel\framework\src\Illuminate\Macroable\Traits\Macroable.php on line 124 and exactly 1 expected
Here is my Route:
Route::post('', [CategoriesController::class, 'store'])->name('admin.categories.store');
my page:
<form action="{{ route('admin.categories.store') }}" method="post">
#csrf
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Slug</label>
<input type="text" class="form-control" name="slug">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control" name="title">
</div>
</div>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary float-left">Save</button>
</div>
</form>
my Contoller:
public function store(StoreRequest $request)
{
$validatedData = $request->validate();
$createdCategory = Category::create([
'title' => $validatedData['title'],
'slug' => $validatedData['slug'],
]);
if(!$createdCategory){
return back()->with('failed', 'Failed to Create Category.');
}
return back()->with('success', 'Success to Create Category.');
}
my Request:
<?php
namespace App\Http\Requests\Admin\Categories;
use Illuminate\Foundation\Http\FormRequest;
class StoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'title' => 'required|min:3|max:128|unique:categories,title',
'slug' => 'required|min:3|max:128|unique:categories,slug',
];
}
}

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

laravel The PUT method is not supported for this route. Supported methods: GET, HEAD

I have a problem. i want to create an annnouncements from my post table
by making 2 combined forms but i don't if this works cause I'm new to the laravel and this is what i have
the blade
<form action="{{route('superadminpage.admin_announce.admin_view_announce',$announces->id)}}" method="POST" enctype="multipart/form-data">
<div class="container">
<div class="jumnbotron">
<h1> </h1>
<br>
<div class="container">
<div class="jumnbotron">
<h1> Notification Announcement </h1>
<br>
<form method="POST" action="{{action('AdminController#store',$announces->id)}}">
{{csrf_field() }}
<div class="card">
<div class="card-body">
<input type="hidden" name="_method" value="PUT" />
<input type="text" name="title" class="form-control" value="{{$announces->departments->department}} "> <h3> </h3>
<input type="hidden" name="_method" value="PUT" />
<input type="text" name="title" class="form-control" value="{{ $announces->Title}}"> <br>
<input type="hidden" name="_method" value="PUT" />
<input type="text" name="title" class="form-control" value="{{ $announces->Content}}"> <br>
</div>
<input type="submit" name="submit" class="btn btn-primary" value="notify"/>
</div>
</form>
edit
</div>
</div>
</form>
the Controller
public function view($id)
{
$announces = Post::find($id);
return view('superadminpage.admin_announce.admin_view_announce',compact('announces', 'id'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data = request()->validate([
'department_id' => ['required', 'string', 'max:255'],
'Title' => ['required', 'string', 'max:255'],
'Content' => ['required', 'string', 'max:255'],
]);
$data = Announcement::create([
'department_id' => $data['department_id'],
'Title' => $data['Title'],
'Content' => $data['Content'],
]);
return redirect('admin_update')->with('success', 'Events has been added');
}
and the Route
Route::get('/admin_update', 'AdminController#index');
Route::get('/admin_view_announce/{id}', 'AdminController#view')->name('superadminpage.admin_announce.admin_view_announce');
Route::put('/admin_view_announce', 'AdminController#store');
Route::get('/admin_announce_create/{id}', 'AdminController#show');
Route::get('/admin_announce_editform/{id}', 'AdminController#edit')->name('superadminpage.admin_announce.admin_announce_editform');
Route::put('/admin_announce_editform/{id}', 'AdminController#update')->name('superadminpage.admin_announce.admin_announce_editform');
NOTE:(I'm not using an eloquent here i just fetch the data from the Post to the Announcement that showed in the blade)

Laravel View Error

public function index()
{
$students = Student::all()->toArray();
return view('student.index', compact('students'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('student.create');//
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request, [
'first_name' => 'required',
'last_name' => 'required',
'Age' => 'required',
'Address' => 'required',
'Grade_Level' => 'required',
'mothers_first_name' => 'required',
'mothers_last_name' => 'required',
'mothers_age' => 'required',
'fathers_first_name' => 'required',
'fathers_last_name' => 'required',
'fathers_age' => 'required'
]);
$student = new Student([
'first_name' => $request->get('first_name'),
'last_name' => $request->get('last_name'),
'Age' => $request->get('Age'),
'Address' => $request->get('Address'),
'Grade_Level' => $request->get('Grade_Level'),
'mothers_first_name' => $request->get('mothers_first_name'),
'mothers_last_name' => $request->get('mothers_last_name'),
'mothers_age' => $request->get('mothers_age'),
'fathers_first_name' => $request->get('fathers_first_name'),
'fathers_last_name' => $request->get('fathers_last_name'),
'fathers_age' => $request->get('fathers_age')
]);
$student->save();
return redirect()->route('student.index')->with('success', 'New teacher data successfully added');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
$student = Student::find($id);
return view('student.edit', compact('student', 'id'));
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$this->validate($request, [
'first_name' => 'required',
'last_name' => 'required',
'Age' => 'required',
'Address' => 'required',
'Grade_Level' => 'required'
]);
$student = Student::find($id);
$student->first_name = $request->get('first_name');
$student->last_name = $request->get('last_name');
$student->Age = $request->get('Age');
$student->Address = $request->get('Address');
$student->Grade_Level = $request->get('Grade_Level');
$student->save();
return redirect()->route('student.index')->with('success', 'Student data successfully updated');
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$student = Student::find($id);
$student->delete();
return redirect()->route('student.index')->with('success', 'Data successfully deleted');
}
This is my Controller
<div class="container student-create">
<div class="students-info">
<h3>Student's Personal Info</h3>
<form method="post" action="{{url('student')}}">
{{csrf_field()}}
<div class="form-group">
<div class="col-sm">
<label for="Inputfirstname">First Name</label>
<input type="text" class="form-control" name="first_name" placeholder="Enter First Name">
</div>
<div class="col-sm">
<label for="Inputlastname">Last Name</label>
<input type="text" class="form-control" name="last_name" placeholder="Enter Last Name">
</div>
<div class="col-sm">
<label for="Inputlastname">Age</label>
<input type="number" class="form-control" name="Age" placeholder="Enter Last Name">
</div>
</div>
<div class="form-group">
<div class="col-sm">
<label for="InputAddress">Address</label>
<input type="text" class="form-control" name="Address" placeholder="Enter Address">
</div>
<div class="col-sm">
<label for="InputAddress">Grade Level/College Level</label>
<select class="custom-select" name="Grade_Level">
<option selected="">Select Grade Level</option>
<option>Day Care 1</option>
<option>Day Care 2</option>
<option>Kinder Garten 1</option>
<option>Kinder Garten 2</option>
<option>Elementary 1</option>
<option>Elementary 2</option>
<option>Elementary 3</option>
<option>Elementary 4</option>
<option>Elementary 5</option>
<option>Elementary 6</option>
<option>Junior Highschool 1</option>
<option>Junior Highschool 2</option>
<option>Junior Highschool 3</option>
<option>Junior Highschool 4</option>
<option>Senior Highschool 1</option>
<option>Senior Highschool 2</option>
<option>College Level Information Technology 1</option>
<option>College Level Information Technology 2</option>
<option>College Level Information Technology 3</option>
<option>College Level Information Technology 4</option>
</select>
</div>
</div>
<h3>Student's Parents Info</h3>
<div class="form-group">
<div class="col-sm">
<label for="Inputfirstname">Mothers First Name</label>
<input type="text" class="form-control" name="mothers_first_name" placeholder="Enter First Name">
</div>
<div class="col-sm">
<label for="Inputfirstname">Mothers Last Name</label>
<input type="text" class="form-control" name="mothers_last_name" placeholder="Enter Last Name">
</div>
<div class="col-sm">
<label for="Inputfirstname">Mothers Age</label>
<input type="number" class="form-control" name="mothers_age" placeholder="Enter Age">
</div>
</div>
<div class="form-group">
<div class="col-sm">
<label for="Inputfirstname">Fathers First Name</label>
<input type="text" class="form-control" name="fathers_first_name" placeholder="Enter First Name">
</div>
<div class="col-sm">
<label for="Inputfirstname">Fathers Last Name</label>
<input type="text" class="form-control" name="fathers_last_name" placeholder="Enter Last Name">
</div>
<div class="col-sm">
<label for="Inputfirstname">Fathers Age</label>
<input type="number" class="form-control" name="fathers_age" placeholder="Enter Age">
</div>
</div>
<div class="form-group submit">
<button type="button submit" class="btn btn-success">
<i class="fas fa-check"></i> Submit Data
</button>
</div>
</form>
</div>
This is my html code
so the function is like , if i create a data then after that it should redirected to an index or a view page but in my case after submitting/creating data it gives me this error i dont know why
View Page
i have the same page with the same function as this one but it turns out to be okay thats why im wondering why and also this create page that i posted here i started to get this error after i change the style of it and i added some fillable area's like the mother's age, name and etc
I think you can try this.
Change the APP_DEBUG=false to APP_DEBUG=true in .env file and run the following command
php artisan config:clear
It will show the proper error on page
If not work proper, let me know

PostTooLarge Exception Laravel

I have uploaded 15mb image and it throws exception of PostTooLarge Exception instead of exception i have to show flash error message but could not get it.
below is the handler i have used for Handler.php it works great but not display flash message.
if ($exception instanceof \Illuminate\Http\Exceptions\PostTooLargeException)
{
return redirect()->route('users.add')->withFlashError('Image Size too large!');
}
then i tried validate of laravel for image in my controller which is as below
$validator = Validator::make($request->all(), [
'image' => 'max:4000',
]);
if ($validator->fails())
{
return redirect()->route('user')->withFlashError('Image size exceeds 4MB');
}
but still no luck
below is blade file with form:
<form method="post" action="{{route('users.submit')}}" id="adduser" enctype="multipart/form-data">
{{csrf_field()}}
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-form-label">User name*</label>
<input type="text" class="form-control" name="name" placeholder="User name" value="{{ old('name') }}">
</div>
<div class="form-group col-md-6">
<label class="col-form-label">User email*</label>
<input type="text" class="form-control" name="email" value="{{ old('email') }}" placeholder="User email" autocomplete="off">
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-form-label">Password*</label>
<input id="password" type="password" class="form-control" name="password" placeholder="Password">
</div>
<div class="form-group col-md-6">
<label class="col-form-label">Confirm password</label>
<input type="password" class="form-control" name="confirmpassword" placeholder="Confirm password">
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="form-group col-md-6">
<label class="col-form-label">Phone number*</label>
<input type="text" class="form-control" name="mobile" value="{{ old('mobile') }}" placeholder="Phone Number">
</div>
<div class="col-md-6">
<label class="col-form-label">User role*</label>
<select name="role" class="form-control valid">
<option value="">Select Role</option>
<option {{ old('role')==3?'selected':'' }} value="3">Author</option>
<option {{ old('role')==5?'selected':'' }} value="5">Product Admin</option>
</select>
</div>
</div>
</div>
<div class="form-group">
<div class="form-row">
<div class="col-md-6">
<label class="col-form-label">User image</label>
<div class="card-body">
<div class="was-validated">
<label class="custom-file">
<input type="file" name="image" accept=".jpg, .png,.jpeg" id="file" class="custom-file-input">
<span class="custom-file-control"></span>
</label>
<ul class="preview" style="margin-top: 10px;">
</ul>
</div>
</div>
</div>
</div>
</div>
<button style="float: right;" type="submit" class="btn btn-primary" id="validateForm">Save</button>
</form>
and below is controller code :
<?php
namespace App\Http\Controllers\admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\User;
use Validator;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
public function index()
{
$users = User::whereNotIn('role', array(1,4))->orderBy('id','DESC')->get();
return view('admin.users.index',compact('users'));
}
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.users.add');
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data=$request->all();
$validate = Validator::make($data, [
'email' => 'required|string|email|max:255|unique:users',
]);
if ($validate->fails()) {
return redirect('users/add')->withFlashError('Email already exists')->withInput();
}
if(isset($_FILES['image']))
{
$validator = Validator::make($request->all(), [
'image' => 'max:4000',
]);
if ($validator->fails())
{
//return redirect()->route('user')->withFlashError('Image size exceeds 4MB');
//return redirect()->route('user')->withErrors(['error' => 'Image size exceeds 4MB']);
//return redirect('users/add')->withFlashError('Image size exceeds 4MB')->withInput();
return redirect()->back()->withErrors(['error' => 'Image size exceeds 4MB']);
}
if(basename($_FILES['image']['name']) != "")
{
$target_path = base_path().'/public/user/';
$time=round(microtime(true));
$newfilename = $target_path .$time . '.jpg';
$target_path = $target_path .time().basename($_FILES['image']['name']);
if (move_uploaded_file($_FILES['image']['tmp_name'], $newfilename))
{
$user_data['image'] =$time.'.jpg';
}
}
else
{
$user_data['image'] = "";
}
}
$insert_data = array
(
"name" => $data['name'],
"email" => $data['email'],
"password" => bcrypt($data['password']),
"mobile" => $data['mobile'],
"image" => $user_data['image'],
"role" => $data['role'],
"status" => 1
);
User::create($insert_data);
return redirect()->route('users')->withFlashSuccess('User added successfully');
}
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* #param \Illuminate\Http\Request $request
* #param int $id
* #return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function destroy($id)
{
$user = User::find($id);
$user->delete();
return redirect()->route('users')->withFlashSuccess('User deleted successfully');
}
public function delete($id)
{
$news = User::find($id);
$news->delete();
}
public function status($id)
{
$store=User::find($id);
if($store->status==1)
{
$status=0;
}
else
{
$status=1;
}
User::whereId($id)->update(array('status'=>$status));
return redirect()->route('users')->withFlashSuccess('User status changed');
}
public function saverole(Request $request)
{
$data=$request->all();
$string_role = implode(', ', $data['role']);
User::whereId($data['user_id'])->update(array('role'=>$string_role));
return redirect()->route('users')->withFlashSuccess('User role changed');
}
}
Change your validate line of code to this:
$this->validate(request(), [
'image' => 'max:4000'
],
[
'image.max' => 'Image size exceeds 4MB'
]);
And inside your view file, run this code to display the errors:
#if($errors->any())
<h4>{{$errors->first()}}</h4>
#endif
Because the php.ini stands first while throwing exception, it will not carry forward that expectation to your defined errors.

Resources