Upload photo in project and db - Laravel - php

I need some help with upload. I want to insert a product into the db. The product has 3 pictures. I want the picture to be uploaded into a specific folder in the project, and the path to be entered into the db.
The folder where I want to upload the photos is: /public/css/img
My Db looks like this: I will put an example added manually in db.
id | title |price|category_id| images1 | images2| | images3| etc.
1 |Sofa |324.0 5 |/css/img/1.jpg |/css/img/2.jpg |/css/img/3.jpg
This is my view addProductModal.blade.php -> Is a modal with form.
<div class="modal fade" id="modalFormaddproduct" role="dialog">
<div class="modal-dialog" id="route">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Inchide</span>
</button>
<h4 class="modal-title" id="myModalLabel">Adauga Subcategorie</h4>
</div>
<!-- Modal Body -->
<div class="modal-body" style="text-align: center;">
<p class="statusMsg"></p>
<form role="form" action="{{route('addproduct')}}" method="post">
{{csrf_field()}}
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label>Nume</label>
<input type="text" class="form-control text-center" name="name" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Pret</label>
<input type="text" class="form-control text-center" name="price" placeholder="">
</div>
</div>
</div>
<!-- /.row -->
<div class="col-sm-6">
<div class="form-group">
<label>Subcategoria:</label>
<select style="text-align-last:center" class="form-control text-center" name="category_id">
#foreach($categories as $category)
#foreach($category->subcategories as $subcategory)
<option value="{{$subcategory->id}}">{{$subcategory->category}}</option>
#endforeach
#endforeach
</select>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<label>Descriere</label>
<input type="text" class="form-control text-center" name="description" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Marime</label>
<input type="text" class="form-control text-center" name="size" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Material</label>
<input type="text" class="form-control text-center" name="material" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Cantitate</label>
<input type="text" class="form-control text-center" name="quantity" placeholder="">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Recomandat:</label>
<select style="text-align-last:center" class="form-control text-center" name="hot">
<option value="0">Nerecomandat</option>
<option value="1">Recomandat</option>
</select>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label>Imagine 1:</label>
<input type="file" name="file1" id="file1">
<input type="submit" value="Upload1" name="submit1">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Imagine 2:</label>
<input type="file" name="file2" id="file2">
<input type="submit" value="Upload2" name="submit2">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label>Imagine 3:</label>
<input type="file" name="file3" id="file3">
<input type="submit" value="Upload3" name="submit3">
<input type="hidden" value="{{ csrf_token() }}" name="_token">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" style="background: gainsboro; border-radius: 8px" class="btn btn-default" data-dismiss="modal">Inchide</button>
<button type="submit" style="background: #10D47D; border-radius: 8px" class="btn btn-primary">Adauga</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
Route is Route::post('/products/add', 'AdminController#addproduct')->name('addproduct');
Controller: AdminController.php
public function addproduct(Request $request)
{
$product = new Product();
$product->title = $request->name;
$product->price = $request->price;
$product->category_id = $request->category_id;
$product->description = $request->description;
$product->size = $request->size;
$product->material = $request->material;
$product->quantity = $request->quantity;
$product->hot = $request->hot;
$product->images1 = $request->file1;
$product->images2 = $request->file2;
$product->images3 = $request->file3;
if (Input::hasFile('file1','file2','file3')) {
echo 'Uploaded';
$file = Input::file('file1','file2','file3');
$file->move('uploads', $file->getClientOriginalName());
echo '';
}
$product->save();
return redirect(route('adminproducts'))->with('success', 'The Product was added');
}

You should add into your form:
enctype="multipart/form-data"
to make sure you can upload a file like so:
<form role="form" action="{{route('addproduct')}}" method="post" enctype="multipart/form-data">

I do not think there is any reason not to use some maintained library but write all the functionality yourself. I would recommend you to take a look on: https://github.com/spatie/laravel-medialibrary

