Image is not saving on my public path - laravel - mysql

im trying to save a profile image on a register blade, the images are going to database with the name but couldn't be save on my public/imgs repository, so i can't even show on a view. Can someone help me to resolve this problem? the name of the image on my database is "foto".
public function store
public function store(StoreUpdatePost $request)
{
$data = $request->all();
if($request->foto('foto')->isValid()){
$file = $request->foto->storeAs('public/users',$nameFile);
$file = str_replace('public/','app/',$file);
$data['foto'] = $file;
}
User::create($data);
//foto upload
if($request->hasFile('foto') && $request->file('foto')->isValid()){
$requestFoto = $request->foto;
$extension = $requestFoto->extension();
$fotoName = md5($requestFoto->getClientOriginalName() . strtotime("now")) . "." . $extension;
$request->foto->move(public_path('imgs\users'), $fotoName);
$data->foto = $fotoName;
$data->save();
}
}
VIEW REGISTER BLADE
#extends('layouts.app')
#section('content')
<div class="container"><!---->
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card" >
<br>
<div class="textocs">
<h4>CADASTRE-SE</h4>
</div>
<br><br>
<div class="card-body">
<form method="POST" enctype=”multipart/form-data” action="{{ route('register') }}">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Nome') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<br>
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-mail:') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<br>
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Senha:') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="new-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<br>
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Redigite a senha:') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
<br>
</div><br>
<div class="form-group row">
<label for="cidade" class="col-md-4 col-form-label text-md-right">{{ __('Cidade') }}</label>
<div class="col-md-6">
<input id="cidade" type="text" class="form-control #error('cidade') is-invalid #enderror" name="cidade" value="{{ old('cidade') }}" required autocomplete="cidade" autofocus>
#error('cidade')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<br>
</div>
</div>
<div class="form-group row">
<label for="numero" class="col-md-4 col-form-label text-md-right">{{ __('Número') }}</label>
<div class="col-md-6">
<input id="numero" type="number" class="form-control #error('numero') is-invalid #enderror" name="numero" value="{{ old('numero') }}" required autocomplete="numero" autofocus>
#error('numero')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<br>
</div>
</div>
<div class="form-group row">
<label for="foto" class="col-md-4 col-form-label text-md-right">{{ __('Foto') }}</label>
<div class="col-md-6">
<label for="foto">Foto:</label>
<input type="file" class="form-control-file" name="foto" >
<br>
</div>
</div>
<br><br>
<button type="submit" class="btn btn-primary">
{{ __('Cadastrar') }}
</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
MIGRATION TO ADD FIELDS ON USERS TABLE
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddCamposTableUsers extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('cidade');
$table->string('numero');
$table->string('foto')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
Schema::dropIfExists('users');
});
}
}

Related

ErrorException Trying to get property 'id' of non-object - trying to do a register on database

