Get database results from frontend user's form input details - database

I'm trying to use select statement if a laravel form has been filled. I have 2 tables "query" and "all". If a user fills a form it is saved in query but I also want it to select certain fields from table all and returns the answer.
When a user fills in a form its now saving in query.
$post = new query;
$post->name = $request->input('name');
$post->description = $request->input('description');
$post->email = auth()->user()->email;
$post->save();
session()->flash('notif', "Query has been submitted succesfully");
$check = check::select('all.*')
->where('name', '=', $post->name)
->get();

If I understand your problem, you need to get result from all table, where 'name' is $post->name. If so please check this:
$post = new query;
$post->name = $request->input('name');
$post->description = $request->input('description');
$post->email = auth()->user()->email;
$post->save();
session()->flash('notif', "Query has been submitted succesfully");
$check = DB::table('all')
->where('name', $post->name)
->get();
And please dont forget to place: use DB; on the top of your Controller

I can't understand what is check here in check::select(). But what I got from query is you want to select specific fields from table "all" after inserting the data in the table "query".
So you can do this to achieve what I understood.
$post = new query;
$post->name = $request->input('name');
$post->description = $request->input('description');
$post->email = auth()->user()->email;
$post->save();
// use any of the following
$check = all::where('name', $post->name)->get(); // SELECT *
$check = all::where('name', $post->name)->get(['field1', 'field2']); // SELECT 'specific'
session()->flash('notif', "Query has been submitted succesfully");

Related

How to Unserialize this data in wordpress

