This chapter will explain a file upload. https://laravel.com/docs/7.x/filesystem
Another example https://gist.github.com/jimgwhit/0e4929230cd6b7df4b274b0b04312edd
For some reason this does not allow me to upload an image from my form and I do not understand why this happens, where is my error?
<div class="row">
{!! Form::model($user, [
'route' => $user->exists ? ['admin.users.update', $user->id] : 'admin.users.store',
'files' => true,
'enctype' => 'multipart/form-data',
'method' => $user->exists ? 'PUT' : 'POST'
]) !!}
<div class="col-md-4">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Perfil</h3>
</div>
<div class="box-body">
<img width="300px" class="profile-user-img img-responsive img-circle"
src="{{ Storage::url($user->user_photo)}}"
alt="{{ $user->name}}">
<div class="form-group">
{!! Form::label('user_photo', 'Imagen de Perfil') !!}
{!! Form::file('user_photo') !!}
</div>
</div>
</div>
</div>
{!! Form::close() !!}
</div>
this is how I try to store the information entered from my controller
<?php
namespace App\Http\Controllers;
use App\User;
use App\Statu;
use DataTables;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Providers\UserWasCreated;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\SaveUserRequest;
use Illuminate\Support\Facades\Storage;
class UserController extends Controller
{
public function store(SaveUserRequest $request)
{
if ($request->ajax()){
try {
// Transacciones
DB::beginTransaction();
// Creamos el usuario
$user = new User;
$user->name = $request->get('name');
$user->surname = $request->get('surname');
$user->nickname = $request->get('nickname');
$user->email = $request->get('email');
$user->password = $request->get('password');
$user->gender = $request->get('gender');
$user->address = $request->get('address');
$user->postcode = $request->get('postcode');
$user->city = $request->get('city');
$user->province = $request->get('province');
$user->phone = $request->get('phone');
$user->birthdate = Carbon::parse($request->get('birthdate'));
$user->status_id = $request->get('status_id');
if ($request->hasFile('user_photo'))
{
$user->user_photo = $request->file('user_photo')->store('public');
}
$user->save();
// Enviamos el email
// UserWasCreated::dispatch($user, $data['password']);
//$user->update($request->validated());
DB::commit();
} catch (Exception $e) {
// anula la transacion
DB::rollBack();
}
}
}
}
Before saving I make a verification with dd ($ user);
if ($request->hasFile('user_photo'))
{
$user->user_photo = $request->file('user_photo')->store('public');
}
dd($user);
$user->save();
insepected I see that the field user_photo exists inside #fillable: array: 14 [ 13 => "user_photo" ]
but it is not within #attributes: array: 13
Result of all verification:
App\User {#1747
#fillable: array:14 [
0 => "status_id"
1 => "surname"
2 => "nickname"
3 => "email"
4 => "email_verified_at"
5 => "password"
6 => "gender"
7 => "birthdate"
8 => "province"
9 => "city"
10 => "address"
11 => "postcode"
12 => "phone"
13 => "user_photo"
]
#dates: array:1 [
0 => "birthdate"
]
#hidden: array:2 [
0 => "password"
1 => "remember_token"
]
#casts: array:1 [
"email_verified_at" => "datetime"
]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: array:13 [
"name" => "Rodrigo"
"surname" => "Ruiz"
"nickname" => "rjr"
"email" => "[email protected]"
"password" => "y$mZw/n4LpvK58bE3ghXx.NuabyduUhsA6FDmFBWVdjRBDaTVPJAaTO"
"gender" => "Masculino"
"address" => null
"postcode" => null
"city" => null
"province" => "North Carolina"
"phone" => "3434434"
"birthdate" => "2020-06-17 00:00:00"
"status_id" => "1"
]
What am i doing wrong?. Please help me with the solution
Please or to participate in this conversation.