I'm trying to do a "livros" register with a foreign key from user, but when i try to send the information to database this error appear "ErrorException Trying to get property 'id' of non-object" could someone help me?
LIVROCONTROLLER
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Livro;
class LivroController extends Controller
{
/**
* Display a listing of the resource.
*
* #return \Illuminate\Http\Response
*/
protected $request;
private $repository;
private $livro;
public function __construct(Livro $livro)
{
$this->livro = $livro;
}
public function index()
{
/* $title = 'listagem dos livros';
$livros = $this->livro->all();
return view ('livros/cadastro', compact('livros','title'));*/
return view ('livros/cadastro');
}
protected function validator(Request $request)
{
return Validator::make($request, [
'namel' => ['required', 'string', 'max:200'],
'autor' => ['required', 'string', 'email', 'max:200'],
'editora' => ['required', 'string', 'max:50'],
'categoria'=> ['required', 'string', 'min:50'],
'classificação'=> ['required', 'string', 'min:1','max:2'],
'descricao'=> ['required', 'string', 'min:200'],
'image'=> ['not required'],
]);
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function create(Request $request)
{
$user = Auth::user()->id;
Livro::create([
'users_id' => $user,
'namel' => $request['namel'],
'autor' => $request['autor'],
'editora' => $request['editora'],
'categoria'=> $request['categoria'],
'classificação'=>$request['classificação'] ,
'descricao'=>$request['descricao'],
'image'=>$request['image'],
]);
return view('livros/cadastro');
}
}
LIVROS MIGRATION TABLE
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateLivrosTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('livros', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('users_id');
$table->foreign('users_id')->references('id')->on('users');
/*Auth::user()->id;*/
$table->text('namel');
$table->string('autor');
$table->string('editora');
$table->string('categoria');
$table->string('classificação');
$table->text('descricao');
$table->string('image')->nullable();
$table->timestamp('created_at')->nullable();
$table->timestamp('updated_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('livros');
}
}
LIVRO BLADE FILE
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card" >
<br>
<div class="textocs">
<h4>CADASTRO DE LIVROS</h4>
</div>
<br><br>
<div class="card-body">
<form method="POST" enctype=”multipart/form-data” action="{{ url('/cadastro_livros') }}">
#csrf
<div class="form-group row">
<label for="namel" class="col-md-4 col-form-label text-md-right">{{ __('Nome') }}</label>
<div class="col-md-6">
<input id="namel" type="text" class="form-control #error('namel') is-invalid #enderror" name="namel" value="{{ old('namel') }}" required autocomplete="namel" autofocus>
#error('namel')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<br>
</div>
</div>
<div class="form-group row">
<label for="autor" class="col-md-4 col-form-label text-md-right">{{ __('Autor') }}</label>
<div class="col-md-6">
<input id="autor" type="text" class="form-control #error('autor') is-invalid #enderror" name="autor" value="{{ old('autor') }}" required autocomplete="autor">
#error('autor')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<br>
</div>
</div>
<div class="form-group row">
<label for="editora" class="col-md-4 col-form-label text-md-right">{{ __('editora') }}</label>
<div class="col-md-6">
<input id="editora" type="text" class="form-control #error('editora') is-invalid #enderror" name="editora" value="{{ old('editora') }}" required autocomplete="editora" autofocus>
#error('editora')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<br>
</div>
</div>
<div class="form-group row">
<label for="categoria" class="col-md-4 col-form-label text-md-right">{{ __('categoria') }}</label>
<div class="col-md-6">
<input id="categoria" type="text" class="form-control #error('categoria') is-invalid #enderror" name="categoria" value="{{ old('categoria') }}" required autocomplete="categoria" autofocus>
#error('categoria')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<br>
</div>
</div>
<div class="form-group row">
<label for="classificação" class="col-md-4 col-form-label text-md-right">{{ __('classificação (1-10)') }}</label>
<div class="col-md-6">
<input id="classificação" type="number" maxlength="2" class="form-control #error('classificação') is-invalid #enderror" name="classificação" value="{{ old('classificação') }}" required autocomplete="classificação" autofocus>
#error('classificação')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<br>
</div>
</div>
<div class="form-group row">
<label for="descricao" class="col-md-4 col-form-label text-md-right">{{ __('descricao') }}</label>
<div class="col-md-6">
<input id="descricao" type="text" class="form-control #error('descricao') is-invalid #enderror" name="descricao" value="{{ old('descricao') }}" required autocomplete="descricao" autofocus>
#error('descricao')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<br>
</div>
</div>
<div class="form-group row">
<label for="image" class="col-md-4 col-form-label text-md-right">{{ __('imagem') }}</label>
<div class="col-md-6">
<input type="file" class="form-control #error('image') is-invalid #enderror" name="image" value="{{ old('image') }}" required autocomplete="image" autofocus>
#error('image')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
<br>
</div>
</div>
<br><br>
<button type="submit" class="btn btn-primary">
{{ __('Cadastrar') }}
</button>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
ROUTE TO THE REGISTER "LIVROS"
Route::get('/cad_livros', [App\Http\Controllers\LivroController::class, 'index']);
Route::post('/cadastro_livros',[App\Http\Controllers\LivroController::class, 'create']);
I think you are not logged-in and inside your create method you are trying to get the id of Auth::user() which is null, so you get that error.
If this create requires the user to login, protect your route with auth middleware.
Route::post(
'/cadastro_livros',
[App\Http\Controllers\LivroController::class, 'create']
)->middleware('auth');

what is wrong in this it is not inserting [duplicate]

i have a small problem here and it is that the image field doesn't have a default value as the complier said but actually i assigned a value for it i know that this error pops up when the field should have a value but the programmer didn't assign a value to it i know all of this but i already assigned a value to my field so what i am missing here
controller
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'image' => ['required' , 'image' , 'mimes:jpeg,png,jpg', 'max:2048'],
]);
}
protected function create(array $data)
{
$imageName = time().'.'.request()->image->getClientOriginalExtension();
request()->image->move(public_path('images'), $imageName);
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'image' => $imageName,
'password' => Hash::make($data['password']),
]);
}
view
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Register') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">
#csrf
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control #error('name') is-invalid #enderror" name="name" value="{{ old('name') }}" required autocomplete="name" autofocus>
#error('name')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email">
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</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 id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="new-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
<div class="col-md-6">
<input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
</div>
</div>
<label class="col-md-4 col-form-label text-md-right" >Upload a picture</label>
<div class="custom-file mb-3 col-sm-6">
<input type="file" class="custom-file-input center" id="image" name="image">
<label class="custom-file-label" for="customFile"></label>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Register') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
the problem is mainly from the controller but i said that i put the view just in case you want to see it
small note : the image is uploading and it is stored at public folder just like the code said but its name doesn't save at the database also the field image is string at my database because i just want its name so i could view it
snap shot
Set a default image for your image field. Also make sure you have made it fillable, if not set it in protected $fillable in your model User.php
You will get this error when a user doesn't upload an image.
$table->string('image')->default('default.jpg');//You can also add .png
Now have a default image in your storage with name default.jpg
This way the image will have a default value if user doesn't' upload it.
You need to add the default value or make that field null in the migration file, not the controller or view.
$table->string('image')->nullable();
or...
$table->string('image')->default('value');

