Save data of input files multiple - laravel

I'm trying to save the list of files in the post but it does not save the relationship.
I want to save the first post and then add the images inserted.
class SaveData extends ComponentBase {
public function onSubmitContact() {
/*
* This field: <input type="file" name="files[]" id='files' multiple="true"/>
*/
$files = Input::file('files');
$modelFiles = new MyModeToSave;
$modelRelation = new ModelToRelation;
foreach($files as $file):
$modelFiles->data = $file;
$modelFiles->save();
endforeach;
$modelRelation->title = post('title');
/* more fields */
$modelRelation->save();
$modelRelation->files()->add($modelFiles);
}
}
tanks
Code this is CMS OCTOBER

Not sure on your exact use case, but something like this should work:
//Pass your model id, and then find it in your function
$model = ModelToRelation::find(post('modelID'));
//commit all the files relations
$model->commitDeferred(post('_session_key'));
foreach($model->files() as $file) {
//now you can do stuff with the file...or model
}

Related

How to save data on different database tables in Laravel Backpack

how to save the data from the Features fields on a different table. Example in the link below, it is saving the Features table fields in a JSON field in the database. However, I want to save this data from the features into another table.
https://demo.backpackforelavel.com/admin/product/211/Edit
I'm coming back here to post my answer. I managed to settle, I'm putting here to help other developers.
This first part of the question I have already solved. But now I can not bring the data from the Features fields in the form.
Below is the source code that I was able to save and edit the form data. However, I can not carry the data from the Feature fields. Someone knows how I can carry the field data in Feature
class ProductCrudController extends CrudController
{
use ListOperation;
use CreateOperation {
store as traitStore;
}
use UpdateOperation {
update as traitUpdate;
}
use DeleteOperation;
public function store()
{
// insert item in the db
$item = $this->crud->create($this->crud->getRequest()->except(['save_action', '_token', '_method']));
$features = json_decode($this->crud->getRequest()->input('features'));
foreach ($features as $itens) {
$obj = new Feature();
$obj->product_id = $item->getKey();
$obj->name = $itens->name;
$obj->save();
}
// show a success message
\Alert::success(trans('backpack::crud.insert_success'))->flash();
return redirect('admin/product');
}
public function update()
{
$featureForm = json_decode($this->crud->getRequest()->input('features'));
// insert item in the db
$item = $this->crud->update($this->crud->getRequest()->id, $this->crud->getRequest()->except(['save_action', '_token', '_method', 'features']));
$objF = Feature::where('product_id', $item->getKey())->get();
foreach ($objF as $itens) {
Feature::where('id', $itens->id)->delete();
}
foreach ($featureForm as $itens) {
$obj = new Feature;
$obj->product_id = $item->getKey();
$obj->name = $itens->name;
$obj->save();
}
// show a success message
\Alert::success(trans('backpack::crud.insert_success'))->flash();
return redirect('admin/product');
}
}
I'm coming back here to post my answer. I managed to settle, I'm putting here to help other developers.
I was able to bring the data in the form edition, in the Features fields. I used the Mutators in the Model - https://laravel.com/docs/8.x/eloquent-mutators
Below is the example I did.
It would be interesting to have an example of this in the official documentation or demo project. I believe it would help a lot also to other developers.
//model Product
public function getFeaturesAttribute($value)
{
$objects = Features::where('product_id', $this->id)->get();
$array = [];
foreach ($objects as $itens) {
$obj = new stdClass;
$obj->name = $itens->name;
$array[] = $obj;
}
return \json_encode($array);
}
There are a few ways to go about it. Personally I prefer to create accessors and mutators in the model - https://laravel.com/docs/8.x/eloquent-mutators . Not only does it keep the CrudController clean (so updateable) but it also allows you to save/load stuff from other forms (ex: user-facing forms).
//model Product
public function getFeaturesAttribute($value)
{
$objects = Features::where('product_id', $this->id)->get();
$array = [];
foreach ($objects as $itens) {
$obj = new stdClass;
$obj->name = $itens->name;
$array[] = $obj;
}
return json_encode($array);
}

ErrorException (E_ERROR) :Trying to get property of non-object. when i trying implode array to string in index.blade.php

