I wanted to select two columns and display it as one in my controller. Here is the code I currently have:
public function assignment()
{
$title = "View Parent Assignment";
$vpc = DB::table('dbo_guardianchild')
->join('dbo_students', 'dbo_guardianchild.StudentID', '=' , 'dbo_students.StudentID')
->join('dbo_guardianinformation' , 'dbo_guardianchild.GuardianInformationID' , '=' , 'dbo_guardianinformation.GuardianInformationID')
->select('dbo_students.StudentID' , 'dbo_students.FirstName AS sFname' , 'dbo_students.LastName AS sLname')
->get();
}
Any ideas on how I can combine the dbo_students.FirstName and dbo_students.LastName into one column?
You should try DB::raw. Try replacing the code in select as
->select(DB::raw('dbo_students.StudentID' ,'CONCAT(dbo_students.FirstName, " ", dbo_students.LastName) AS full_name'))
And your final code will be
public function assignment()
{
$title = "View Parent Assignment";
$vpc = DB::table('dbo_guardianchild')
->join('dbo_students', 'dbo_guardianchild.StudentID', '=' , 'dbo_students.StudentID')
->join('dbo_guardianinformation' , 'dbo_guardianchild.GuardianInformationID' , '=' , 'dbo_guardianinformation.GuardianInformationID')
->select(DB::raw('dbo_students.StudentID' ,'CONCAT(dbo_students.FirstName, " ", dbo_students.LastName) AS full_name'))
->get();
}
Related
In my project I managed to do a search via request and I wanted to do a search filtered by name (in this case already) and also by reference.
I looked on the internet and nothing that helps me up so far.
I appreciate your help:\
else {
$valida=2;
$produtc = Produtos::where('nome' & 'refencia', 'like', "%{$tpesquisa}%")->paginate();
$nrr = count($produtos);
$msgm = 'Pesquisa concluida com sucesso, foram econtrados ' .$nrr. ' resultados';
return view('pesquisa', compact('produtc', 'msgm', 'nrr', 'valida'));
You are doing it wrong please try with the following way.
else {
$valida=2;
$produtc = Produtos::where('nome', 'like', "%".$tpesquisa."%")->orWhere('refencia','like', "%".$tpesquisa."%")->paginate();
$nrr = count($produtos);
$msgm = 'Pesquisa concluida com sucesso, foram econtrados ' .$nrr. ' resultados';
return view('pesquisa', compact('produtc', 'msgm', 'nrr', 'valida'));
If you want to do it using AND operator then use following code.
else {
$valida=2;
$produtc = Produtos::where('nome', 'like', "%".$tpesquisa."%")->where('refencia','like', "%".$tpesquisa."%")->paginate();
$nrr = count($produtos);
$msgm = 'Pesquisa concluida com sucesso, foram econtrados ' .$nrr. ' resultados';
return view('pesquisa', compact('produtc', 'msgm', 'nrr', 'valida'));
Give this a try I use this when ever i need to do a search
Produtos::where(function ($query){
$query->orWhere('nome', 'LIKE', "%". $tpesquisa. "%")
->orWhere('refencia', 'LIKE', "%". $tpesquisa. "%");
})
->paginate();
I want to get subquery from impala table as one dataset.
Code like this:
String subQuery = "(select to_timestamp(unix_timestamp(now())) as ts from my_table) t"
Dataset<Row> ds = spark.read().jdbc(myImpalaUrl, subQuery, prop);
But result is error:
Caused by: java.sql.SQLDataException: [Cloudera][JDBC](10140) Error converting value to Timestamp.
I can use unix_timestamp function,but to_timestmap failed, why?
I found code in org.apache.spark.sql.execution.datasources.jdbc.JDBC.compute() exists some problem:
sqlText = s"SELECT $columnList FROM ${options.table} $myWhereClause"
$columList contains " like "col_name" , when I delete " it work fine.
I solve this problem by add dialect, default dialect will add "" to column name,
JdbcDialect ImpalaDialect = new JdbcDialect(){
#Override
public boolean canHandle(String url) {
return url.startsWith("jdbc:impala") || url.contains("impala");
}
#Override
public String quoteIdentifier(String colName) {
return colName;
}
};
JdbcDialects.registerDialect(ImpalaDialect);
How to prevent auto fix in the select statement using CodeIgniter 3?
Query that I want: SELECT REPLACE(item, ',', '') AS item (without space)
I tried : $this->db->select("replace(item, ',', '') AS item", FALSE);
Result : SELECT REPLACE(item, ', ', '') AS item (CI added a space in my query)
I read this documentation but it didn't work.
This my real query:
$this->db->select("a.unit_id");
$this->db->select("replace(c.topik, ',', '') AS topik", FALSE); //this my problem (added space which shouldnt)
$this->db->select("COUNT(*) AS jumlah");
$this->db->from('opub_kliping a');
$this->db->join('opub_media b','a.media_id = b.organisasi_id');
$this->db->join('opub_tag c','a.id = c.kliping_id');
if(!empty($data['keyword']))$this->db->where("(a.judul LIKE '%".$data['keyword']."%' OR a.text LIKE '%".$data['keyword']."')");
if(!empty($data['tgl1']))$this->db->where("a.tanggal >=",$data['tgl1']);
if(!empty($data['tgl2']))$this->db->where("a.tanggal <=",$data['tgl2']);
if(!empty($data['unit']))$this->db->where("a.unit_id ",$data['unit']);
if(!empty($data['media']))$this->db->where("a.media_id ",$data['media']);
if(!empty($data['jenis']))$this->db->where("a.jenis_id ",$data['jenis']);
if(!empty($data['tone']))$this->db->where("a.karakter ",$data['tone']);
if(!empty($data['topik']))
{
$qTopik = array();
$expTopik = explode(',', $data['topik']);
foreach($expTopik as $t)
{
if(!empty($t))
{
$qTopik[] = "c.topik LIKE '%$t%'";
}
}
if(!empty($qTopik))
{
$qTopiks = implode(",", $qTopik);
$this->db->where($qTopiks);
}
}
$this->db->where('a.unit_id !=', 0);
$this->db->where('c.topik !=', NULL);
$this->db->where('c.topik !=', '');
$this->db->group_by("a.unit_id");
$this->db->group_by("replace(c.topik, ',', '')", FALSE); //this produce correct query (without adding space)
$this->db->order_by('a.unit_id', 'ASC');
$this->db->order_by('jumlah', 'DESC');
$q= $this->db->get();
i found this after tried some experiment
$this->db->select(array('REPLACE(c.topik', "','", "'') AS topik"), false);
seems little hack maybe. but its work for me
hope will help others
change 4 lines of your query
$this->db->select("a.unit_id,replace(c.topik, ',', '') AS topik,COUNT(*) AS jumlah")->from('opub_kliping a');
$this->db->join('opub_media b','a.media_id = b.organisasi_id');
I have four tables I need to join in query build
function get_debtors( ) {
$this->db->select('invoice.student_id as STUDENT_ID,student.name AS
STUDENT_NAME,section.name AS CLASS,
invoice.description AS DESCRIPTION,
invoice.amount AS TOTAL_AMOUNT,
invoice.amount_owed AS AMOUNT_OWED,
parent.name AS PARENT_NAME,
parent.email AS PARENT_EMAIL,
parent.phone AS PARENT_PHONE,
parent.address AS PARENT_ADDRESS,
invoice.status AS STATUS');
$this->db->from('invoice , student , parent , section ');
$this->db->join('student ', 'student.student_id = invoice.student_id');
$this->db->join('parent ', 'parent.parent_id = student.parent_id, 'left'');
$this->db->join('section ', 'section.section_id = student.section_id','left');
$this->db->where('invoice.status', 'debtor');
$query = $this->db->get();
Try this. If there is still any error than post the error also here.
function get_debtors( )
{
$this->db->select('invoice.student_id as STUDENT_ID,student.name AS
STUDENT_NAME,section.name AS CLASS,
invoice.description AS DESCRIPTION,
invoice.amount AS TOTAL_AMOUNT,
invoice.amount_owed AS AMOUNT_OWED,
parent.name AS PARENT_NAME,
parent.email AS PARENT_EMAIL,
parent.phone AS PARENT_PHONE,
parent.address AS PARENT_ADDRESS,
invoice.status AS STATUS');
$this->db->from('invoice');
$this->db->join('student ', 'student.student_id = invoice.student_id');
$this->db->join('parent ', 'parent.parent_id = student.parent_id', 'left');
$this->db->join('section ', 'section.section_id = student.section_id','left');
$this->db->where('invoice.status', 'debtor');
$query = $this->db->get();
}
I want to create a slug. it should be a concatenation of title and subtitle.The following is my code and it's not working, i don't know where i went wrong?
public function setTitleAttribute($value)
{
$this->attributes['main_title'] = ucfirst($value);
$this->attributes['sub_title'] = $value;
if (! $this->exists) {
$this->attributes['slug'] = str_slug($this->attributes['main_title'].$this->attributes['sub_title']);
}
}
I need slug as a combination of main_title+sub_title
public function to_slug ($string) {
$table = array(
'Š'=>'S', 'ı'=>'i', 'ğ'=>'g', 'ü'=>'u', 'ş'=>'s', 'ö'=>'o', 'ç'=>'c', 'Ğ'=>'G', 'Ü'=>'U', 'Ş'=>'S',
'İ'=>'I', 'Ö'=>'O', 'Ç'=>'C',
'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
);
return preg_replace('/[^A-Za-z0-9-]+/', '-', strtr($string, $table) );
}
I'm using this code and its working for me.
It is also good for utf-8.
You can use a laravel package:
https://github.com/cviebrock/eloquent-sluggable
Or I use JavaScript when I sent the form:
https://github.com/madflow/jquery-slugify
I have replicated the exact condition like yours passing static data and its working perfectly in my machine.. try below solution.. do some dd and see whats the combination of the main_title and sub_title is resulting. Also check by removing that if condition once.
$this->attributes['main_title'] = ucfirst($value) . " ";
$this->attributes['sub_title'] = $value;
$slugToUse = $this->attributes['main_title'] . $this->attributes['sub_title'];
if (! $this->exists) {
$this->attributes['slug'] = str_slug($slugToUse);
}
}