How can I do this Input as a Dropdown button

How can I do this Input as a Dropdown button in bootstrap
<div class="form-group">
<label class="col-md-4 control-label" for="user_type">Tipi userit</label>
<div class="col-md-8">
<input id="user_type" name="user_type" type="text" placeholder="Tipi i Userit Admin/Editor/User" class="form-control input-md" value="{{ old('user_type') }}" required>
#if ($errors->has('user_type'))
<span class="help-block">
<strong>{{ $errors->first('user_type') }}</strong>
</span>
#endif
</div>
</div>

Registering form is not storing data in database

Well I am using this form to register a user in database but It is not successfully being storing data. While RegisterContoller.php is in Auth section. And I have given route: Auth::routes();. But still I why it is not storing data.
Register.blade.php
<form style="margin-left: 30%; margin-right: 30%; margin-top: 5%;" class="form-horizontal" method="POST" action="{{ route('register') }}">
{{ csrf_field() }}
<div class="form-group organic-form-2 {{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name">Your Name *</label>
<input id="name" class="form-control" type="text" name="name" value="{{ old('name') }}" required autofocus>
#if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
#endif
</div>
<div class="form-group organic-form-2 {{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email">Your Email *</label>
<input id="email" class="form-control" type="email" name="email" value="{{ old('email') }}" required>
</div>
<div class="form-group organic-form-2">
<label>Password *</label>
<input class="form-control" type="Password">
</div>
<div class="form-group organic-form-2">
<label>Repeat Password *</label>
<input class="form-control" type="Password">
</div>
<div class="form-group remember-me">
<div class="checkbox pull-left">
<label>
<input type="checkbox" name="value1"> Privacy Policy Agreement?
</label>
</div>
</div>
<div class="form-group footer-form">
<button class="btn btn-brand pill" type="submit">SUBMIT</button>
</div>
</form>
</div>
</div>
</div>
</section>
</div>
RegisterController.php
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
It seems you miss input names in your form.
Instead of:
<div class="form-group organic-form-2">
<label>Password *</label>
<input class="form-control" type="Password">
</div>
<div class="form-group organic-form-2">
<label>Repeat Password *</label>
<input class="form-control" type="Password">
</div>
use:
<div class="form-group organic-form-2">
<label>Password *</label>
<input class="form-control" type="Password" name="password">
</div>
<div class="form-group organic-form-2">
<label>Repeat Password *</label>
<input class="form-control" type="Password" name="password_confirmation">
</div>

Laravel 5.4 UnexpectedValueException in Response.php line 444 in form submition

I am making multiauth. When I submit admin email and password from admin login view I got bellow error
"UnexpectedValueException in Response.php line 444:
The Response content must be a string or object implementing __toString(), "boolean" given."
here is my form
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Admin Login</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('admin.login.submit') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
And here is route
Route::prefix('admin')->group(function (){
Route::post('/login', 'Auth\AdminLoginController#login')->name('admin.login.submit');
});
And the controller is here
<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class AdminLoginController extends Controller
{
public function __construct()
{
$this->middleware('guest');
}
public function showLoginForm()
{
return view('admin.auth.login');
}
public function login(Request $request)
{
return true;
}
}
What wrong here?
And How can I solve it?
Thanks in advance.
I found that problem. that was in my controller. Exactly in login function. I returened true. Here is the problem. When I return 1 then it worked.