I found the solution.
public function addproduct(Request $request)
{
$product = new Product();
$product->title = $request->name;
$product->price = $request->price;
$product->category_id = $request->category_id;
$product->description = $request->description;
$product->size = $request->size;
$product->material = $request->material;
$product->quantity = $request->quantity;
$product->hot = $request->hot;
$file1 = Input::file('file1');
$file2 = Input::file('file2');
$file3 = Input::file('file3');
$filename1 = $file1->getClientOriginalName();
$filename2 = $file2->getClientOriginalName();
$filename3 = $file3->getClientOriginalName();
$file1 = $file1->move(public_path().'/img', $filename1);
$file2 = $file2->move(public_path().'/img', $filename2);
$file3 = $file3->move(public_path().'/img', $filename3);
$product->images1 = '/img/'.$filename1;
$product->images2 = '/img/'.$filename2;
$product->images3 = '/img/'.$filename3;
$product->save();
return redirect(route('adminproducts'))->with('success', 'Produsul a fost creat');
}

Related

Can't insert data on my database from laravel

When I try to insert data into my database, Laravel does not insert the records, but it is strange because when I migrate the tables to be able to perform the database, Laravel creates them without any problem, I do not know what I can be doing wrong if the migration run but stored no
Route:
Route::post('/proyecto', [ctrlCars::class,'store'])->name('cars');
Controler:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\cars;
class ctrlCars extends Controller
{
public function store(Request $request){
$request->validate([
'carRegistration'=> ['required','min:6'],
'name'=> ['required','min:6'],
'fromProduction' => ['required','min:6'],
'stateStored'=> ['required'],
'model'=> ['required','min:4'],
'dateAssembled' => ['required'],
]);
$car = new cars;
$car->carRegistration = $request->carRegistration;
$car->name = $request->name;
$car->fromProduction = $request->fromProduction;
$car->stateStored = $request->stateStored;
$car->model = $request->model;
$car->dateAssembled = $request->dateAssembled;
$car-> save();
return redirect()->route('cars')->with('success','Registro guardado satisfactoriamente');
}}
Template:
#extends('header')
#section('content')
<div class="container w-10 mt-5 border p-4">
<form action="{{ route('cars') }}" method="POST">
#csrf
#if (session('success'))
<h6 class="alert alert-success">{{ session('success') }}</h6>
#endif
#error('carRegistration')
<h6 class="alert alert-danger">{{ $message }}</h6>
#enderror
<p class="h2">Registro vehiculos</p>
<br>
<div class="row">
<section class="col-md-12">
<div class="form-group">
<section class="row">
<div class="col-md-4">
<label for="carRegistration" class="form-label">Placa</label>
<input type="text" class="form-control" name="carRegistration" placeholder="CDE001" maxlength="6">
</div>
<div class="col-md-4">
<label for="name" class="form-label">Nombre</label>
<input type="text" class="form-control" name="name" placeholder="Ferrari Enzo">
</div>
<div class="col-md-4">
<label for="fromProduction" class="form-label">Planta Produccion</label>
<input type="text" class="form-control" name="fromProduction" placeholder="Bmw sede1">
</div>
</section>
<section class="row mt-4">
<div class="col-md-4">
<label for="placa" class="form-label">Fecha Ensamble</label>
<input type="date" class="form-control" name="dateAssembled" placeholder="CDE001">
</div>
<div class="col-md-4">
<label for="model" class="form-label">Módelo Matricula</label>
<input type="text" class="form-control" name="model" maxlength="4" placeholder="2013">
</div>
<div class="col-md-4">
<label for="stateStored" class="form-label">Ciudad Almacenamiento</label>
<Select type="text" class="form-control" id="stateStored" placeholder="Medellin">
<option value=''>Elija una opción</option>
<option value='Medellin'>Medellín</option>
<option value="Bucaramanga">Bucaramanga</option>
<option value="Cali">Cali</option>
<option value="Bogota">Bogotá</option>
</Select>
</div>
</section>
</div>
</section>
</div>
<button type="submit" class="btn btn-success mt-4">Guardar</button>
</form>
</div>
The problem is from your template. The select tag should have a name attribute.
Change your template to this
$car->dateAssembled = $request->dateAssembled;
$car-> save();
return redirect()->route('cars')->with('success','Registro guardado satisfactoriamente');
}}
Template:
#extends('header')
#section('content')
<div class="container w-10 mt-5 border p-4">
<form action="{{ route('cars') }}" method="POST">
#csrf
#if (session('success'))
<h6 class="alert alert-success">{{ session('success') }}</h6>
#endif
#error('carRegistration')
<h6 class="alert alert-danger">{{ $message }}</h6>
#enderror
<p class="h2">Registro vehiculos</p>
<br>
<div class="row">
<section class="col-md-12">
<div class="form-group">
<section class="row">
<div class="col-md-4">
<label for="carRegistration" class="form-label">Placa</label>
<input type="text" class="form-control" name="carRegistration" placeholder="CDE001" maxlength="6">
</div>
<div class="col-md-4">
<label for="name" class="form-label">Nombre</label>
<input type="text" class="form-control" name="name" placeholder="Ferrari Enzo">
</div>
<div class="col-md-4">
<label for="fromProduction" class="form-label">Planta Produccion</label>
<input type="text" class="form-control" name="fromProduction" placeholder="Bmw sede1">
</div>
</section>
<section class="row mt-4">
<div class="col-md-4">
<label for="placa" class="form-label">Fecha Ensamble</label>
<input type="date" class="form-control" name="dateAssembled" placeholder="CDE001">
</div>
<div class="col-md-4">
<label for="model" class="form-label">Módelo Matricula</label>
<input type="text" class="form-control" name="model" maxlength="4" placeholder="2013">
</div>
<div class="col-md-4">
<label for="stateStored" class="form-label">Ciudad Almacenamiento</label>
<Select type="text" name="stateStored" class="form-control" id="stateStored" placeholder="Medellin">
<option value=''>Elija una opción</option>
<option value='Medellin'>Medellín</option>
<option value="Bucaramanga">Bucaramanga</option>
<option value="Cali">Cali</option>
<option value="Bogota">Bogotá</option>
</Select>
</div>
</section>
</div>
</section>
</div>
<button type="submit" class="btn btn-success mt-4">Guardar</button>
</form>
</div>
Thanks for help, the error come from because I call from name on the controller and in this input only have ID, and in the validation for this field have 'required';
I don't know how reply comments, but thanks aynber and Daniel L, you were nice help
First time Comment your validation section and try again. If your data successfully inserted. Then Your need to modify your required field like below.
Replace
['required','min:6']
Like this:
'required|min:6',

