Property does not exist on this collection instance while trying to iterate through foreach - eloquent

I'm trying to display an edit form with current value from the database.
Here's the Controlller.
public function edit(PenerimaanHadiahSponsor $id)
{
$collection = PenerimaanHadiahSponsor::find($id);
$dataanak = Anak::find($id);
$datatp = TipeHadiahSponsor::find($id);
// dd($dataanak);
return view('sponsor.penerimaan.updatetrx', compact('collection', 'dataanak', 'datatp'));
}
For now, I try to iterate the $datanak collection, but it returns that property [id] does not exist on the collection.
Here's the blade view file
<div class="mb-3">
<label class="form-label" for="basic-form-gender">Pilih Anak</label>
<select class="form-select" id="basic-form-gender" aria-label="Default select example" name="id_anak">
<option selected="selected">Pilih Anak yg Dapat Hadiah</option>
#foreach ($dataanak as $dtanak)
<option value="{{ $dtanak -> id }}">{{ $dtanak -> fullname }}</option>
#endforeach
</select>
</div>
I expect that it could return this data because it has relation with my curent model table which named penerimaan_data_sponsors
https://cdn.discordapp.com/attachments/856336794068189208/1051057589376532580/image.png
When I try to dd($dataanak) with this method:
$dataanak = Anak::where('id', 1)->get();
It returns the collection using id = 1, but when I used the current data id which is 12, it's returns blank array. Because the current data I want to edit had id 12.
https://cdn.discordapp.com/attachments/856336794068189208/1051058294464188456/image.png

the fix was this. I removed the model 'PenerimaanHadiahSponsor' from edit function.
public function edit($id)
{
$collection = PenerimaanHadiahSponsor::find($id);
$dataanak = Anak::find($id);
$datatp = TipeHadiahSponsor::find($id);
dd($dataanak->fullname);
// return view('sponsor.penerimaan.updatetrx', compact('collection', 'dataanak', 'datatp'));
}

Related

Angular2 Dynamic form displaying incorrect ngModel data

Im generating a table and form to edit each field but when I try to edit it, the data displayed in the input fields is incorrect.
HTML
<form (ngSubmit)="onEditSubmit()">
<span>What the form gets</span>
<div *ngFor="let k of keys; let i = index" class="form-group row">
<span>{{k|uppercase}}</span>
<div class="col-md-9">
<input [(ngModel)]="updateModels[keys[i]]" type="text" name="text-input" class="form-control" placeholder="{{k}}" />
</div>
</div>
<button>Submit</button>
</form>
JS
public data: any = [{id: 1, code1: "VALUE1", code2: "VALUE2", code3: "VALUE3"},{id: 2, code1: "TEST1", code2: "TEST2", code3: "TEST3"}];
public keys: any = ["code1","code2","code3"];
public activeRoleId = '';
public updateModels: any = {};
public toEditData: any = "";
public editedData: any = "";
constructor() {
}
onEditSubmit() {
this.editedData = this.updateModels;
}
/*CLick on element actions*/
editElement(item): void {
for (let i = 0; i < this.keys.length; i++) {
this.updateModels[this.keys[i]] = item[this.keys[i]];
}
console.log(this.updateModels);
this.toEditData = this.updateModels;
this.activeRoleId = item.id;
}
Plunker
This error is caused because of you are using a form. If you'd remove the form tags, this would work as you want. BUT, since you are using a form, each name-attribute has to be unique, so that these fields are evaluated as separate form fields.
So the solution for your problem is quite simple, just assign an unique name to your fields, you can make use of the index, so change:
name="text-input"
for example to the following:
name="text-input{{i}}"
Then it works like a charm! :) Here's the forked
Plunker

FE Searchfield in simpleblog extension doesn't work

I am trying to going through the book "TYPO3 Extbase - Modern Extension Development" from P. Lobacher. There I am on the point with the searchfield. Here my Editor (PHPStorm) tells me that $search isn't defined.
I have a listAction with:
/**
* #internal param string $search
*/
public function listAction() {
if ($this->request->hasArgument('search')){
$search = $this->request->getArgument('search');
}
$limit = ($this->settings['blog']['max']) ?: NULL;
$this->view->assign('blogs', $this->blogRepository->findSearchForm($search,$limit));
$this->view->assign('search', $search);
}
In my repository i try to setup a query like that:
/**
* #param string $search
* #param int $limit
* #return array|\TYPO3\CMS\Extbase\Persistence\QueryResultInterface
*/
public function findSearchForm($search,$limit) {
$query = $this->createQuery();
$query->matching(
$query->like('title','%'.$search.'%')
);
$query->setOrderings(array('title' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_ASCENDING));
$limit = (int)$limit; if ($limit > 0) {
$query->setLimit($limit);
}
return $query->execute();
}
In my list view i have the searchform like that:
<f:form action="list" additionalAttributes="{role:'form'}"> <div class="form-inline">
<div class="form-group">
<f:form.textfield name="search" value="{search}" class="form-control" /> <f:form.submit value="Search!" class="btn-xs btn-primary" />
</div> </div>
</f:form>
If i change the assign to the view in my listAction to:
$this->view->assign('blogs', $this->blogRepository->findSearchForm('Testblog',$limit));
Then he shows me the blogs with the titlefragment "Testblog" in it. But dynamically it won't work.
I am using TYPO3 v8.5.1
Thx for your help.
at first you should change the $limit line because its hard to read. Change it to:
$limit = $this->settings['blog']['max'] ? $this->settings['blog']['max'] : 0;
I changed NULL to 0 because in the repository annotation the parameter should be an integer.
Then you should also change the two assign() functions to an assignMultiple()-Array.
Have you ever debugged $search in your controller using
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($search);
Because your is not bound to an object, you are may not able to get $search with getArgument(). Can you please give us the rendered HTML output of that form field?
EDIT: Because of your search you may not cache the list action. Change list action in ext_localconf.php to NOT CACHED.

