I'm trying to save an image from a form into database. I tried this but it's not working:
public function store(Request $request)
{
$article = new article;
$photo_articles = new photo_articles;
// $type = new type;
$article->NOM_ARTICLE = $request->NOM_ARTICLE;
$article->DESCRIPTION_ARTICLE = $request->DESCRIPTION_ARTICLE;
$article->id = auth()->user()->id;
$article->TYPE_ARTICLE = $request->LABEL_TYPE;
$article->save();
$photo_articles->PHOTO_ARTICLE = base64_encode(file_get_contents($request->PHOTO_ARTICLE));
$photo_articles->ID_ARTICLE = $article->ID_ARTICLE;
$photo_articles->save();
return;
}
Here is my form:
<form method="post" action="{{ route('addarticle.store') }}" class="contact_form text-center" id="contact_form">
{{ csrf_field() }}
<div class="row">
<div class="col-lg-6">
<div class="col-lg-12">
<input type="text" class="contact_input" name="NOM_ARTICLE" placeholder="Nom d'article"
required="required">
</div>
<div class="col-lg-12">
<select class="contact_input" name="LABEL_TYPE">
#foreach($types as $type)
<option> {{$type->LABEL_TYPE}}</option>
#endforeach
</select>
</div>
</div>
<div class="col-lg-6">
<div id="uploading" class="uploadfile">
<input type="hidden" name="MAX_FILE_SIZE" value="250000"/>
<input type="file" class="contact_input uploadFileInput" id="imagearticle" name="PHOTO_ARTICLE"
placeholder="Capture de votre article" name="fic" size=50 required="required"/>
<p id="uploadtextid" class="uploadText">upload image</p>
<img class="uploadImage" src="" id="displayedimage">
</div>
</div>
<div class="col-lg-12">
<textarea class="contact_textarea contact_input" name="DESCRIPTION_ARTICLE" placeholder="Description"
required="required"></textarea>
</div>
<button class="contact_button right" type="submit">Valider!</button>
</div>
</form>
My image is in $request->PHOTO_ARTICLE.
Can someone show me how to save it as base64? I've searched a lot but without result.
When you're submitting a file with a form you have to set add the attribute
enctype="multipart/form-data" to the opening form tag:
<form enctype="multipart/form-data" method="post" action="{{ route('addarticle.store') }}" class="contact_form text-center" id="contact_form">
otherwise it will only submit the name of the file and not the file itself.
Then in your route you would just need:
$photo_articles->PHOTO_ARTICLE = base64_encode(
file_get_contents($request->file('PHOTO_ARTICLE')->path())
);
Related
I'm creating a basic upload form with different input fields, but once I submit my form I'm getting the error :
Call to a member function storeAs() on null
I checked the problem it could have been and everyone was talking about adding the enctype="multipart/form-data" tag on the form... which is already present, so now I really have no clue about what my error could be...
drive.blade.php :
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Drive') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
<form action="/postForm" enctype="multipart/form-data">
<label for="file">File:</label><br>
<input type="file" id="file" name="file"><br>
<label for="folder_id">Folder ID :</label><br>
<input type="text" id="folder_id" name="folder_id"><br>
<label for="organisation_id">Organisation ID :</label><br>
<input type="text" id="organisation_id" name="organisation_id"><br>
<label for="organisation_type">Organisation type :</label><br>
<input type="text" id="organisation_type" name="organisation_type"><br>
<input type="submit" value="Submit"><br>
</form>
</div>
</div>
</div>
</x-app-layout>
My route : Route::get('postForm', "App\Http\Controllers\FileController#store");
My controller:
public function store(Request $request)
{
$userLogged = Auth::user();
$document = $request->file("file");
// dd($document);
$organisation_type = $request->organisation_type;
$organisation_id = $request->organisation_id;
$folder_id = $request->folder_id;
$path = $document->storeAs('public', "tempName");
return response()->json(["result" => "ok"], 201);
}
The Error Is Showing To me 419 Page Expired Because You Don't Add
#csrf In Your Blade
Or You Can Add <input value='csrf_token()' hidden>
My update function in the resource controller
public function update(UpdateOeuvreRequest $request, Oeuvre $oeuvre)
{
$oeuvre->titre = $request->input('titre');
$oeuvre->auteur = $request->input('auteur');
$oeuvre->annee = $request->input('annee');
$oeuvre->description = $request->input('description');
$oeuvre->category_id = $request->input('category_id');
$oeuvre->qt = $request->input('qt');
$query=$oeuvre->save();
if($query){
return redirect()->route('Oeuvre')->with('success','updated successfuly');
}
}
MY edit.blade.php
<form action="{{ route('Oeuvre.update',$oeuvre) }}" enctype="multipart/form-data" method="POST">
#csrf
#method('PUT')
<div class="row mb-3">
<div class="col-md-6">
<div class="form-floating mb-3 mb-md-0">
<input class="form-control" value="{{$oeuvre->titre}}" name="titre" type="text" placeholder="titre" />
<label for="titre">Titre</label>
</div>
</div>
<div class="col-md-6">
<div class="form-floating">
<input class="form-control" value="{{$oeuvre->auteur}}" name="auteur" type="text" placeholder="auteur" />
<label for="auteur">Auteur</label>
</div>
</div>
I think what is happening is that you are using this parameter in your controller =>
$oeuvre
Then you have to pass it also through your form and route
I tried to create adding/creating a new menu and saved it in the database. However, when I clicked the button, my system didn't show any error but the data is not saved in the database.
adminAddMenu.blade.php
<form>
#csrf
<div class="form-group row">
<label for="categorycode" class="col-sm-3 col-form-label">Category Code</label>
<div class="col-md-4">
<input name="category_code" value="{{old('category_code')}}" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="menutitle" class="col-sm-3 col-form-label">Menu Title</label>
<div class="col-md-4">
<input name="menu_title" value="{{old('menu_title')}}" class="form-control">
</div>
</div>
<div class="form-group row">
<label for="menuprice" class="col-sm-3 col-form-label">Menu Price</label>
<div class="col-md-4">
<textarea name = "menu_price" value="{{old('menu_price')}}" class="form-control"></textarea>
</div>
</div>
<div class="form-group row">
<div class="offset-sm-3 col-sm-9">
<button type="submit" class="btn btn-primary" href="">Submit</button>
</div>
</div>
</div>
</form>
AdminMenuController.php
public function store(Request $request)
{
$menu = new \App\Menu;
$menu->category_code = $request->category_code;
$menu->menu_title = $request->menu_title;
$menu->menu_price = $request->menu_price;
$menu->save();
Session::flash('flash_message', 'Menu is successful! added');
return redirect()->back();
}
web.php
Route::resource('/menus', 'AdminMenuController');
You don't give the form action method where your data will submit. Like you want to send your form data to store method in your controller. So you have to write follow:
<form method="POST" action="{{ route('your route name') }}">
I think you miss the action attribute of your form. Please add action to your form
<form action="{{ YOUR_ROUTER }}">
...
Please add action on form tag.
Like as-
<form action={{ route("your route write here") }} method="post">
I think now it's work's.
I am using laravel to make form upload, it worked well before. But, now it doesn't work.
I've tried to debug my code using dd($request->all(), but my photo file was missing.
This is my view code
<form action="" method="post" class="form-horizontal form-bordered" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label class="col-xs-2 col-md-1 control-label" style="text-align: left;position: relative;right: -8px;">
<div class="row">
<span class="fa fa-file-image-o" aria-hidden="true" style="position: relative;font-size: 20px;top: 0px;text-align: left;color: #ADADAD;"></span>
</div>
</label>
<div class="col-xs-10 col-md-11">
<input name="photo" value="" class="form-control" style="padding-top: 5px;height: auto;" type="file">
</div>
</div>
</form>
and this is the controller code
public function store(Request $request)
{
...
dump($request->all());
if ($request->hasFile('photo')) {
$file = $request->file('photo');
$timestamp = 21;
$name = $timestamp. '-' .$file->getClientOriginalName();
$Members->photo = $name;
$n = Image::make(Input::file('photo'));
$n->resize(330, 330);
$path = 'uploads/'.$name;
$n->save($path);
}
}
PS. I was modified php.ini file before, does it matter?
the edit doesn't work when i click on edit button it recover the values of the input but do not modify it in the database
Edit.blade.php
#extends('index')
#section('content')
<script language="javascript" type="text/javascript" src={{ URL::to('js/extension.js') }}></script>
<div class="form-group" style="margin-top: 100px;">
<form class="forms-sample" action="/documentSideBar" method="get" enctype="multipart/form-data">
#csrf
#method('PATCH')
<div class="form-group">
<label for="nomDocument">Nom document</label>
<input type="text" class="form-control" id="nomDocument" name="nomDocument"
value="{{$document->nomDocument}}" onChange='getoutput2()'>
</div>
<div class="form-group">
<label for="description">Description</label>
<input type="text" class="form-control" id="description" name="description"
value="{{$document->description}}">
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group row">
<label class="col-sm-3 col-form-label">Type</label>
<div class="col-sm-9">
<select class="form-control">
<option>Documents administratifs</option>
<option>Documents financiƩres</option>
</select>
</div>
</div>
</div>
</div>
<div class="form-group">
<label for="fille">Importer un fichier</label>
<input type="file" onChange='getoutput()' name="file" id="fille" class="form-control">
<input id='extension' type='hidden' name='extension'>
<input id="pathe" type="hidden" name="path">
</div>
<button type="submit" class="btn btn-success mr-2">Modifier</button>
<button class="btn btn-light">Annuler</button>
</form>
</div>
#endsection
documentSideBar.blade.php
<td style="width:300px">
<a href="/download/{{$document->id}}" class="btn btn-success"><i
class="glyphicon glyphicon-download-alt"></i></a>
<a href="/documentSideBar/{{$document->id}}/edit" style="float:right" class="btn btn-primary">
<i class="glyphicon glyphicon-edit"></i>
</a>
<form action="{{ action('DocumentsController#destroy', $document->id)}}" method="POST">
#method('DELETE')
#csrf
<button class="btn btn-danger"><i
class="glyphicon glyphicon-trash"></i></button>
</form>
</td>
this is the edit and update method in the controller
DocumentsController.php
public function edit($id)
{
$document = Document::findOrFail($id);
return view('documents.edit', compact('document'));
}
public function update(Document $document)
{
$document->nomDocument = request('nomDocument');
$document->description = request('description');
$d=request('path');
if(isset($d)){
$document->path ='username'.'/'.request('path');
}
$document->save();
return redirect('/documentSideBar');
}
public function show($id)
{
$document = Document::findOrFail($id);
return view('documentSideBar', compact('document'));
}
also i I want to adjust the buttons , idon't how to make the space between them
the error was in the web.php : here is the right route
Route::resource('documentSideBar','DocumentsController');
Route::get('download/{id}','DocumentsController#download');
Route::get('show/{id}','DocumentsController#show');
Route::post('/update/{id}','DocumentsController#update');
You should change your form method to POST, and change your action to the resource pattern:
<form class="forms-sample" action="{{route('documentSideBar.update', $document->id)}}" method="POST" enctype="multipart/form-data"
#csrf
#method('PATCH')
When you're modifying data in your database you should use POST, not GET.
Take a look here to see more information about the methods.
You also should take a look at the Resource Controller section to understand more about what are you using