302 found in Laravel project

Route
Route::get('/addproduct', [UserController::class, 'addproduct'])->name('addproduct');
Route::post('/addnewproduct', [UserController::class, 'addnewproduct'])->name('addnewproduct');
Route::get('/showproducts', [UserController::class, 'showproducts'])->name('showproducts');
Controller
public function addproduct()
{
return view('addProduct');
}
public function addnewproduct(Request $request)
{
$user_id = Auth::user()->id;
$request->validate([
'file' => 'required|mimes:jpg,png,gif,svg',
'name' => 'required|string|min:3|max:30',
'description' => 'required|string|min:1|max:255',
'category' => 'required|string|max:255',
]);
$productModel = new Product();
$filename =time().'_' .$request->name;
$filePath = $request->file('file')->move('upload', $filename);
$productModel->name = $request->name;
$productModel->description = $request->description;
$productModel->category = $request->category;
$productModel->image = $filePath;
$productModel->save();
return redirect()->route('showproducts');
}
public function showproducts()
{
$user_id = Auth::user()->id;
$results=DB::select('SELECT * FROM products');
$data = [
'results' =>$results
];
return view('showProduct')->with($data);
}
View
<div class="card-body">
<form method="POST" action="{{ route('addnewproduct') }}" class="needs-validation form" enctype="multipart/form-data" novalidate>
#csrf
<div class="row">
<div class="col-sm-6">
<input type="file" id="input-file-now" class="form-control dropify" name="file" required/>
</div>
<div class="col-sm-6">
<div class="row mb-3">
<label for="validationName" class="form-label">Product name</label>
<div class="input-group has-validation">
<input type="text" class="form-control" name="name" id="validationName" aria-describedby="inputGroupPrepend" required>
<div class="invalid-feedback">
You have to enter Product name!
</div>
</div>
</div>
<div class="row mb-3">
<label for="validationCategory" class="form-label">Category</label>
<div class="input-group has-validation">
<select class="form-select" name="category" id="validationCategory" required>
<option selected disabled value="">Select...</option>
<option value="c1">c1</option>
<option value="c2">c2</option>
</select>
</div>
<div class="invalid-feedback">
Please select Category
</div>
</div>
<div class="row mb-3">
<label for="validationDes" class="form-label">Description</label>
<div class="input-group has-validation">
<textarea type="text" class="form-control text-des" name="address" id="validationDes" aria-describedby="inputGroupPrepend" required></textarea>
<div class="invalid-feedback">
Please enter product descriiption in here.
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
<label class="form-check-label" for="invalidCheck">
Check it.
</label>
<div class="invalid-feedback">
You have to checkbox!
</div>
</div>
</div>
<div class="row">
<div class="float-end">
<button class="btn btn-primary sumbtn float-end" type="submit"><i class="bi bi-person-plus-fill"></i> ADD</button>
</div>
</div>
</form>
</div>
after enter all fields and file select and then click checkbox, but when I click button don't go to showproducts page, and don't save enterd data and file.
view screenshot
after click button get 302 and not redirect to "showproducts"
Please help me, I'm very very stress with that problem