Codeigniter : get array value from view and send to contoller and model

view:insert_view.php
for($i=0;$i<10;$i++){
<input type="text" name="val[]" />
}
Table:
create table val_ex(val character varying);
controller:insert_ctl.php
$data['val'] = $this->input->post('val')
but it doesn't work it
.And i don't know how to write model to insert data to table
I don't know postgresql I just say How to insert records in model
Controller :-
$data['val'] = $this->input->post('val');
foreach($this->input->post('val') as $value) {
$this->model_name->insert_ctl($value);
}
Model :-
public function insert_ctl($value) {
$Array = array ('val' => $value);
$this->db->insert("val_ex", $Array);
}

How to create nested <ul> <li> tags with HtmlTags (FubuMVC)

I have no previous experience with FubuMVC HtmlTags library, and I simply got stuck when trying to accomplish a simple nested structure like this:
<ul>
<li>text</li>
<li>text
<ul>
<li>subtext</li>
<li>subtext</li>
</ul>
</li>
<li>text</li>
</ul>
Here's how I have it when building the string:
public static HtmlString ChildNodesRecursive(DocumentNode documentNode)
{
var tag="";
if (documentNode.Children.Count > 0)
{
tag = "<ul>";
foreach (var c in documentNode.Children)
{
tag += "<li>" + c.Name;
tag += ChildNodesRecursive(c);
tag += "</li>";
}
tag += "</ul>";
}
return new HtmlString(tag);
}
Works fine, but I like to use HtmlTags library (outside of FubuMvc, with the HtmlTags separate Nuget).
Edit : I got inspiration from both answers and came up with what I needed. So here's the code I ended up using.
public static HtmlTags.HtmlTag ChildNodesRecursiveHtmlTag(DocumentNode documentNode)
{
var ul = new HtmlTags.HtmlTag("ul");
foreach (var c in documentNode.Children)
{
var li = new HtmlTags.HtmlTag("li");
li.Add("a").Attr("href",c.ContextFullPath).Text(c.Name);
if (c.Children.Count > 0)
{
li.Children.Add(ChildNodesRecursiveHtmlTag(c));
}
ul.Children.Add(li);
}
return ul;
}
I can give you an example which may make things clearer to you:
var ul = new HtmlTag("span").AddClass("form_input");
ul.Modify(t =>
{
foreach (var value in choice)
{
t.Add("input")
.Attr("type", "radio")
.Attr("name", request.Accessor.Name)
.Attr("value", value)
.Add("span")
.AddClass("fixed-width")
.Text(value);
}
});
Gives you something like
<span class="form-input">
<input type="radio" name="bla" value="foo" />
<span class="fixed-width">foo</span>
...etc...
</span>
You can carry on nesting tags with modify and filling in the lambda. I think you will find that what you want to do is possible with the bits of syntax shown.
This code:
var root = new HtmlTags.HtmlTag("ul");
root.Add("li").Text("item1");
var child = root.Add("ul");
child.Add("li").Text("item2");
return root.ToPrettyString();
produces the following output:
<ul>
<li>item1</li><ul>
<li>item2</li>
</ul>
</ul>

ASP.NET MVC 2 RC: How to use the EditorFor to render correct name attributes for a List<>?

In the MVC RC 2 docs, we find:
Expression-based helpers that render input elements generate correct name attributes when the expression contains an array or collection index. For example, the value of the name attribute rendered by Html.EditorFor(m => m.Orders[i]) for the first order in a list would be Orders[0].
Anyone care to link an example of the C# view code (using a List where the result can bind back to the Model upon post)?
Just as a reference, I use the following code to verify the model binds correctly round trip. It simply shows view that allows change, then displays a view with the edited data upon form submission.
[HandleError]
public class HomeController : Controller
{
public ActionResult Index()
{
var myStudents = new List<Student>();
myStudents.Add(new Student { Name = "Harry" });
myStudents.Add(new Student { Name = "Tom" });
myStudents.Add(new Student { Name = "Richard" });
var myClass = new Classroom {Students = myStudents};
return View(myClass); // EditorFor()
}
[HttpPost]
public ActionResult Index( Classroom myClass)
{
return View("IndexPost", myClass); // DisplayFor()
}
This code:
<% for (int count = 0; count < Model.Students.Count; count++ )
{ %><%=
Html.EditorFor(m => m.Students[count]) %><%
}
%>
Rendered this output:
<input class="text-box single-line" id="Students_0__Name" name="Students[0].Name" type="text" value="Harry" />
<input class="text-box single-line" id="Students_1__Name" name="Students[1].Name" type="text" value="Tom" />
<input class="text-box single-line" id="Students_2__Name" name="Students[2].Name" type="text" value="Richard" />
And when I posted the content, the display was this (because I have a Student.ascx):
<table>
<tr><td><span>Harry</span> </td></tr>
<tr><td><span>Tom</span> </td></tr>
<tr><td><span>Richard</span> </td></tr>
</table>
But that's it (I think). Next question is how to get rid of those name="" tags.