if query returning a return error laravel - laravel

I need some help with this query, I don't understand why am I getting this error "Parse error: syntax error, unexpected 'return'(T_RETURN)" even though my previous line was working. I checked on other question similar to this error and see if I have similar mistake but I don't think so, I even double checked to see if I am missing any bracket in my query. Can someone please help me? Thanks a lot.
This part of the code work, "if(count($data)>0){" but when I changed to this it doesn't work anymore " if (hire::where('hire_status','Yes'->count($data) > 0){"
Controller:
public function getHire(){
$data['data'] = DB::table('personal_infos')->where('deleted_at',NULL)->get()->sortByDesc('created_at');
if (hire::where('hire_status','Yes'->count($data) > 0){
// if(count($data)>0){
return view('hire',$data);
}else{
return view('hire');
}

Missing closing parenthesis ).
if (hire::where('hire_status','Yes'->count($data) > 0){
if (hire::where('hire_status', 'Yes')->count($data) > 0) {

Related

Condition Using Controller in Code Igniter

I'm trying to create a function, it exists and does not exist in the search feature. Condition does not exist, will display Error 404. Condition, exists. Then it will display from search. But in the code I wrote, it only shows Error 404.
This is my model:
function search_album($keyword){
$result=$this->db->query("SELECT tbl_album.*,DATE_FORMAT(album_date,'%d %M %Y') AS date FROM tbl_album WHERE album_title LIKE '%$keyword%'");
return $result;
}
This is my controller:
function search(){
$keyword=str_replace("'", "", $this->input->post('xfilter',TRUE));
$x['data']=$this->model_album->search_album($keyword);
if(empty($x) && empty($keyword)){
$this->load->view('view_contents',$x);
}
else if (!empty($x) && !empty($keyword)){
$this->load->view('view_error_404');
}
}
I've tried from this source, but it doesn't work. Can you help me?
There are several errors in your code:
ONE: You have interchanged your if statements. Like, if the search did not return any data and the keyword was not supplied, it is supposed to display the "view_error_404", otherwise, it is supposed to load data into the view "view_contents".You did the vice versa I have corrected that for you in the code below.
TWO: You are checking if $x is empty which will never be empty as you have initialized $x['data']. Note that the model can return empty data, such that $x['data'] is empty. Instead, check if the search result is empty by replacing empty($x) with empty($x['data'])
THREE: In your model, you are returning the query builder class, but not the actual data from the query statement. instead, replace return $result; with return $result->result();
FOUR:
From your if statements, you need to add an else statement so that if the 2 conditions are never met, it can execute. With your current implementation, there is a state which will not meet first or second conditions and will lead to a blank screen.
if(empty($x['data']) && empty($keyword)){
$this->load->view('view_error_404');
}else if (!empty($x['data']) && !empty($keyword)){
$this->load->view('view_contents',$x);
}else{
$this->load->view('view_error_404'); // replace this code with your preference
}
you need to mention search function in routes.php file like this:-
$routes['search'] ='your_controller_name/search';
without knowing your function model_album->search_album($keyword) and assuming it returns a string or array, you don't need to worry about checking for the keyword, since without a keyword the function should return false or empty.
anyway you mixed up your if/else conditions, since you are checking for not empty results to return a 404, instead of empty result returning the 404.
this should do it:
if(empty($x)){
$this->load->view('view_error_404');
}
else {
$this->load->view('view_contents',$x);
}

Codeigniter 4 testing when controller method not found

I am doing HTTP testing in Codeigniter 4 and am getting the following error:
CodeIgniter\Exceptions\PageNotFoundException: Controller method is not found
The reason for this is I am trying to call a get() method on a route that only has a post (switching it to add results in a different response). That is correct. I am trying to make sure that the page returns 404 if someone tries to hack it. The problem is Codeigniter doesn't assert Status is 404 it simply falls over. Here is my code triggering:
$r = $this->withSession($session)->get($url);
$r->assertStatus(404);
Note the assert never runs! Instead I get the original error message. Surely I should be able to check for 404 routes (I've tried '404' instead of int) ?? Yes I could set a custom route that allowed Get but I want to test it as-is rather than what it isn't.
How do I get CI4 to accept my incorrect route as a 404?
My solution is to define a constant (IN_TESTING) for when I am testing and then edit system/Codeigniter run() function. This is of course amending the core system which some people won't like but until there is a fix (I am using 4.1.2) then this will suffice.
catch (PageNotFoundException $e)
{
if (!defined('IN_TESTING') || IN_TESTING == false){
$this->display404errors($e);
} else {
$this->router = Services::router($routes, $this->request);
$path = $this->determinePath();
$controller = $this->router->handle($path);
$method = $this->router->methodName();
echo $path."\n".$controller."\n".$method;
exit;
$this->response->setStatusCode(404);
return $this->response;
}
}
Key tip if you do make changes to system files, track them! If you then need to run an upgrade you'll know what changes you've made.

Trying to sum a groupBy value in Laravel 5.5

I have this code suggested to me. I am trying to sum the stock_in_qty and stock_out_qty.
$warehouse1stocks = Warehouse1stocks::select(
"order_item_id",
Warehouse1stocks::raw('SUM(stock_in_qty) as stock_in_qty'),
Warehouse1stocks::raw('SUM(stock_out_qty) as stock_out_qty'))->groupBy('order_item_id')->get();
// dd($warehouse1stocks);
return view("warehouse1.index", compact("warehouse1stocks", $warehouse1stocks));
My problem is this error
I tried to look for an answer from other questions here and I think my code seems ok but why am i still having that error?
what do you think is the problem with my code? thanks in advance!
Instead of writing:
Warehouse1stocks::raw('SUM(stock_in_qty) as stock_in_qty')
write:
DB::raw('SUM(stock_in_qty) as stock_in_qty')
when defining the write expressions
use this one
$warehouse1stocks = Warehouse1stocks::select(
"order_item_id",
\DB::raw('SUM(stock_in_qty) as stock_in_qty'),
\DB::raw('SUM(stock_out_qty) as stock_out_qty'))->groupBy('order_item_id')->get();
// dd($warehouse1stocks);
return view("warehouse1.index", compact("warehouse1stocks", $warehouse1stocks));

Typescript 1.4: Arrow function without curly braces, syntax changed?

I am following some tutorials that says you can do this
module testme {
var testmeA = function(num) => num * num;
}
but i am getting an error on the => saying { missing. In the tutorial the above works.
After sometime playing around I got the following to work
var testmeA = (num: number) => {return num*num};
So you will notice i was "forced" to remove the function keyword, although the tutorial says you can but you are not forced to do and i was forced to add curly brakets after the =>
I was hoping for some insight to understand if I am doing something wrong or the syntax has changed ?
If the syntax has changed, where is the change documented ?
The version I am using is
➜ ~ tsc -v
message TS6029: Version 1.4.1.0
Thanks in advance
Remove only the function keyword:
var testmeA = (num: number) => num * num;

Label must be a simple identifier?? Flash/Flex Builder

I have this code:
public function chooseCategoryDDL_changeHandler(event:IndexChangeEvent):void {
var para:Object = new Object();
para.action = "changecategoryxml";
para.book_class = event:IndexChangeEvent.book_class;
if (event.IndexChangeEvent > -1 ) {
changeCategory.send(para);
}
I keep getting the error message that, 'Label must be a simple identifier'. Ideally, i'm wanting to write the code to state:
... para.book_class = event.selectedItem.book_class;
if (event.selectedItem > -1 ) {
changeCategory.send(para);
}
Though, when I try and use the selectedItem syntax, it gives me an 'Access to undefined property selectedItem'. I'm really tearing my hair out about this and it's been bugging me for ages. If anyone can please shed any light on this I will be eternally grateful :)
Thanks
I had the same error from a dumb typo : some line was terminated by ':' instead of ';'
For the visually impaired (like me), that's colon instead of semicolon.
I didn't really read this, but the syntax of this line looks invalid:
para.book_class = event:IndexChangeEvent.book_class;
Use a dot maybe?
para.book_class = event.IndexChangeEvent.book_class;

Resources