ItemsController has below code
public function index()
{
$items = Item::all();
return view('welcome' , ['items' => $items]);
}
The model name is Item
In welcome.blade i am calling it like this
#foreach($items as $item)
<li class="list-group-item">{{$item->title}}</li>
#endforeach
but it gives "Undefined variable: items (View: ..... welcome.blade.php)"
Also i want to know, how will i get and show a single row from database. and how i will get and show many rows. Being new to laravel; things are confusing me a lot.
Related
I want to find a better way to update local component state after executing mutation. I'm using svelte-apollo but my question is about basic principles. I have watchQuery which get list of items and returns ObservableQuery in component.
query GetItems($sort: String, $search: String!) {
items(
sort: $sort
where: { name_contains: $search }
) {
id
name
item_picture{
pictures{
url
previewUrl
}
}
description
created_at
}
}
In component I call it:
<script>
$: query = GetItems({
variables: {
sort: 'created_at:DESC',
search
}
});
</script>
...
{#each $query.data?.items || [] as item, key (item.id)}
<div>
<Item
deleteItem={dropItem}
item={item}
setActiveItem={setActiveItem}
/>
</div>
{/each}
...
And I have addItem mutation.
mutation addItem($name: String!, $description: String) {
createItem(
input: { data: { name: $name, description: $description } }
) {
item {
name
description
}
}
}
I just simply want to update local state and add new item to an observable query result after addItem mutation, without using refetchQueries (because I don't want to get all list by network when I just added one item).
I seen this item in cache but my view is not updated.
P.S. If you have similar problems and some ways to solve it, be glad to see some cases from you.
I believe in this case, you could use the cache.modify function to modify the cache directly if you’re looking to skip the network request from refetchQueries. Would that work for your use case? https://www.apollographql.com/docs/react/data/mutations/#making-all-other-cache-updates
If you don’t mind the network request, I like using cache.evict to evict the data in the cache that I know changed personally. I prefer that to refetchQueries in most cases because it refetches all queries that used that piece of data, not just the queries I specify.
I have a "custom_fields "table where I store fields that a user has created. I need to use the "field_slug" value from that table to form the properties in another form. By default the public properties do not exist so an error("Property [$field_name] not found on component") is thrown when an attempt to enter data inside the input field takes place.
I know that livewire utilizes public properties, but because the data coming back is dynamic I am not able to(or know how to) create those public properties for use within the form. So, the "field_name" within the array is what I would want to act as a public property(or this may be an incorrect approach, not sure) so I can store the values entered within the input by a user.
Any useful assistance with this problem would be greatly appreciated.
Data within CustomField
Component
HTML(blade)
My solution was to call my custom fields query in livewire's mount() method and assign the values to a custom field property. I mutated the values assigned in the $this->customFields property and assigned it to a $this->formSlugs property
public function mount()
{
$this->customFields = CustomField::all();
$data = [];
$this->formSlugs = collect($this->customFields)->map(function($value) use ($data) {
$data[$value->field_slug] = '';
return $data;
})->toArray();
}
public function render()
{
return view('livewire.inventory.items.create', ['customFields' => $this->customFields])
->extends('layouts.master')
->section('content');
}
In the HTML
#foreach($customFields as $key => $customField)
<input type="text" wire:model="formSlugs.{{$key}}.{{$customField->field_slug}}" class="form-control focus:placeholder-transparent" placeholder="{{ $customField->placeholder_text }}">
#endforeach
I have a table called history in which i have a column called PRODUCTS in which the products are saved.
I have a search screen with products using checkboxes, I can get the data if i use only checkbox.
How can i get data if i tick more than one checkboxes.
Displaying the checkboxes using below code
#foreach($products as $product)
#if (in_array($product->name, explode(" , ",$user->products)) !== false)
<input type="checkbox" name="products[]" value="{{$product->name}}"
#if(in_array($product->name, explode(" , ",$user->products))) #endif>
{{$product->name2}}
#endif
#endforeach
Controller
public function usersSearch(Request $request, Repair_History $query)
{
$query = $query->newQuery();
if ($request->has('products')) {
$query->where('proc_doc', $request->input('products'));
}
return $query->get();
}
Fixed it by using below code in my controller, posting as it might help someone.
$friends_checked = $request->input('products');
if (count($friends_checked) > 1 && is_array($friends_checked)) {
$query->whereIn('proc_doc', $friends_checked)
}
For a Tag model that I have in Ember-Data, I have 4 records in my store:
Tags:
id tag_name
1 Writing
2 Reading-Comprehension
3 Biology
4 Chemistry
In my code I have an array of tag_names, and I want to get a corresponding array of tag IDs. I'm having 2 problems:
My server is being queried even though I have these tags in my store. When I call store.find('tag', {tag_name: tag_name}), I didn't expect to need a call to the server. Here is all the code I'm using to attempt to create an array of IDs.
var self = this;
var tagsArray = ["Writing", "Reading-Comprehension", "Chemistry"];
var tagIdArr = []
tagsArray.forEach(function(tag_name) {
return self.store.find('tag', { tag_name: tag_name }).then(function(tag) {
tagIdArr.pushObject(tag.get('content').get('0').get('id'));
})
})
return tagIdArr;
When I console.log the output of the above code gives me an empty array object with length 0. Clicking on the caret next to the empty array shows three key-value pairs with the correct data. But the array is empty. I'm sure there is a simple explanation for this behavior, but I'm not sure why this is. I've used code similar to the above in other places successfully.
Find hits the server, but peek does not.
var tagsArray = ["Writing", "Reading-Comprehension", "Chemistry"];
return this.store.peekAll('tag').filter(function(tag){
return tagsArray.indexOf(tag) !== -1;
}).mapBy('id');
See: http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_reorganized-find-methods
I need to put a search box within a list of objects as a result of a typical indexSuccess action in Symfony. The goal is simple: filter the list according to a criteria.
I've been reading the Zend Lucene approach in Jobeet tutorial, but it seems like using a sledge-hammer to crack a nut (at least for my requirements).
I'm more interested in the auto-generated admin filter forms but I don't know how to implement it in a frontend.
I could simply pass the search box content to the action and build a custom query, but is there any better way to do this?
EDIT
I forgot to mention that I would like to have a single generic input field instead of an input field for each model attribute.
Thanks!
I'm using this solution, instead of integrating Zend Lucene I manage to use the autogenerated Symonfy's filters. This is the way i'm doing it:
//module/actions.class.php
public function executeIndex(sfWebRequest $request)
{
//set the form filter
$this->searchForm = new EmployeeFormFilter();
//bind it empty to fetch all data
$this->searchForm->bind(array());
//fetch all
$this->employees = $this->searchForm->getQuery()->execute();
...
}
I made a search action which does the search
public function executeSearch(sfWebRequest $request)
{
//create filter
$this->searchForm = new EmployeeFormFilter();
//bind parameter
$fields = $request->getParameter($this->searchForm->getName());
//bind
$this->searchForm->bind($fields);
//set paginator
$this->employees = $this->searchForm->getQuery()->execute();
...
//template
$this->setTemplate("index");
}
It's important that the search form goes to mymodule/search action.
Actually, i'm also using the sfDoctrinePager for paginate setting directly the query that the form generate to get results properly paginated.
If you want to add more fields to the search form check this :)
I finally made a custom form using the default MyModuleForm generated by Symfony
public function executeIndex {
...
// Add a form to filter results
$this->form = new MyModuleForm();
}
but displaying only a custom field:
<div id="search_box">
<input type="text" name="criteria" id="search_box_criteria" value="Search..." />
<?php echo link_to('Search', '#my_module_search?criteria=') ?>
</div>
Then I created a route named #my_module_search linked to the index action:
my_module_search:
url: my_module/search/:criteria
param: { module: my_module, action: index }
requirements: { criteria: .* } # Terms are optional, show all by default
With Javascript (jQuery in this case) I append the text entered to the criteria parameter in the href attribute of the link:
$('#search_box a').click(function(){
$(this).attr('href', $(this).attr('href') + $(this).prev().val());
});
And finally, back to the executeIndex action, I detect if text was entered and add custom filters to the DoctrineQuery object:
public function executeIndex {
...
// Deal with search criteria
if ( $text = $request->getParameter('criteria') ) {
$query = $this->pager->getQuery()
->where("MyTable.name LIKE ?", "%$text%")
->orWhere("MyTable.remarks LIKE ?", "%$text%")
...;
}
$this->pager->setQuery($query);
...
// Add a form to filter results
$this->form = new MyModuleForm();
}
Actually, the code is more complex, because I wrote some partials and some methods in parent classes to reuse code. But this is the best I can came up with.