Codeigniter ActiveRecord: Where Clause with custom string - codeigniter

I would like to search for the following string using CI active records.
Click on the Event or Booking Code you would like to use.
My query is as follows:
$this->db->selet("id");
$this->db->where("column_name", "Click on the Event or Booking Code you would like to use.");
$this->db->from("table_name")
$ret = $this->db->get();
But it doesn't work since CI will escape the string and produce the select query as follows:
SELECT id FROM table_name WHERE column_name = 'Click on the Event or
Booking Code you would like to use.'
Is there any workaround for this issue?

This should work:
$this->db->where("column_name = 'Click on the Event or Booking Code you would like to use.'", NULL, FALSE);

Related

Doctrine Statement: Select from Database with uuid (value isnt readable)

in Symfony i need access to another Database from another Project. So i created another entityManager for this connection.
Now i want read some Data where id field is uuid.
the statement looks like this:
$entityManager = $this->doctrine->getManager('myProject');
$connection = $entityManager->getConnection();
$statement = $connection->prepare(
'SELECT * FROM salutation '
);
$resultSet = $statement->executeQuery();
dd($resultSet->fetchAll());
But the id value is not readable.
"id" => b"\x0E2#\x05P\x08Gó€dˆk§\vè\x05"
Is there a way to tell Doctrine to handle this field as Uuid?
Thanks for your Help.

How to get specific column with custom column name on Laravel

Looking for a way to get specific columns from Laravel Eloquent with custom column names.
E.g. get example_name as name.
I am trying to replicate SQL Alias like SELECT column_name AS alias_name but without using custom attributes.
Here is and example query in Laravel:
Table::get(['column1', 'column2', 'column3'])->all()
And what I would like it to get would look something like this:
Table::get(['column1', 'column2 AS column4', 'column3'])->all()
for example in user model email as emailAddress u want to get then in your user model do like that
protected $appends = ['emailAddress']; //add custom column here..
public function getEmailAddressAttribute()
{
return $this->email;
}
or u can use by query like that
User::select('id','name','email as emailAddress')->get();
Table::select('column1', 'column2 AS column4', 'column3')->get()
for more detail read https://laravel.com/docs/5.8/eloquent-mutators

trying to Fetch Single record from table in laravel using ajax

here is my code that i write in route file
`Route::get('/{username}/ajax-lead', function(){
$brand_id = Input::get('brand_id');
$table_data = DB::table('users')->where('brand_id',$brand_id)->get();
$table_name = $table_data[0]->table_name;
$table_lastRecords = DB::table($table_name)->get();
$columns = Schema::getColumnListing($table_name);
$table_lastRecords = DB::table($table_name)->get();
return Response::json($table_lastRecords);
});`
here is my view file
`{{Form::open(array('url'=>'','files'=>true))}}
client list *
Select Clients
#foreach($brand as $userLeads){
brand_id }}">{{ $userLeads->name }}
}
#endforeach
{{Form::close()}}
$('#brand_name').on('change',function(e){
// body...
console.log(e);
var brand_id = e.target.value;
//ajax Call
$.get('/{username}/ajax-lead?brand_id=' + brand_id,function(data){
//success data
console.log(data);
});
});
`
i want to get table_name column value in response. i use json for response.
ANSWER: Your easiest solution would be to return an array of data, where the first value is the table_name and the second value is the collection of lastRecords. It would look something like this:
$responseArray[0] = $table_name;
$responseArray[1] = $table_lastRecords;
return json_encode($responseArray);
Then, in your view, you would parse your Json response and let table_name = responseData[0] and lastRecords = responseData[1]. Of course, you could also use non-numeric indices to make the code more readable, such as responseData['table_name'], but those semantics are up to you.
ADDITIONAL INFO:
With that said, there are several issues with your code that I feel I should point out. You should become more familiar with Eloquent queries, as using them properly will greatly simplify your code. For instance, these two lines:
$table_data = DB::table('users')->where('brand_id',$brand_id)->get();
$table_name = $table_data[0]->table_name;
Could be re-written as:
$table_name = User::where('brand_id',$brand_id)->first()->table_name;
But this makes me ask the question, is there only one table per brand? If so, why do you store this information with the user, and not in a 'brands' table? The way that this would make the most sense would be to have a Brands model that has a table_name column, and you would get the table name with this simple query:
$table_name = Brand::find($brand_id)->table_name;
In addition, you should avoid doing this type of work in your routes file. Anything not related specifically to ROUTING the user should be handled in the controller or model. Then, within your controller, you should be utilizing the User or Brand model in your queries by importing App\User and App\Brand.

Laravel - Multiple models in one controller

I have the following database structure:
Enquiries
id
total_widgets
total_amount
customer_id
Customers
id,
first_name,
last_name
Using the form when you are creating an Enquiry you can enter the Customers details into the section and this will store using firstOrCreate and then get the id in order to link to Enquiry to the Customer
Now, the issue is that this is all done inside the store method within the Customers controller, like the following:
public function store(Request $request)
{
$rules = array(
"first_name" => "required",
"last_name" => "required",
"total_widgets" => "required"
);
// Handle validation
// Create customer
$customer = \App\Customers::firstOrCreate(['first_name' => $request-
>get('first_name')]);
$enquiry = new \App\Enquiries();
$enquiry->customer_id = $customer->id;
$enquiry->save();
}
The issue with doing it like this is that it's not separated out and that if the process of creating a customer changes, then I would need to change this a lot of times. (Everytime I have a section which requires a customer)..
Is there a better way to do this? For example, should the customer be created separately and then the id is passed into the $request?
Another way of doing this is :
1) In Enquiries form add button called "add new customer".
2) When you click on this button a modal will appear and then fill details click submit.
3) On clicking submit make an ajax call to Customercontroller and insert the data then return the last inserted id.
4) Now you can see the new user in drop down box(There will be some drop down for selecting the user) of enquirey form just select it and then press submit.
5) It will passed to the enquirey controller and and store it.
Hope this will help you.

Creating event id from database?

I'm populating the calendar with a json feed.
$return_arr = array();
$fetch = mysql_query("SELECT * FROM events");
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$row_array['rowid'] = $row['id'];
$row_array['title'] = $row['title'];
$row_array['start'] = $row['start'];
$row_array['end'] = $row['end'];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
And render the event:
calendar.fullCalendar('renderEvent',{
title: 'title',
start: start,
end: end,
allDay: allDay,
id: rowid
},
true // make the event "stick"
);
When I use eventClick to get the id I get _fc1, _fc2 etc. Is there anyway I can get the id generated by my database? so I can refer to each individual event for updating?
Many thanks
You can simple add a custom field to your Event and then reference that in your click event.
From the Docs
Non-standard Fields
In addition to the fields above, you may also include your own
non-standard fields in each Event Object. FullCalendar will not modify
or delete these fields. For example, developers often include a
description field for use in callbacks such as eventRender.
You can use the same DBid as event id: $row_array['id'] = $row['id'];.
In eventClick(event,...) you will get it as event.id.

Resources