Laravel nestedwhere Parse error: syntax error, unexpected '}' - eloquent

Symfony\Component\Debug\Exception\FatalThrowableError: Parse error: syntax error, unexpected '}'
I followed this resources for nested where here: How to combine WHERE clauses in Eloquent but get the following error while running the codes. The line where it broke was the curly bracket of the callback function, what's wrong here?
$providers = DB::table('users')
->where('verified', '=', 1)
->where('status', '=', 1)
->where(function ($query) use ($search) {
$query->where(DB::raw('lower(name)'), 'LIKE', "%".strtolower($search)."%")
->orWhere(DB::raw('lower(username)'), 'LIKE', "%".strtolower($search)."%")
})
->where('city', '=', $t)
->take($limit)
->toSql();

You are missing a ;after your inner $query. I think that part could be this:
->where(function ($query) use ($search) {
$query->where(DB::raw('lower(name)'), 'LIKE', "%".strtolower($search)."%")
->orWhere(DB::raw('lower(username)'), 'LIKE', "%".strtolower($search)."%");
})
The code needs that semicolon at the end, because it is a line / statement in a function. It does not matter that this function is a callback.

Related

how to show only last record with query builder in laravel

I only want to display the latest data from the tanggal_ins that I have marked, how to?
This is my json data:
And this is my query:
$cuti = DB::table('t_cuti as cuti')
->join('m_karyawan as kary', 'kary.nik', '=', 'cuti.nik')
->join('m_rumah_sakit as rs', 'rs.kd_rs', '=', 'kary.kd_rs')
->join('m_unit as unit', 'unit.kd_unit', '=', 'kary.kd_unit')
->join('m_jabatan as jab', 'jab.kd_jabatan', '=', 'kary.kd_jabatan')
->leftJoin('t_cuti_adjust as adj', function ($leftJoin) {
$leftJoin->on('adj.nik', '=', 'cuti.nik')
->latest();
})
->select('adj.tanggal_ins','cuti.saldo_cuti','kary.nik','kary.nm_karyawan','rs.nm_rs','unit.nm_unit','jab.nm_jabatan')
->whereYear('adj.tanggal_ins', '<', $year)
->orWhere('adj.tanggal_ins', null)
->get();
Thanks.
Use orderBy.
->orderBy('id', 'desc')->first();
This will make descending order by ID.
->orderBy('id', 'desc')
and use first() instead of get().

What does `error: expected open delimiter` when calling rust macro mean?

I have such a macro:
macro_rules! expect_token (
([$($token:matchers, $result:expr)|+] <= $tokens:ident, $parsed_tokens:ident, $error:expr) => (
match $tokens.pop() {
$(
Some($token) => {
$parsed_tokens.push($token);
$result
},
)+
None => {
$parsed_tokens.reverse();
$tokens.extend($parsed_tokens.into_iter());
return NotComplete;
},
_ => return error(expr)
}
);
)
when I call it with expect_token!([Ident(name), name] <= tokens, parsed_tokens, "expected function name in prototype"); I get the error "error: expected open delimiter".
What does this error mean and what I am doing wrong?
P.S. If you are wondering what is the definition of identifiers like NotComplete, you can look at https://github.com/jauhien/iron-kaleidoscope/blob/master/src/parser.rs, but it is not relevant for this question as far as I understand, as the problem is not with the macro body, but with its invocation.
Ok, I have found the response: matchers in macros invocation should be enclosed in parenthesis. The problem was in my misunderstanding of matchers as left hand side of match rules, while they are lhs of the => in macro rules, which is clearly stated in documentation.
P.S. What about the whole macros I gave as example it is wrong anyway. )

WWW::Mechanize::Firefox xpath on previous result

Can I execute a XPath query on previous result?
I have this xpath:
my #objDivRes = $objBrow->xpath('//div[#id="result"]/ol/div/li', all => 1);
but when I execute xpath function on previous result
my #objLink = $objDivRes[0]->MozRepl::RemoteObject::Methods::xpath('//div/h3/a');
I got an error:
MozRepl::RemoteObject: TypeError: doc.evaluate is not a function at test.pl
Is there an example? Thank you
Just use 'node' option to set up a subtree $mech->xpath( $query, %options )
Note dot in the beginning of the path, which means descendands of the context node
my #objDivRes = $objBrow->xpath('//div[#id="result"]/ol/div/li', all => 1);
my #objLink = $objBrow->xpath('.//div/h3/a', node => $objDivRes[0]);