CURRENT ISSUE: I am making one plugin for listing users with there user role in WP_LIST_TABLE table.
This is my query
$this->items = $wpdb->get_results($wpdb->prepare("SELECT {$wpdb->users}.*, {$wpdb->usermeta}.meta_value as roles FROM {$wpdb->users}
LEFT JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID = {$wpdb- >usermeta}.user_id
WHERE {$wpdb->usermeta}.meta_key = '{$wpdb->get_blog_prefix()}capabilities'
ORDER BY {$wpdb->users}.display_name", $per_page, $paged), ARRAY_A);
It display out like This
[roles] => a:1:{s:13:"administrator";b:1;}
How to Unserialize this data. and I want to display the First name too using this query please help me
Finally this helps me.
$input = unserialize($item['roles']);
$result = array();
foreach($input as $key => $value){
$result[] = $key;
}
$userRole = implode(",", $result);
return $userRole;
Try below code:
foreach($this->items as $value){
echo $value->COLUMN_NAME ."<br>";
}
Please change the COLUMN_NAME to whichever column you want to display. If there's a column named e_name, then write "$value->e_name".
Its tried and tested. It works for me. Let me know if it works for you!

Get or fetch Latest or last create multiple rows in laravel

I am in need of desperate help. I am newbie to laravel and programming.
I have a sales controller which fetch multiple records from form using jquery and select.
Sales Store controller looks like:
for ($i=0; $i < count($request['product_id']); ++$i)
{
$sales= new Sale;
$sales->product_id = $request['product_id'][$i];
$sales->qty= $request['qty'][$i];
$sales->user_id = Auth::user()->id;
$sales->save();
$product = new Product;
$product->where('id', '=', $request['product_id'][$i])->decrement('stock', $request['qty'][$i]);
}
this code is working perfectly fine.
Now the scenario is that I want to fetch these last created specific records to send it somehow to other page or as an invoice. Any help will be greatly appreciated on how to achieve this? Thanks.
Make a new array to hold the sales and product data and redirect to your desired url with that data.
$data = array();
for ($i=0; $i < count($request['product_id']); ++$i)
{
$sales= new Sale;
$sales->product_id = $request['product_id'][$i];
$sales->qty= $request['qty'][$i];
$sales->user_id = Auth::user()->id;
$sales->save();
$product = new Product;
$product->where('id', '=', $request['product_id'][$i])->decrement('stock', $request['qty'][$i]);
$data[]['sales'] = $sales;
$data[]['product'] = $product;
}
return redirect("/your desired url")->with('data', $data);
For your second question in the comment, In your controller function of your desired url do this -
$data = [];
if ($request->session()->has('data')) {
$data = $request->session()->get('data');
}
return view('your view', compact('data'));
And then in your view -
#foreach($data as $d)
{{$d['sales']}} //here $d['sales'] is your sales object
{{$d['product']}} //here $d['product'] is your product object
#endforeach
You can do
$sales = Sale::latest()->take(count($request['product_id']))->get();
latest() will just order all the sales by the date of creation in descending order.
if count($request['product_id']) is 5, take() will fetch the first 5 sales.

Cakephp2. save data is adding two more rows

public function index($tin=null) {
$this->layout = 'index_1';
$this->loadModel('Insurance');
$this->loadModel('Verification');
$this->loadModel('Insurance_price');
$date = date('Y-m-d');
$verification_fee=$this->Insurance_price->findByInsurance_type("Third Party Auto Insurance");
$ver = $verification_fee['Insurance_price']['verification_amount'];
if(!empty($tin)) {
$this->request->data['tin'] = $tin;
$this->request->data['amount'] = $ver;
$this->request->data['date'] = date('Y-m-d');
$this->Verification->save($this->request->data);
$this->Verification->clear($this->request->data);
}
}
That's my controller. I am trying to save those data once the page loads.
But when I check the database table instead of seeing only 1 new record I see 3 new rows with the same data as 'amount' and 'date', except for tin column which will have 'image'.
What could I be doing wrong?
try inserting
$this->Verification->create();
before
$this->Verification->save($this->request->data);
And check

cakePHP: Look for a row inside database by using other fields other than primary key?

I'm going to check if a row exists in my DB which one of its field matches a custom value.
e.g. consider table licences which contains fields: (id,serial,validity).
I'm going to check two conditions in my controller:
licence with serial 'xyz' is presents in db
licence with serial 'xyz' have validity field value 'valid'
How should i complete $option for this code:
public function validity($serial = null) {
$this->autoRender = false; // We don't render a view in this example
$options = ?????;
$license = $this->License->find('first', $options);
if ($license){
// it is valid and present
$data = array('validity' => 'valid');
);
}else{
//not present actions
$data = array('validity' => 'invalid');
}
$this->response->body(json_encode($data));
}
The options argument has a lot of parameters you can use like conditions, order and fields. In your case you need conditions
$options=array('conditions'=>array('License.serial'=>'xyz', 'License.validity'='valid'));
(by default it is AND between conditions)

Drupal 7 pull content type fields with db api

What am I doing wrong. I can't seem to pull based upon content type fields in drupal 7.
function ycs_list($number) {
$query = db_select('field_data_field_active_image', 'a')
->fields('a', array('field_active_image_value', 'entity_id'))
->condition('a.field_active_image_value', 1);
$query->join('node', 'n', 'n.nid = a.entity_id');
$query
->fields('n', array('nid', 'title', 'uid'))
->range(0, $number)
->addTag('node_access')
->execute();
print $query;
return $query;
}
This is how the query prints:
SELECT a.field_active_image_value AS field_active_image_value, a.entity_id AS entity_id, n.nid AS nid, n.title AS title, n.uid AS uid FROM {field_data_field_active_image} a INNER JOIN {node} n ON n.nid = a.entity_id WHERE (a.field_active_image_value = :db_condition_placeholder_0) LIMIT 3 OFFSET 0
It looks right and does work directly in mysql. I have to change :db_conditon_placehoder_0 to 1 and it works doing a direct sql query. I want to pull an array of nodes based upon a condition in the active_image field. Any help would be much appreciated.
Without knowing the actual error you're getting it's a bit tricky to debug your existing code, but I'd advise using the EntityFieldQuery class to do this instead:
function ycs_list($number) {
$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'name_of_node_type') // Remove this line if you don't want to specify a particular node type for the query.
->fieldCondition('field_active_image', 'value', 1)
->range(0, $number)
->addTag('node_access');
// Execute the query and check for results.
$results = $query->execute();
$nodes = array();
if ($results && isset($results['node'])) {
// Load the node objects based on the node ids returned from the query.
$nodes = node_load_multiple(array_keys($results['node']));
}
// If nodes matching your conditions were found you now have an array of fully loaded node objects in $nodes.
}

Resources