index.blade.php
#php
$p = [];
foreach($p->genres as $genre){
$genres[] = $genre->genres;
}
$genre = implode(",", $genres);
#endphp
<span>{{$genre}}</span>
Controller
public function index()
{
$series = Series::all();
return view('admin/series/index',['series' => $series]);
}
model Genres.php
class Genres extends Model
{
protected $primarykey= 'id';
protected $fillable = ['genres'];
public function series()
{
return $this->belongsToMany(Series::class);
}
}
Model Series.php
class Series extends Model
{
protected $table = 'series';
protected $primarykey='id';
protected $fillable = ['title','country','japanese','year','synonyms','trailer','image','network','rating','duration','genres','aired','directors','screenwriters','score','type','episode','synopsis'];
public function genres(){
return $this->belongsToMany(Genres::class);
}
}
I want to display data from different databases using multiple table relationships, but the data is still a compilation array to bring it up.
and compilation I asked a friend. I was told to use a code like the one in index.blade.php. and return like this
ErrorException (E_ERROR) Try to get properties that are not objects
You are accessing array with -> whereas only the object can be accessed with arrow. To access array keys you have to use $array['key'] approach.
Arrays and objects are two different data types, so to access their inner data, you've to use their respective approaches.
You are looping wrong variables/collections. You need to make some changes to make it work. Below code should solve your problem.
#php
$p = [];
foreach($series as $ser){
foreach($ser->genres as $genre){
$p[] = $genre->genres;
}
}
$gen = implode(",", $p);
#endphp
<span>{{$gen}}</span>
You are setting series from the controller so inside the view you must loop over the series first.
#foreach($series as $s){
// then your code should be take place.
#php
$genres = [];
foreach($s->genres as $genre){
$genres[] = $genre->genres;
}
$genre = implode(",", $genres);
#endphp
<span>{{$genre}}</span>
#endforeach

codeigniter pagination - how to keep on same page after deleting row