How i fixed $data is undefined?

I have a problem while creating edit and create forms
I have a form that is used to create and edit when I call $data on the form so that it appears when editing the form.
But when I open the form to create new data the error $data is undefined appears.
This is my route
Route::resource('/tabungan/jadwal-autodebet', SavingAccountScheduleController::class);
This is the create and edit script in my controller
public function create()
{
$info['header'] = $this->title;
$info['description'] = 'Tambah';
$info['breadcrumb'] = null;
return view('banking::autodebet-schedule.form',$info);
}
public function edit($id)
{
$info['header'] = $this->title;
$info['description'] = 'Ubah';
$info['breadcrumb'] = null;
$data = TransactionSchedule::find($id);
$origin = clone $data;
$origin = $data->origin;
$destination = clone $data;
$destination = $data->destination;
$destinationType = '';
$saving = SavingAccount::class;
$loan = LoanAccount::class;
if($data->origin_type == $saving){
$destinationType = 'Tabungan';
}elseif($data->origin_type == $loan){
$destinationType = 'Pinjaman';
}
// dd($data,$origin,$destination);
return view('banking::autodebet-schedule.form',compact('data','origin','destination','destinationType'),$info);
}
and this is my autodebet.schedule.form script
<form id="formData" action="{{ isset($data) ? admin_url('cb/tabungan/jadwal-autodebet/'.$data->id) : admin_url('cb/tabungan/jadwal-autodebet/')}}" method="POST" autocomplete="off">
{{ csrf_field() }}
#if(isset($data))
{{method_field('PUT')}}
#endif
<div class="container d-flex justify-content-center">
<div class="col-12">
<div class="row">
<div class="col-2">
<label class="label-required" for="" >Produk Tujuan</label>
</div>
<div class="col-10">
<select name="destination_type" id="destination_type" class="select2 form-control destination_type">
<option value="{{ $data->origin_type }}">{{ $destinationType }}</option>
<option class="type_option" id="SavingAccount" value="Modules\Banking\Entities\SavingAccount">Tabungan</option>
<option class="type_option" id="LoanAccount" value="Modules\Banking\Entities\LoanAccount">Pinjaman</option>
</select>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="">No Rekening Sumber</label>
</div>
<div class="col-10">
<select id="origin_code" class="select2 form-control origin_code">
<option value="{{ $origin->id }}">{{ $origin->saving_code }}-{{ $origin->customer->customer_name}}-{{ $destinationType }}</option>
</select>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="" >Transaksi Rekening Anggota/Nasabah Lain</label>
</div>
<div class="col-10">
<input type="hidden" id="other_costumer_transaction" name="other_costumer_transaction" value="0">
<div class="az-toggle {{ 'off' }}" id="other_costumer_transaction_toggle"><span></span></div>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="">No Rekening Tujuan</label>
</div>
<div class="col-10">
<select id="destination_code" class="select2 form-control destination_code" >
<option value="{{ $destination->id }}">{{ $destination->saving_code }}-{{ $destination->customer->customer_name}}-{{ $destinationType }}</option>
</select>
</div>
</div>
<div class="row" id="info-nasabah" style="display: none">
<div class="col-2">
</div>
<div class="col-10">
<h6>No. Rekening : <span id="info-kode">xxx.xxx.xxx</span></h6>
</div>
<div class="col-2">
</div>
<div class="col-10">
<h6>Nama Nasabah : <span id="info-nama">xxx.xxx.xxx</span></h6>
</div>
<div class="col-2">
</div>
<div class="col-10">
<h6>Tipe Produk : <span id="info-produk">xxx.xxx.xxx</span></h6>
</div>
<div class="col-2">
</div>
<div class="col-10">
<h6>Saldo : <span id="info-saldo">xxx.xxx.xxx</span></h6>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="">Tanggal Autodebet</label>
</div>
<div class="col-10">
<select name="scheduled_day" id="scheduled_day" class="select2 form-control scheduled_day">
<option disabled value="{{ $data->scheduled_day }}">{{ $data->scheduled_day }}</option>
#for ($i = 1; $i <= 31; $i++)
<option class="day_option" id="day_{{ $i }}" value="{{ $i }}">{{ $i }}</option>
#endfor
</select>
</div>
</div>
<div class="row">
<div class="col-2">
<label for="saving_type_package">Keterangan</label>
</div>
<div class="col-10">
<textarea name="desc" class="form-control desc" id="" cols="30" rows="5">{{ $data->desc }}</textarea>
</div>
</div>
<div class="row">
<div class="col-2">
<label class="label-required" for="" >Status Autodebet</label>
</div>
<div class="col-10">
<input type="hidden" id="transaction_schedule_status" name="transaction_schedule_status" value="{{ $data->transaction_schedule_status ?? 1 }}">
<div class="az-toggle {{ isset($data->transaction_schedule_status) ? ($data->transaction_schedule_status == 1 ? 'on' :'') : 'on' }}" id="autodebet_status_toggle"><span></span></div>
</div>
</div>
<input type="hidden" value="" id="origin_id" name="origin_id">
<input type="hidden" class="schedule_id" id="schedule_id" name="schedule_id">
<input type="hidden" name="destination_code" id="destination_code_input">
<input type="hidden" name="origin_code" id="origin_code_input">
<div class="row">
<div class="col-12">
<div class="float-right">
<div class="btn-group">
<a href="{{admin_url('cb/tabungan/jadwal-autodebet')}}">
<button id="back_button" type="button" class="btn btn-secondary btn-block"><i class="typcn typcn-times"></i>Batalkan</button>
</a>
</div>
<div class="btn-group">
<button id="submit_button" class="btn btn-success btn-block">
<i id="loading_icon" style="display: none" class="fa fa-spin fa-spinner"></i>
<i id="submit_icon" class="typcn typcn-input-checked"></i>
Simpan</button>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</form>
how i fixed this problem?

