Error when I submit form to insert to database. The problem is why picture does not exist?
Method Illuminate\Http\Request::picture does not exist.
This is my Controller.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\User;
use File;
class UsersController extends Controller
{
public function registeract(Request $request)
{
$this->validate($request,[
'name' => 'required|alpha',
'email' => 'required|email',
'level' => 'required|alpha',
'picture' => 'required|file|image|mimes:jpeg,jpg,png|max:1048',
'password' => 'required'
]);
// Menyiapkan data gambar yg diupload ke variable $file
$picture = $request->picture('picture');
$file_name = time()."_".$picture->getClientOriginalName();
// Isi dengan nama folder tempat kemana file diupload
$upload_directory = 'p_users';
$picture->move($upload_directory, $file_name);
User::create([
'name' => $request->name,
'email' => $request->email,
'level' => $request->level,
'picture' => $file_name,
'password' => bcrypt($request->password),
'remember_token' => Str::random(60)
]);
return redirect('/ec-admin/users')->with('usrsinsertno', 'Data Inserted Successfully!');
}
}
I don't know therefore can use as Model but I see on youtube that there some people use as model.
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'level', 'picture', 'password'
];
protected $hidden = [
'password', 'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
}
CodePudding user response:
Picture method doesn't exist on Laravel's request class.
You need to use $picture = $request->file('picture');
CodePudding user response:
Any methods that appear in your head cannot be used.
There is no method like picture
in Laravel, as @amirrezam75 mentioned.
Firstly, you need to check if the file exists. You can then save the file and its name.
Try below code:
if ($request->hasFile('picture')) {
$picture = $request->picture('picture');
$file_name = time()."_".$picture->getClientOriginalName();
}else{
$file_name = null;
}