I used webpatser/laravel-countries
in my controlleer
$countries= DB::table('countries')->lists('name');
to select country list in my form i got all the country
{!! Form::select('country',$countries,null) !!}
in my schema now i have like this
$table->string('country');
i can select the country but when i submit country column blank. any idea ?
Edit 2:
Your insert need to look like this:
DB::table('users')->insert(
['country' => 'Canada']
);
https://laravel.com/docs/5.2/queries#inserts
Edit: if you want to submit the string of the country name then you need the string to be the key in your array. Like this:
$countries= DB::table('countries')->lists('name', 'name');
That's not typical use of a database relationship though. I'd expect the user table to have a country_id integer field that matches the id field in countries. That's what my original answer (below) covers.
Original:
You'll want the id in your $countries array. I bet right now your ids are off by one, starting at 0 instead of 1 like in your DB.
$countries= DB::table('countries')->lists('name', 'id');
There are different ways to save relations in laravel, but I actually prefer to skip that when possible and save directly to the country_id field.
{!! Form::select('country_id',$countries,null) !!}
But that depends on how your relationship is setup. And how you're saving. Show all the code!
Related
I have a problem of checking whether any values in an array match any values in json column which contains an array with a name.
suppose i have an array [25,36,45,52] and json column is {"values": [25,24,15]}.
I want to check whether any values in array match any of values in json column in xampp mysql. please provide a better solution of doing this. this image show table structure of my database
i have 4 tables.
user
profile
profile
jobs
user table (id,userid)
jobs table (id,user_id,skill_id)
skill table (id,job_id,)
profile table (id,user_id)
now i want to search all jobs that match some or at least one skills.
i have tried with this but this is giving all jobs with out skills filtered.
$jobs = Job::with(['user','profile'])->with(['skills' => function($query){
$query->whereJsonContains('skills->skills',[35]);
}])->where('jobs.is_completed',0);
please help me.
you can use where Clause easily for example you would like to get rows that match skills 35,54:
$users = DB::table('table')
-> whereJsonContains('skills->skills', [35,54])
->get();
for more details about how to querying json column check official docs :
https://laravel.com/docs/5.8/queries#json-where-clauses
I have a model called lists, which has a column called item_ids. item_ids is a JSON column (MySQL) and the column contains array of UUIDs, each referring to one item.
Now when someone creates a new list, I need to search whether there is an existing list with same set of UUIDs, and I want to do this search using query itself for faster response. Also use ActiveRecord querying as much as possible.
How do i achieve this?
item_ids = ["11E85378-CFE8-39F8-89DC-7086913CFD4B", "11E85354-304C-0664-9E81-0A281BE2CA42"]
v = List.new(item_ids: item_ids)
v.save!
Now, how do I check whether a list exists which has item ids exactly matches with that mentioned in query ? Following wont work.
list_count = List.where(item_ids: item_ids).count
Edit 1
List.where("JSON_CONTAINS(item_ids, ?) ", item_ids.to_json).count
This statement works, but it counts even if only one of the item matches. Looking for exact number of items.
Edit 2
List.where("JSON_CONTAINS( item_ids, ?) and JSON_LENGTH(item_ids) = ?", item_ids.to_json, item_ids.size).count
Looks like this is working
You can implement a has many relation between lists and items and then access like this.
List.includes(:item).where('items.id in (?)',item_ids)
To implement has_many relation:
http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
Right so at the minute my query looks like this:
$subcategory = Subcategory::find()->asArray()->all();
this basically grabs all the data from my subcategory table and stores it into $subcategory.
However i want to find specific data. For example in my subcategory table i have a column called subcategory_id.
Now i want to pull back all the subcategories where subcategory_id = $firstcategory(This will be a number)
Im guessing that it is something like this, however it is not working. Any ideas on how to do this query?
$subcategory = Subcategory::find()->asArray()->all()->where('subcategory_id'
== $firstcategory);
There is no need to guess, just read the docs. Here is one of the ways to do it:
$subcategory = Subcategory::find()
->where(['subcategory_id' => $firstcategory]);
->asArray()
->all();
Note that order of using all() is crucial, once it's called you get the array of results and you can not modify query anymore.
my question is if is possible and how can i fill the description field on the payment form, whith a reference number from my slq database ?
Thank you.
You can try something like this query to define the description:
<?php $product = SELECT desc FROM table WHERE id = 'x'; ?>
And then use the $product variable to populate the value for "item_name" or whatever variable for the item description that matches the product you're using. I don't know what payment method you're using so I can't be more specific.
I apologize for the confusing title, I was a little stumped as to how to word my question.
I am new to CakePHP, but am following along through the cookbook/tutorials nicely, however I have come up against something which I cannot find an answer to.
My structure is as follows:
'Invoices' hasMany 'InvoiceHistory'
'InvoiceHistory' belongsTo 'InvoiceHistoryDeliveryStatus'
Whereby, an invoice can have multiple invoice histories, and each history contains a delivery status id, which links to a name.
On the Invoice view (index.ctp) I am displaying a list of all invoices but wish to display the Most Recent Delivery Status Name (InvoiceHistory contains a date field so it can be sorted) - thereby displaying the 'current Delivery Status'.
When I do:
$this->set('invoices', $this->Invoice->find('all'));
It does not go deep enough in what it returns to provide me with Delivery Status Names, nor have I deduced a way of only returning the most recent Invoice History within my result. I know how to do this manually with a MYSQL query but I figured that is probably just plain wrong.
What is the correct way of going about this while following CakePHP conventions?
Use Containable
$this->Invoice->Behaviors->attach('Containable');
$this->set('invoices', $this->Invoice->find('all', array(
'contain' => array(
'InvoiceHistory' => array(
'InvoiceHistoryDeliveryStatus'
)
)
));
From what I can tell, I think you should check out the Containable behavior.