The GET method is not supported for this route. Supported methods: POST, but it's POST method. - laravel

I need help. When I fill in the form and go to the POST method I get an error on the link The GET method is not supported for this route. Supported methods: POST. But i use POST method in form and in route. How do I fix this?
This is my code in index.blade.php
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#exampleModalCenter">
Add Hotel
</button>
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle">Add Hotel</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form class="form" action="{{ route('hotel.create.fusr') }}" enctype="multipart/form-data" method="POST" autocomplete="off">
#csrf
<div class="media">
<!-- upload and reset button -->
<div class="media-body mt-75 ml-1">
<h5>LOGO</h5>
<div class="form-group">
<div class="custom-file">
<input type="file" name="logo" class="custom-file-input" id="customFile" accept="image/*" />
<label class="custom-file-label" for="customFile">ფაილის არჩევა</label>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<br>
<br>
<p>allowed JPG, GIF or PNG.</p>
</div>
</div>
<!--/ upload and reset button -->
</div>
<br>
<div class="media">
<!-- upload and reset button -->
<div class="media-body mt-75 ml-1">
<h5>background</h5>
<div class="form-group">
<div class="custom-file">
<input type="file" name="background" class="custom-file-input" id="customFile" accept="image/*" />
<label class="custom-file-label" for="customFile">ფაილის არჩევა</label>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<br>
<br>
<p>allowed JPG, GIF or PNG.</p>
</div>
</div>
<!--/ upload and reset button -->
</div>
<div class="row">
<div class="col-md-6 col-12">
<div class="form-group">
<label for="title">სასტუმროს სახელი</label>
<input type="text" id="title" class="form-control" placeholder="სასტუმროს სახელი" name="name" />
</div>
</div>
</div>
<div class="col">
<div class="form-group">
<label for="school_creating">Hotel description</label>
<textarea class="form-control" id="school_creating" placeholder="Write your hotel description" name="description">
</textarea>
</div>
</div>
...
<div class="col-md-6 col-12">
<div class="form-group">
<label for="map">Google map</label>
<textarea type="text" id="map" class="form-control" name="map" placeholder="iframe" /></textarea>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Create Hotel</button>
</div>
</form>
</div>
</div>
</div>
This is my code in web.php
Route::post('user/dashboard/hotels/create', 'App\Http\Controllers\HotelsController#create_user')->name('hotel.create.fusr')->middleware(['auth', 'active_user']);
This is my code in HotelsController
public function create_user(Request $request)
{
$validated = $request->validate([
'name' => 'required|unique:hotels|max:255',
'logo' => 'required',
'background' => 'required',
'status' => 'required',
'address' => 'required|unique:hotels',
'email' => 'required|unique:hotels|max:255',
'phone' => 'required|unique:hotels|max:255',
'price' => 'required|max:255',
'description' => 'required|min:60',
'service' => 'required|min:60',
]);
$hotel = new Hotels;
$hotel->name = $request->name;
$hotel->description = $request->description;
$hotel->service_description = $request->service;
$hotel->slug = str_slug($hotel->name, "-");
$hotel->status = '2';
$hotel->address = $request->address;
$hotel->region = $request->region;
$hotel->country = $request->country;
$hotel->preprice = $request->preprice;
$hotel->price_per_day_person = $request->price;
$hotel->city_or_state = $request->city;
$hotel->website = $request->website;
$hotel->twitter = $request->twitter;
$hotel->facebook = $request->facebook;
$hotel->instagram = $request->insta;
$hotel->email = $request->email;
$hotel->phone = $request->phone;
$hotel->phone_two = $request->phone_two;
$hotel->google_map = $request->map;
$hotel->owner_id = auth()->user()->id;
if($request->hasFile('logo')){
$logo = $request->file('logo');
$filename = time() . '.' . $logo->getClientOriginalExtension();
Image::make($logo)->save( public_path('/uploads/hotels/' . $filename ) );
$hotel->logo = $filename;
}
if($request->hasFile('background')){
$background = $request->file('background');
$filename = time() . '.' . $background->getClientOriginalExtension();
Image::make($background)->save( public_path('/uploads/hotels/' . $filename ) );
$hotel->main_photo = $filename;
}
$hotel->save();
return redirect()->view('success.hotel');
}
I really need help. Thank you.
change your #csrf in index.blade.php to
<input type="hidden" name="_token" value="{{ csrf_token() }}">
there's two options to fix this issue
first
find if there's other route with same name
as
hotel.create.fusr
or same url of this route with diffrent method and delete it
second your route is put method
if same issue still happend
try to change route name and path for this route
and do next steps
php artisan cache:clear
php artisan route:clear