What's happening with this DBI:db object when errstr is called?

I'm delegating an attribute in my current class called 'dbc' as a DBIx::Connector so that I can call $self->dbc->dbh from inside methods, however I'm not really understanding some behaviors I'm seeing when calling the 'errstr' method on the DBI::db instance:
This:
eval {
$dbh->do($sql);
};
$self->log->warn("Warning SQL error: $dbh->errstr") if ($#);
returns WARN - Warning SQL error: DBI::db=HASH(0xaf43130)->errstr
However, this works, and returns a proper error string:
eval {
$dbh->do($sql);
};
if($#){
my $errstr = $dbh->errstr;
$self->log->warn("Warning SQL error: $errstr");
}
What's happening here?
Perl doesn't interpolate method calls inside double-quoted strings. $dbh->errstr is calling a method. Try:
$self->log->warn("Warning SQL error: " . $dbh->errstr) if $#;
You're trying to interpolate a function call in a string.
Try concatenating the warning string with function call.
$self->log->warn("Warning SQL error: ".$dbh->errstr) if ($#);
This part of the error:
DBI::db=HASH(0xaf43130)
is the address of the db handle.

New to Perl, problems with curly brackets / syntax

I have recently started programming in Perl (I'll skip the long story) but I've stumbled upon a few errors that I can't seem to understand:
syntax error at /usr/sbin/test.pl line 238, near ") {"
syntax error at /usr/sbin/test.pl line 247, near "} else"
syntax error at /usr/sbin/test.pl line 258, near ") {"
syntax error at /usr/sbin/test.pl line 276, near ") {"
syntax error at /usr/sbin/test.pl line 304, near "}"
syntax error at /usr/sbin/test.pl line 308, near "}"
syntax error at /usr/sbin/test.pl line 323, near "}"
it seems to be something to do with the brackets surrounding if and else
I'm experienced in C, C#, Java, PHP, Lua, and others so I'm a bit embaressed to get stuck on syntax errors..
I've pasted a sample of code that generates a syntax error:
if (substr(ToString($buffer),0,4) == 'HELO') {
$contype = 'smtp';
send($client,'250 Welcome',0);
} elsif (substr(ToString($buffer),0,4) == 'EHLO') {
$contype = 'esmtp';
send($client,'250-$hostname Welcome',0);
send($client,'250 SIZE $msgmaxsize',0);
}
do {
recv($client,$buffer,1024,0);
} while (ToString($buffer) != 'QUIT') {
if (substr(ToString($buffer),0,10) == 'MAIL FROM:')
{
$sender = ToString($buffer);
$sender =~ m/<(.*?)>/;
send($client,'250 OK',0);
} else {
send($client,'503 I was expecting MAIL FROM',0);
send($client,'221 Bye',0);
break;
}
}
unfortunately I can not show the entire program.
Perl version 5.10.1
This makes no sense:
...
do {
recv($client,$buffer,1024,0);
} while (ToString($buffer) != 'QUIT') {
if (substr(ToString($buffer),0,10) == 'MAIL FROM:')
...
You're combining a statement modifier (do {..} while...; with a while () {} loop. It's either or.
So write something like:
...
while ( recv($client,$buffer,1024,0) ) {
last if ToString($buffer) eq 'QUIT';
if (substr(ToString($buffer),0,10) eq 'MAIL FROM:') {
...
}
}
etc.
Besides the mistake of using == and != (which are number-comparison operators) instead of eq and ne for string comparisons, you are missing a semi-colon after the while's test. That is, you have
do { ...; } while (...) { if (...) { ... } else {...}}
Note that Perl, like C, supports both forms
while (expr) { stuff }
and
do { stuff } while (expr)
and I'm supposing you meant to use the latter form.
If the above accounts for the error at line 238, then it's possible that the error at line 247 might go away when you correct it, if it somehow causes a dangling else; but without compiling the code, I don't quite see how.