I am using codeigniter pagination library and doing update and delete through form. The problem is when I delete or update any record after delete or update I redirected back to 1st page even if I am at 4th page. How I can redirected to the same page at which I have done update or delete.
Here is my controller code for update and delete functions:
public function update_att(){
if(isset($_POST['update'])){
$u_id = $_POST['at_id'];
if($_POST['status']=="Present"){
$data = array(
'status'=>"Absent"
);
$this->db->where('at_id', $u_id);
$this->db->update('attendence', $data);
}elseif ($_POST['status']=="Absent") {
$data = array(
'status'=>"Present"
);
$this->db->where('at_id', $u_id);
$this->db->update('attendence', $data);
}
redirect("usr/att_list", "refresh");
}
}
public function delete_att(){
if (isset($_POST['delete'])) {
$at_id = $_POST['at_id'];
$this->db->where('at_id', $at_id);
$this->db->delete('attendence');
}
redirect("usr/att_list" );
}
Currently I am redirecting to the first page , any suggestion how I redirect to that page where I have done delete or update.
Add <input type='hidden' name='current_page' value='<?php echo $current_page?>' /> to your form.
Keep in mind you need to populate $current_page with something like
$current_page = ''; // empty means first page
if( isset($_REQUEST['current_page'] ){
$current_page = $_REQUEST['current_page'];
}
Then based on your sample you just modifiy the redirect path as follows (for both your functions):
public function delete_att(){
if (isset($_POST['delete'])) {
$at_id = $_POST['at_id'];
$this->db->where('at_id', $at_id);
$this->db->delete('attendence');
}
redirect("usr/att_list".(strlen($current) ? "?".$current : "" );
}
Note:
Your path to your page should look something like this domain.com/controller/your_page_with_pagination/?current_page=A_NUMBER then in your controller/your_page_with_pagination you adjust the data you wish to output based on $_GET['current_page']
Edit based on your link:
public function att_list($id = ""){
/**
* Keep your data in array
*/
$this->data = array();
/**
* Add the page number ($id) to the array
* Check for url /:id first , or else check for a $_GET/$_POST current_page (this will be triggered when you submit your form)
*/
$this->data['current_page'] = is_numeric($id) ? $id : (isset($_REQUEST['current_page'] ? $_REQUEST['current_page']: 0);
/**
* This is your 'keep on the same page' line
* $this->data['current_page'] will act as an offset in your query
* You can read about 'offset' here https://www.w3schools.com/php/php_mysql_select_limit.asp
* $this->data['my_desired_records'] must be generated into your <form> ... </form> , and the same form must contain the input with name='current_page'
*/
$this->data['my_desired_records'] = $this->your_model->get_data($this->data['current_page']);
/**
* Pass the whole array into your view
*/
$this->load->view('path/to/view', $this->data);
}
Then in your view add in your form <input type='hidden' name='current_page' value='<?php echo $this->data['current_page']?>' />

ManyToOne form creating linked object

Imagine these 2 entities:
Article
title
description
category <- ManyToOne
Category
name
Say we need to manage an article with a form and the category attached.
$builder
->add('name')
->add('description')
->add('category')
This one will allow me to select from existing categories.
Now I'd like to be able to create categories if needed.
Title [ ]
Description
[ ]
[ ]
Category [ ]
The category field would be a free text box.
If the text corresponds to no category, a new one would be created and attached.
I tried with some DataTransformer with no luck
I need a reusable solution to manage that because I'll need to embed it especially in another form as a collection.
How can I do it reusable ?
Suggest that you do an "if" on your category.
When you check your form submission is valid and submitted, get the 'category' data, and if not exists persist it as a new category then query for the article after.
The code might be something like this:
if ($form-isSubmitted() && $form->isValid()){
$catName = $form->get('category')->getData(); // Get category.
// Query if it exists.
$qb = $em->createQueryBuilder();
$qb->select('c')
->from('AppBundle:Category', 'c')
->where('c.name = :catName')
->setParameter('catName', $catName);
$cat_results = $qb->getQuery()->setMaxResults(1)->getOneOrNullResult();
if ($cat_results == null){
$newCat = new Category();
$newCat()->setname($catName);
$em->persist($newCat);
$em->flush();
}
else{
...
\\ Render your form etc...
}
Hopefully you get the idea. The above code is simpler, because then you create the category in the same form.
Here is what I ended up with using a DataTransformer
My ArticleType:
class ArticleType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('description')
->add('category', CategoryType::class)
//...
A new CategoryType class:
class CategoryType extends AbstractType
{
protected $myService;
public function __construct(MyService $myService)
{
$this->myService = $myService;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new CategoryTransformer($this->myService);
$builder->addModelTransformer($transformer);
}
}
Registered as service
app.form.category_type:
class: AppBundle\Form\CategoryType
arguments: ['#app.my.service']
tags:
- { name: form.type }
And finally the transformer:
class CategoryTransformer implements DataTransformerInterface
{
// ...MyService initialization...
public function transform($category)
{
if (null === $category) {
return '';
}
return $category->getName();
}
public function reverseTransform($categoryName)
{
if (!$categoryName) {
return null;
}
$category = $this->myService->getOrCreateCategoryFromName($categoryName);
if (null === $category) {
throw new TransformationFailedException();
}
return $category;
}
}
MyService is responsible to get or create a category with the given name using the entity manager.
Quite some lines of code but once this is done, wherever I'll use my form, it can be dealt in the easy standard way:
$articleForm = $this->createForm(ArticleType::class, $article);
$articleForm->handleRequest($request);
if ($articleForm->isValid()) {
$em->persist($article);
$em->flush();
//...

How to return to edit form?

I have this code in my controller (admin):
function save(){
$model = $this->getModel('mymodel');
if ($model->store($post)) {
$msg = JText::_( 'Yes!' );
} else {
$msg = JText::_( 'Error :(' );
}
$link = 'index.php?option=com_mycomponent&view=myview';
$this->setRedirect($link, $msg);
}
In model I have:
function store(){
$row =& $this->getTable();
$data = JRequest::get('post');
if(strlen($data['fl'])!=0){
return false;
}
[...]
And this is working - generate error message, but it return to items list view. I want to stay in edit view with entered data. How to do it?
In your controller you can:
if ($model->store($post)) {
$msg = JText::_( 'Yes!' );
} else {
// stores the data in your session
$app->setUserState('com_mycomponent.edit.mymodel.data', $validData);
// Redirect to the edit view
$msg = JText::_( 'Error :(' );
$this->setError('Save failed', $model->getError()));
$this->setMessage($this->getError(), 'error');
$this->setRedirect(JRoute::_('index.php?option=com_mycomponent&view=myview&id=XX'), false));
}
then, you will need to load the data from session with something like:
JFactory::getApplication()->getUserState('com_mycomponent.edit.mymodel.data', array());
normally this is loaded in the method "loadFormData" in your model. Where to load that data will depend on how are you implementing your component. If you are using the Joomla's form api then you can add the following method to your model.
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_mycomponent.edit.mymodel.data', array());
if (empty($data)) {
$data = $this->getItem();
}
return $data;
}
EDIT:
BUT please note, that Joomla's API already can do all this for you if you controller inherits from "JControllerForm", you don't need to rewrite the save method. The best way to create your component is copying what is in Joomla's core components, com_content for example
It is not recommended to rewrite save or any method.
If you really want to override something and want to update something before or after save, you should use JTable file.
For Example:
/**
* Example table
*/
class HelloworldTableExample extends JTable
{
/**
* Method to store a node in the database table.
*
* #param boolean $updateNulls True to update fields even if they are null.
*
* #return boolean True on success.
*/
public function store($updateNulls = false)
{
// This change is before save
$this->name = str_replace(' ', '_', $this->name);
if (!parent::store($updateNulls))
{
return false;
}
// This function will be called after saving table
AnotherClass::functionIsCallingAfterSaving();
}
}
You can extends any method using JTable class and that's the recommended way to doing it.

Resources