Hi all today i have this problem with my api.
I don't update the record on DB.
In postaman the response is true but don.t save in db.
In Postaman i passed with PUT method and set in Body name a text
ProductController:
public function update(Request $request, $id)
{
$product = auth()->user()->products()->find($id);
if (!$product) {
return response()->json([
'success' => false,
'message' => 'Product with id ' . $id . ' not found'
], 400);
}
$updated = $product->fill($request->all())->save();
if ($updated)
return response()->json([
'success' => true
]);
else
return response()->json([
'success' => false,
'message' => 'Product could not be updated'
], 500);
}
You should take a look at your Product Model to see if name is set as a fillable field: $fillable = ['name'];. Also, the key is probably just name instead of "name".
Related
My Laravel insert function in my controller looks for a "created_by" column that I do not have.
Model
class HmsBbrCategory extends Model
{
public $timestamps = false;
protected $table = 'hms_bbr_category';
protected $fillable = ["id", "category_id", "category_name",
"category_description"];
}
Controller
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'category_name' => 'required|max:191',
'category_description' => 'required|max:191',
]);
if ($validator->fails()) {
return response()->json([
'status' => 400,
'errors' => $validator->messages(),
]);
} else {
$categoryId = HmsBbrCategory::orderByDesc('id')->first();
$autoIncId = $categoryId->id + 1;
$category = new HmsBbrCategory;
$category->category_name = $request->input('category_name');
$category->category_description = $request->input('category_description');
// Option tags
$category->config_view_type = $request->input('config_view_type');
$category->config_edit_type = $request->input('config_edit_type');
$category->bbrmode_view_type = $request->input('bbrmode_view_type');
$category->id = $autoIncId;
$category->category_id = $autoIncId;
$category->save();
return response()->json([
'status' => 200,
'message' => 'Category Added!'
]);
}
}
I have a feeling that I may have the issue here in my request: $validator = Validator::make($request->all(), [ I was thinking that by all() I was taking all columns that I added. But this line may be the one looking for created_by. So how do I make an exemption not to have my code look for created_by?
Appears like your created_by column is NOT NULL, change to NULL
I've created a list which is fully populated with records from database. I'd like to add a button 'delete' in order to delete records on demand. The button appears, but whenever it makes a request, it does not have an ID of the record I want to delete. The URL looks like this:
controller=AdminModules&configure=estimateddelivery&=&deleteestimateddelivery&token=6d1625ddf520e0bf8d2c43bea84f21d3
There is a &=& which if I understand correctly should be populated with something like &id=10&. I am not sure what the problem is or where to look at. I've check code examples of similar functionalities and it looks like I am doing everything the same way.
public function renderList()
{
$this->$fields_list = [
'id_estimateddelivery' => [
'title' => $this->l('ID'),
'type' =>'text',
],
'from' => [
'title' => $this->l('Delivery period from')
],
'to' => [
'title' => $this->l('Delivery period to')
],
'countries' => [
'title' => $this->l('Countries applicable'),
'type' => 'text'
]
];
$helper = new HelperList();
$helper->module = $this;
$helper->shopLinkType = '';
$helper->simple_header = true;
$helper->idientifier = 'id_estimateddelivery';
$helper->actions = [
'delete'
];
$helper->show_toolbar = false;
$helper->title = $this->l('List of created estimated deliveries');
$helper->table = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
return $helper->generateList($this->getEstimatedDeliveries(), $this->$fields_list);
}
public function deleteEstimatedDelivery()
{
return Db::getInstance()->execute('DELETE FROM '. _DB_PREFIX_ .'estimateddelivery WHERE `id_estimateddelivery` = '. (int)Tools::getValue('id_estimateddelivery'));
}
else if(Tools::isSubmit('delete' . $this->name))
{
if(!$this->deleteEstimatedDelivery())
$output . $this->displayError($this->l('An error occured during link deletion'));
else
$output . $this->displayConfirmation($this->l('The estimated delivery has been deleted'));
}
Even though there was no error message, the code line: $helper->idientifier = 'id_estimateddelivery'; had a typo. Should have been identifier.
I have create API of update my project details, I test it in POSTMAN app it shows the success message but there no effect in the database.
Here are my code:
ProjectsController.php
public function UpdateProject($id)
{
$data = Input::all();
$q = Project::where('id',$id)->update($data);
return response()->json([
'code' => SUCCESS,
'message' => 'Project data update successfully'
]);
}
api.php
Route::post('UpdateProject/{id}','ProjectsController#UpdateProject');
Postman - see image.
output in postman:
{
"code": "200",
"message": "Project data update successfylly"
}
Can anyone help me out?
Thank you
I think you need to check all input details closely , it also comes with token when you submit the form so you need to save all details except token
Change this
$data = Input::all();
to this
$data = Input::except('_token');
I hope this resolves the issue.
in your model add fillable :
protected $fillable = ['name', 'project_group_id','number','ROM','address','city','state','zip','start_date','end_date','duration','description','timeline_type','project_type_id','project_category_id','office_id'];
You have forgotten to run the ->save() method after updating the data:
public function UpdateProject($id)
{
$data = Input::all();
$q = Project::find($id)
$q = $q->fill($data);
$q->save();
return response()->json([
'code' => SUCCESS,
'message' => 'Project data update successfully'
]);
}
You can use this method it will reduce your code
Route (api)
Route::post('UpdateProject/{project}','ProjectsController#UpdateProject');
ProjectsController.php
public function UpdateProject(Request $request, Project $project)
{
$data = $request->all();
$project->update($data);
return response()->json([
'code' => SUCCESS,
'message' => 'Project data update successfully'
]);
}
Hello I try to filter a List in the Backoffice. It shows the filter, it also saves it after clicking on search, but nothing is happening. Same with pagination.
$schueler = $this->getAllSchuelerbyDiplom($id_diplom);
$diplom_name = $this->getDiplomNamebyID($id_diplom);
$fields_list = array(
'id_schueler' => array(
'title' => 'ID',
'align' => 'center',
'class' => 'fixed-width-xs',
'search' => true),
'customer_name' => array(
'title' => $this->l('ID Customer')),
'id_gruppe' => array(
'title' => $this->l('ID Gruppe')),
'name' => array(
'title' => $this->l('Name'),
'filter_key' => 'name'.$diplom_name),
'vorname' => array(
'title' => $this->l('Vorname')),
'punkte' => array(
'title' => $this->l('Punkte')),
'bestanden' => array(
'title' => $this->l('Bestanden'),
'active' => 'toggle',
'class' => 'fixed-width-xs',
'type' => 'bool'),
'date_added' => array(
'title' => $this->l('Datum'),
'class' => 'fixed-width-xs',
'type' => 'date'),
);
$helper = new HelperList();
$helper->table = 'no-idea-what-this-is-for';
$helper->title = $diplom_name;
$helper->shopLinkType = '';
$helper->actions = array('view', 'edit', 'delete');
$helper->listTotal = count($schueler);
$helper->identifier = 'id_schueler';
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) . '&configure=' . $this->name .'&diplom_name=' . $diplom_name;
return $helper->generateList($schueler, $fields_list);
What is wrong with my code? What is $helper->table for? I tried different things there, but nothing helps...
EDIT
public function getAllSchuelerbyDiplom($id_diplom) {
$query = new DbQuery();
$query->select('s.*, CONCAT(c.firstname,\' \',c.lastname) AS customer_name');
$query->from($this->table_name.'_schueler', 's');
$query->leftJoin('customer', 'c', 's.id_customer = c.id_customer');
$query->where('s.id_diplom = ' . (int)$id_diplom);
return Db::getInstance()->ExecuteS($query);
}
The problem is that HelperList object itself will only display filters being used but won't filter the actual data list for you.
In your $this->getAllSchuelerbyDiplom($id_diplom); method I assume you execute SQL query to retrieve all rows, however you need modify it to account for any filters, paginations (page number and rows per page) or ordering. You have to check GET/POST values or values set in cookie to detect them.
$helper->table sets the table name and list actions are prepended with that name so they are distinct from any other lists you might have on the page.
Edit:
Example of setting pagination and page
public function getPage()
{
// $tableName must be equal to what you set in $helper->table
// Check if page number was selected and return it
if (Tools::getIsset('submitFilter'.$tableName)) {
return (int)Tools::getValue('submitFilter'.$tableName);
}
else {
// Check if last selected page is stored in cookie and return it
if (isset($this->context->cookie->{'submitFilter'.$tableName})) {
return (int)$this->context->cookie->{'submitFilter'.$tableName};
}
else {
// Page was not set so we return 1
return 1;
}
}
}
public function getRowsPerPage()
{
// $tableName must be equal to what you set in $helper->table
// Check if number of rows was selected and return it
if (Tools::getIsset($tableName. '_pagination')) {
return (int)Tools::getValue($tableName. '_pagination');
}
else {
// Check if number of rows is stored in cookie and return it
if (isset($this->context->cookie->{$tableName. '_pagination'})) {
return (int)$this->context->cookie->{$tableName. '_pagination'};
}
else {
// Return 20 rows per page as default
return 20;
}
}
}
public function getAllSchuelerbyDiplom($id_diplom) {
$query = new DbQuery();
$query->select('s.*, CONCAT(c.firstname,\' \',c.lastname) AS customer_name');
$query->from($this->table_name.'_schueler', 's');
$query->leftJoin('customer', 'c', 's.id_customer = c.id_customer');
$query->where('s.id_diplom = ' . (int)$id_diplom);
// Limit the result based on page number and rows per page
$query->limit($this->getRowsPerPage(), ($this->getPage() - 1) * $this->getRowsPerPage());
return Db::getInstance()->ExecuteS($query);
}
You can place a p(Tools::getAllValues()); d($this->context->cookie->getAll(); after you generate a list then set a filter in your list and it will show you all the variables you need to create filters and ordering.
I have been looking into select2 and yii and have managed to load data via json request/response.
The issue I'm faced with is when I try to select an entry of the returned data, I can not.
Where am I going wrong ? The data returnd by the action is json formatted as CustomerCode and Name
Widget code in form
$this->widget('bootstrap.widgets.TbSelect2', array(
'asDropDownList' => false,
'name' => 'CustomerCode',
'options' => array(
'placeholder' => 'Type a Customer Code',
'minimumInputLength' => '2',
'width' => '40%',
'ajax' => array(
//'url'=> 'http://api.rottentomatoes.com/api/public/v1.0/movies.json',
'url'=> Yii::app()->getBaseUrl(true).'/customer/SearchCustomer',
'dataType' => 'jsonp',
'data' => 'js: function (term,page) {
return {
term: term, // Add all the query string elements here seperated by ,
page_limit: 10,
};
}',
'results' => 'js: function (data,page) {return {results: data};}',
),
'formatResult' => 'js:function(data){
var markup = data.CustomerCode + " - ";
markup += data.Name;
return markup;
}',
'formatSelection' => 'js: function(data) {
return data.CustomerCode;
}',
)));
code snipped from controller action SearchCustomer
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
$this->renderJSON(Customer::model()->searchByCustomer($term));
renderJSON function from base controller class
protected function renderJSON($data)
{
header('Content-type: application/json');
echo $_GET['callback'] . "(";
echo CJSON::encode($data);
echo ")";
foreach (Yii::app()->log->routes as $route) {
if($route instanceof CWebLogRoute) {
$route->enabled = false; // disable any weblogroutes
}
}
Yii::app()->end();
}
Appreciate any help on this
i try.
change
'dataType' => 'jsonp' to 'dataType' => 'json'
and check json format
https://github.com/ivaynberg/select2/issues/920