One form is working while other form is not working in laravel 5.4

I've UserController in which I've two options -
1) For Updating Profile
2) For Updating Password
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\User;
use Auth;
use Hash;
class UserController extends Controller
{
public function profile(){
return view('profile', array('user' => Auth::user()));
}
public function update_avatar(Request $request){
if(isset($request->avatar) && $request->avatar->getClientOriginalName()){
$ext = $request->avatar->getClientOriginalExtension();
$file = date('YmdHis').rand(1,99999).'.'.$ext;
$request->avatar->storeAs('public/avatar',$file);
}
else
{
$user = Auth::user();
if(!$user->avatar)
$file = '';
else
$file = $user->avatar;
}
$user = Auth::user();
$user->avatar = $file;
$user->name = $request->name;
$user->email = $request->email;
$user->mb_number = $request->mb_number;
$user->home_town = $request->home_town;
$user->save();
return view('profile', array('user' => Auth::user()));
}
public function update_password(Request $request){
$user = Auth::user();
if(Hash::check(Input::get('old_password'), $user['password']) && Input::get('new_password') == Input::get('confirm_new_password')){
$user->password = bcrypt(Input::get('new_password'));
$user->save();
}
return view('profile', array('user' => Auth::user()));
}
}
In my view blade, I've two forms -
update_avatar for updating profile like name, phone number and avatar.
update_password for updating password.
</div>
<div class="widget-user-image">
<img class="img-circle" src="{{ asset('public/storage/avatar/'.$user->avatar) }}" alt="User Avatar">
</div>
<div class="box-footer">
<div class="row">
<div class="col-sm-4 border-right">
<div class="description-block">
<h5 class="description-header">{{ $user->email }}</h5>
<span class="description-text">Email</span>
</div>
<!-- /.description-block -->
</div>
<!-- /.col -->
<div class="col-sm-4 border-right">
<div class="description-block">
<h5 class="description-header">{{ $user->name }}</h5>
<span class="description-text">{{ $user->home_town }}</span>
</div>
<!-- /.description-block -->
</div>
<!-- /.col -->
<div class="col-sm-4">
<div class="description-block">
<h5 class="description-header">{{ $user->mb_number }}</h5>
<span class="description-text">Phone No.</span>
</div>
<!-- /.description-block -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->
</div>
<!--
<div class="box-footer no-padding">
<ul class="nav nav-stacked">
<li>Projects <span class="pull-right badge bg-blue">31</span></li>
<li>Tasks <span class="pull-right badge bg-aqua">5</span></li>
<li>Completed Projects <span class="pull-right badge bg-green">12</span></li>
<li>Followers <span class="pull-right badge bg-red">842</span></li>
</ul>
</div>
-->
</div>
</div>
<section class="content">
<div class="container-fluid">
<form action="/profile" enctype="multipart/form-data" method="POST">
<div class="form-group">
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" placeholder="Title" value="{{$user->name}}">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="email" class="form-control" id="email" placeholder="Description" value="{{$user->email}}" readonly>
</div>
<div class="form-group">
<label for="mb_number">Mobile No.</label>
<input type="text" name="mb_number" class="form-control" id="mb_number" placeholder="Schedule" value="{{$user->mb_number}}">
</div>
<div class="form-group">
<label for="home_town">Home Town</label>
<input type="text" name="home_town" class="form-control" id="home_town" placeholder="Deadline" value="{{$user->home_town}}">
</div>
<div class="form-group">
<label>Update Profile Image</label>
<input type="file" name="avatar">
#if($user->avatar)
<img src="{{ asset('public/storage/avatar/'.$user->avatar) }}" style="width:150px;">
#endif
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}"
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
</section>
<section class="content">
<div class="container-fluid">
<form action="/profile" enctype="multipart/form-data" method="POST">
<div class="form-group">
<div class="form-group">
<label for="old_password">Old Password</label>
<input type="password" name="old_password" class="form-control" id="old_password" placeholder="Old Password">
</div>
<div class="form-group">
<label for="new_password">New Password</label>
<input type="password" name="new_password" class="form-control" id="new_password" placeholder="New Password">
</div>
<div class="form-group">
<label for="confirm_new_password">Confirm New Password </label>
<input type="password" name="confirm_new_password" class="form-control" id="confirm_new_password" placeholder="Confirm New Password">
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}"
<button type="submit" class="btn btn-primary">Update Password</button>
</div>
</div>
</section>
update_password function is working fine but update_avatar function is not working neither it's showing any error. I've tried dd($user) but still not giving output to dd.
Check this out i updated form a bit with indentation and proper closing, also please redirect profile avatar to different URL so that it can access different method :-
I would suggest you to use https://laravelcollective.com/docs/5.4/html , by using form collective it would be much more easier to implement more complex form structure.
In Route,
Route::post('/profile_avatar',"UserController#update_avatar");
Route::post('/profile_password',"UserController#update_password");
In Blade,
<section class="content">
<div class="container-fluid">
<form action="/profile_avatar" enctype="multipart/form-data" method="POST">
<div class="form-group">
<div class="form-group">
<label for="name">
Name
</label>
<input class="form-control" id="name" name="name" placeholder="Title" type="text" value="{{$user->name}}">
</input>
</div>
<div class="form-group">
<label for="email">
Email
</label>
<input class="form-control" id="email" name="email" placeholder="Description" readonly="" type="text" value="{{$user->email}}">
</input>
</div>
<div class="form-group">
<label for="mb_number">
Mobile No.
</label>
<input class="form-control" id="mb_number" name="mb_number" placeholder="Schedule" type="text" value="{{$user->mb_number}}">
</input>
</div>
<div class="form-group">
<label for="home_town">
Home Town
</label>
<input class="form-control" id="home_town" name="home_town" placeholder="Deadline" type="text" value="{{$user->home_town}}">
</input>
</div>
<div class="form-group">
<label>
Update Profile Image
</label>
<input name="avatar" type="file">
#if($user->avatar)
<img src="{{ asset('public/storage/avatar/'.$user->avatar) }}" style="width:150px;">
#endif
</img>
</input>
</div>
<input <a="" class="btn btn-info" href="" name="_token" type="submit" value="{{ csrf_token() }}"/>
</div>
<button class="btn btn-primary" type="submit">
Update
</button>
</form>
</div>
</section>
<section class="content">
<div class="container-fluid">
<form action="/profile_password" enctype="multipart/form-data" method="POST">
<div class="form-group">
<div class="form-group">
<label for="old_password">
Old Password
</label>
<input class="form-control" id="old_password" name="old_password" placeholder="Old Password" type="password">
</input>
</div>
<div class="form-group">
<label for="new_password">
New Password
</label>
<input class="form-control" id="new_password" name="new_password" placeholder="New Password" type="password">
</input>
</div>
<div class="form-group">
<label for="confirm_new_password">
Confirm New Password
</label>
<input class="form-control" id="confirm_new_password" name="confirm_new_password" placeholder="Confirm New Password" type="password">
</input>
</div>
<input <a="" class="btn btn-info" href="" name="_token" type="submit" value="{{ csrf_token() }}"/>
</div>
<button class="btn btn-primary" type="submit">
Update Password
</button>
</form>
</div>
</section>

Categories