I have this code in controller to update the data from database :
public function update(Request $request, $id)
{
$anodizing = Anodizing::find($id);
$anodizing->date= $request->date;
$anodizing->number= $request->number;
$anodizing->item_total = $request->item_total;
$anodizing->desc= $request->desc;
if ($request->hasFile('picture')) {
$anodizing_image = public_path("uploads/reports/anodizing/{$anodizing->picture}");
if (File::exists($anodizing_image)) {
File::delete($anodizing_image);
};
$file = $request->file('picture');
$extension = $file->getClientOriginalExtension();
$filename = $request->number. '-' . date('YmdHms') . '.' . $extension;
$file->move('uploads/reports/anodizing', $filename);
$anodizing->picture= $filename;
}
$anodizing->save();
$id = $anodizing->id;
foreach ($request->addmore as $key => $value) {
$anodizingdetail = AnodizingDetail::find($value['id']);
$anodizingdetail->aluminium_id= $value['name'];
$anodizingdetail->qty = $value['qty'];
$anodizingdetail->weight= $value['weight'];
$anodizingdetail->save();
}
Basically this update method works perfectly to update or edit existing data, but the problem is, what to do if I want to edit and then insert a new row in the detail form ?
I'm aware of updateorCreate method in laravel, is that the right method ? How to use that ? or I need to use something else ?
as you said, instead of AnodizingDetail::find use findOrNew
docs: https://laravel.com/docs/master/eloquent-relationships#the-create-method
Related
It is so long that I have been dealing with this but no result have I achieved yet. So I decided to ask for your help.
I have 3 tables => article, category and country and I want to connect these tables.
Each category can have multiple articles but each article is related to only one category.
Each country can have multiple articles but each article is related to only one country.
The problem is with the ArticleController part which works for connecting only one table to article but for connecting both tables to it, I receive this error:
SQLSTATE[HY000]: General error: 1364 Field 'country_id' doesn't have a default value
and also I have country_id and category_id as my foreign keys in articles table.
Below are my tables:
article model:
public function countries(){
return $this->belongsTo('App\country');
}
public function categories(){
return $this->belongsTo('App\category');
}
country model
public function articles(){
return $this->hasMany('App\Article');
}
category model
public function articles(){
return $this->belongsToMany('App\Article');
}
ArticleController - and the main part = the problem
public function store(Request $request)
{
$article = new article(
[
'title' => $request->input('title'),
'top_content' => $request->input('top_content'),
'quote' => $request->input('quote'),
'left_content' => $request->input('left_content'),
'right_content' => $request->input('right_content'),
]
);
if ($request->hasFile('article_slider_image')) {
$file = time() . '_' . $request->file('article_slider_image')->getClientOriginalName();
$destination = base_path() . '/public/images/articleSliderImages';
$request->file('article_slider_image')->move($destination, $file);
$article->article_slider_image = $file;
}
if ($request->hasFile('left_image')) {
$file = time() . '_' . $request->file('left_image')->getClientOriginalName();
$destination = base_path() . '/public/images/articleLeftImages';
$request->file('left_image')->move($destination, $file);
$article->left_image = $file;
}
if ($request->hasFile('right_image')) {
$file = time() . '_' . $request->file('right_image')->getClientOriginalName();
$destination = base_path() . '/public/images/articleRightImages';
$request->file('right_image')->move($destination, $file);
$article->right_image = $file;
}
$country = country::where('name',$request->input('country'))->first();
$category = category::where('name',$request->input('category'))->first();
//$article->category_id = 1; //not commenting this part works fine
$country->articles()->save($article);
$category->articles()->save($article);// but when I use this one, it gives me error
//dd($category->id);
$article->save();
return redirect()->route('article.index')->with('success', 'article created successfully' . $request->title);
}
I will really appreciate if someone helps.
You don't need to load the whole model to save an article, you just need an id, therefore:
$article->country_id = country::where('name',$request->input('country'))->pluck('id')->first();
$article->category_id = category::where('name',$request->input('category'))->pluck('id')->first();
And finally
$article->save();
SQLSTATE[HY000]: General error: 1364 Field 'country_id' doesn't have a
default value
The above error means whenever your are inserting data to database,you are not passing value of country_id and database searching default value and you have not assigned default value for country_id in db table.
Try this, i have assigned value to country_id and category_id before saving the article
public function store(Request $request)
{
$article = new article(
[
'title' => $request->input('title'),
'top_content' => $request->input('top_content'),
'quote' => $request->input('quote'),
'left_content' => $request->input('left_content'),
'right_content' => $request->input('right_content'),
]
);
if ($request->hasFile('article_slider_image')) {
$file = time() . '_' . $request->file('article_slider_image')->getClientOriginalName();
$destination = base_path() . '/public/images/articleSliderImages';
$request->file('article_slider_image')->move($destination, $file);
$article->article_slider_image = $file;
}
if ($request->hasFile('left_image')) {
$file = time() . '_' . $request->file('left_image')->getClientOriginalName();
$destination = base_path() . '/public/images/articleLeftImages';
$request->file('left_image')->move($destination, $file);
$article->left_image = $file;
}
if ($request->hasFile('right_image')) {
$file = time() . '_' . $request->file('right_image')->getClientOriginalName();
$destination = base_path() . '/public/images/articleRightImages';
$request->file('right_image')->move($destination, $file);
$article->right_image = $file;
}
$country = country::where('name',$request->input('country'))->first();
$category = category::where('name',$request->input('category'))->first();
$article->country_id = $country->id;
$article->category_id = $category->id;
$article->save();
return redirect()->route('article.index')->with('success', 'article created successfully' . $request->title);
}
SQLSTATE[HY000]: General error: 1364 Field 'country_id' doesn't have a default value
in this error you can easily see country_id doesn't have a default
it means whenever your are inserting data to data base it is not passing any data and database searching default value
so you code your be
if(!$country){
return redirect()->route('article.index')->with('error', 'country not found.');
}
$article->country_id = $country->id;
$article->category_id = $category->id;
$article->save();
I have a form with a Dropzone div, for massive image uploads with another data.
But, as the images are uploaded with ajax I need to know which Id's are assigned to those images.
The problem became when I save those ids in the session data. It just saves the first and the latest ids.
For example, when I submit 4 images it returns me the second and the last id.
😒
My controller method (via Ajax):
public function store(Request $request)
{
$photos = $request->file('file');
if (!is_array($photos))
$photos = [$photos];
if (!is_dir($this->photos_path))
mkdir($this->photos_path, 0777);
for ($i = 0; $i < count($photos); $i++) {
$photo = $photos[$i];
$name = sha1(date('YmdHis') . str_random(30));
$save_name = $name . '.' . $photo->getClientOriginalExtension();
$resize_name = $name . str_random(2) . '.' . $photo->getClientOriginalExtension();
Image::make($photo)
->resize(250, null, function ($constraints) {
$constraints->aspectRatio();
})
->save($this->photos_path . '/' . $resize_name);
$photo->move($this->photos_path, $save_name);
$upload = new UploadedImages();
$upload->filename = $save_name;
$upload->resized_name = $resize_name;
$upload->original_name = basename($photo->getClientOriginalName());
$upload->user_id = Auth::id();
$upload->save();
Session::push('uploaded_images_ids', $upload->id);
}
return Response::json(['message' => 'Image saved Successfully'], 200);
}
Response with Debug bar (should response 4 ids, not 2):
You can create an array to store your image and id
Here is the idea below
$image_array = [];
for ($i = 0; $i < count($photos); $i++) {
....
array_push($image_array,['image_name' => $save_name, 'image_id' => $upload->id]);
}
I was getting the relationship as in laravel 5.3 and was working fine:
//execute the relation of the given model
$data = $model->{$info["relation"]}();
// get the type of the relation
$class = get_class($data);
$dataType = explode("\\", $class);
$relationType = end($dataType);
$options["columns"][$key]["relationType"] = $relationType;
// if its a simple belongs-to statement
if($relationType == "BelongsTo") {
// get all belongs-to query info
$otherTable = $data->getRelated()->getTable();
$foreignKey = $data->getQualifiedForeignKey();
$otherKey = $data->getOtherKey();
// manually join using it
$retrievedRecords->leftJoin($otherTable . ' as ' . $info["relation"], $info["relation"] . '.' . $otherKey, '=', $foreignKey);
} else if($relationType == "HasMany" || $relationType == "HasOne") {
// get all has-many query info
$otherTable = $data->getRelated()->getTable();
$foreignKey = $data->getPlainForeignKey();
$parentKey = $data->getQualifiedParentKeyName();
// manually join using it
$retrievedRecords->leftJoin($otherTable . ' as ' . $info["relation"], $info["relation"] . '.' . $foreignKey, '=', $parentKey);
}
Now i downloaded fresh laravel 5.4 and it gives me error :
Call to undefined method Illuminate\Database\Query\Builder::getOtherKey()
As the getOtherKey() exists in the above code in if() section.
Is there any alternative for that ?
The getOtherKey method has been renamed to getOwnerKey. So you can get the owner key by saying:
$ownerKey = $data->getOwnerKey();
I am trying to upload an external list of "groups" to add to my custom Joomla 3 component. I have created a CSV file and written a few functions that I hope will do it. I have created a custom button to start the task in my "groups" view.
When I push the button I get an SQL error that has absoloutle nothing to do with the functions so I have tried debugging and when the button is pressed its not even getting to my controller task before the sql error. I am so confused as to why.
This is the code I have
view.html.php TestViewGroups
JToolBarHelper::custom('group.uploadsave', '', '', 'Upload and Save', false);
TestControllerGroup
protected function uploadsave() {
$detail_headers = array(
'agm_date',
'preferred_media'
);
$rows = array_map('str_getcsv', file('groupdata.csv'));
$header = array_shift($rows);
foreach ($rows as $row) {
$entry = array_combine($header, $row);
foreach ($entry as $key => $value) {
if(in_array($key, $detail_headers)){
$details[$key]= $value;
unset($entry[$key]);
}
}
$entry['details'] = $details;
$this->saveUploaded($entry);
}
// Redirect to the list screen.
$this->setRedirect(
JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_list
. $this->getRedirectToListAppend(), false
)
);
}
protected function saveUploaded($dataIn = array()) {
$app = JFactory::getApplication();
$lang = JFactory::getLanguage();
$model = $this->getModel();
$table = $model->getTable();
$data = $dataIn;
$checkin = property_exists($table, 'checked_out');
// Determine the name of the primary key for the data.
if (empty($key))
{
$key = $table->getKeyName();
}
// To avoid data collisions the urlVar may be different from the primary key.
if (empty($urlVar))
{
$urlVar = $key;
}
$recordId = $this->input->getInt($urlVar);
// Populate the row id from the session.
$data[$key] = $recordId;
if (!$model->save($validData))
{
// Redirect back to the edit screen.
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()));
$this->setMessage($this->getError(), 'error');
}
if ($checkin && $model->checkin($validData[$key]) === false)
{
// Save the data in the session.
$app->setUserState($context . '.data', $validData);
// Check-in failed, so go back to the record and display a notice.
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_CHECKIN_FAILED', $model->getError()));
$this->setMessage($this->getError(), 'error');
}
$this->setMessage(
JText::_(
($lang->hasKey($this->text_prefix . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS')
? $this->text_prefix
: 'JLIB_APPLICATION') . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS'
)
);
}
I am not using this as a regular function, its just a once off to upload the data initially.
The SQL error I am getting is like it is trying to load a list of groups?? not anything to do with the save function at all.
The saveUploaded is a similar function to the initial save function.
Thanks :-)
**** Edit *****
I have just followed the task through with debug and its getting to the execute task methotd of JControllerLegacy and because the task is not defined in the task map its defaulting to display, hence the SQL error trying to load a group when it doesn't have an ID. Do I need to now register a task in the task map before it will pick it up?
I am officially an idiot! When I just logged back on to see if anyone had responded I saw that I had declared the function as a protected function!! dir! I just copied and pasted from another function and forgot to change its access. I also made a few other changes and now it works quite well!
public function uploadsave() {
// An array of headers that will need to be entered into a seperate array to allow entry as JSON
$detail_headers = array(
'agm_date',
'preferred_media'
);
$app = JFactory::getApplication();
$lang = JFactory::getLanguage();
$model = $this->getModel();
$path = JPATH_COMPONENT . '/controllers/groupdata.csv';
//Load the file and pass each line into an array.
$rows = array_map('str_getcsv', file($path));
//Take out the first line as it is the headers.
$header = array_shift($rows);
//turn each of the arrays into an entry
foreach ($rows as $row) {
$entry = array_combine($header, $row);
foreach ($entry as $key => $value) {
//separate each of the entries that need to be entered into an array to be stored as JSON
if(in_array($key, $detail_headers)){
$details[$key]= $value;
unset($entry[$key]);
}
}
$entry['details'] = $details;
$recordId = 'id';
// Populate the row id from the session.
$entry[$key] = $recordId;
//Save each one
if (!$model->save($entry))
{
// Redirect back to the edit screen.
$this->setError(JText::sprintf('JLIB_APPLICATION_ERROR_SAVE_FAILED', $model->getError()));
$this->setMessage($this->getError(), 'error');
return false;
}
$this->setMessage(
JText::_(
($lang->hasKey($this->text_prefix . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS')
? $this->text_prefix
: 'JLIB_APPLICATION') . ($recordId == 0 && $app->isSite() ? '_SUBMIT' : '') . '_SAVE_SUCCESS'
)
);
}
// Redirect to the list screen.
$this->setRedirect(
JRoute::_(
'index.php?option=' . $this->option . '&view=' . $this->view_list
. $this->getRedirectToListAppend(), false
)
);
}
How to use jQuery-File-Upload with PHP and database?
I want to insert or delete rows about images when I upload or delete images and that name of each image will be as time() when they are uploaded to the directory.
All result I found through google tell me that I need edit to upload.class.php but the last release has index.php and UploadHandler.php only...
file UploadHandler.php has class UploadHandler with code
public function post($print_response = true) {
if (isset($_REQUEST['_method']) && $_REQUEST['_method'] === 'DELETE') {
return $this->delete($print_response);
}
$upload = isset($_FILES[$this->options['param_name']]) ?
$_FILES[$this->options['param_name']] : null;
// Parse the Content-Disposition header, if available:
$file_name = isset($_SERVER['HTTP_CONTENT_DISPOSITION']) ?
rawurldecode(preg_replace(
'/(^[^"]+")|("$)/',
'',
$_SERVER['HTTP_CONTENT_DISPOSITION']
)) : null;
$file_type = isset($_SERVER['HTTP_CONTENT_DESCRIPTION']) ?
$_SERVER['HTTP_CONTENT_DESCRIPTION'] : null;
// Parse the Content-Range header, which has the following form:
// Content-Range: bytes 0-524287/2000000
$content_range = isset($_SERVER['HTTP_CONTENT_RANGE']) ?
preg_split('/[^0-9]+/', $_SERVER['HTTP_CONTENT_RANGE']) : null;
$size = $content_range ? $content_range[3] : null;
$info = array();
if ($upload && is_array($upload['tmp_name'])) {
// param_name is an array identifier like "files[]",
// $_FILES is a multi-dimensional array:
foreach ($upload['tmp_name'] as $index => $value) {
$info[] = $this->handle_file_upload(
$upload['tmp_name'][$index],
$file_name ? $file_name : $upload['name'][$index],
$size ? $size : $upload['size'][$index],
$file_type ? $file_type : $upload['type'][$index],
$upload['error'][$index],
$index,
$content_range
);
}
} else {
// param_name is a single object identifier like "file",
// $_FILES is a one-dimensional array:
$info[] = $this->handle_file_upload(
isset($upload['tmp_name']) ? $upload['tmp_name'] : null,
$file_name ? $file_name : (isset($upload['name']) ?
$upload['name'] : null),
$size ? $size : (isset($upload['size']) ?
$upload['size'] : $_SERVER['CONTENT_LENGTH']),
$file_type ? $file_type : (isset($upload['type']) ?
$upload['type'] : $_SERVER['CONTENT_TYPE']),
isset($upload['error']) ? $upload['error'] : null,
null,
$content_range
);
}
return $this->generate_response($info, $print_response);
}
public function delete($print_response = true) {
$file_name = $this->get_file_name_param();
$file_path = $this->get_upload_path($file_name);
$success = is_file($file_path) && $file_name[0] !== '.' && unlink($file_path);
if ($success) {
foreach($this->options['image_versions'] as $version => $options) {
if (!empty($version)) {
$file = $this->get_upload_path($file_name, $version);
if (is_file($file)) {
unlink($file);
}
}
}
}
return $this->generate_response($success, $print_response);
}
What rows do I need to add to insert or delete file names in mysql?
P.S.: I use PHP
I use docementation and now can said that instead file upload.class.php need edit file UploadHandler.php
and than use next:
Search this line - > $this->options = array(
Add the following Code in the next lines :
// mysql connection settings
'database' => '**YOUR DATABASE**',
'host' => '**localhost**',
'username' => '**YOUR USERNAME**',
'password' => '**YOUR PASSWORD**',
// end
So now you have to write a function for the SQL Query, copy & paste the following code for example after the handle_file_upload function :
function query($query) {
$database = $this->options['database'];
$host = $this->options['host'];
$username = $this->options['username'];
$password = $this->options['password'];
$link = mysql_connect($host,$username,$password);
if (!$link) {
die(mysql_error());
}
$db_selected = mysql_select_db($database);
if (!$db_selected) {
die(mysql_error());
}
$result = mysql_query($query);
mysql_close($link);
return $result;
}
Add file details to database
I explain this function with a picture upload, so here we save the picture name to the database Add this function also too the upload.class.php
function add_img($whichimg)
{
$add_to_db = $this->query("INSERT INTO yourtable (**yourcolumnone**) VALUES ('".$whichimg."')") or die(mysql_error());
return $add_to_db;
}
so in this function we call the function query with the string between the clamps.
You could also insert other details too, for example, the file size.
At least we have to call this function, with the following code at the end of the function handle_file_upload. Paste the following code underneath or over this line : $file->size = $file_size;
$file->upload_to_db = $this->add_img($file->name);
Delete the entry we created
Deleting the entry we made before is very easy, we create a new function which makes also an sql query to delete it.
function delete_img($delimg)
{
$delete_from_db = $this->query("DELETE FROM yourtable WHERE yourcolumnone = '$delimg'") or die(mysql_error());
return $delete_from_db;
}
Now we must call the function, this time of the delete function.
Go to the delete function and search this line : if ($success) { paste the following code over this.
$this->delete_img($file_name);
Enjoy=)
You should overload the default methods (as described in the documentation) in order to add your custom features.
If you modify original files, you'll have more work when a new version (or a fix) of the plugin is released.
For my own purposes, I basically defined a custom class which extends the original one and modified only the /server/php/index.php file:
class myUploadHandler extends UploadHandler {
//do your stuff
//for exemple if you are using a DB:
//overload methods like handle_file_upload() for insertion in a DB,
//set_additional_file_properties() if you need more fields linked to each uploaded file
// or delete() also if you want to remove from DB
}
$upload_handler = new myUploadHandler(array(
'user_dirs' => true,
'download_via_php' => true,
));