Where Clause in eloquent is responding with `No Properties` - laravel

I am new to laravel. I am trying to keep a where clause for the get method like this.
$employees = newdb::where('status', 'Active');
$response = $employees;
return response()->json($response,200);
But i am getting No Properties as output
In PHP I used this
"SELECT * FROM newdb WHERE status = 'Draft'";
What am i doing wrong? I tried different suggestions But none worked correctly. How do i do this? What is wrong with my code?

where method returns Builder object. So, you have to call get method on it to fetch data.
Try this
$employees = newdb::where('status', 'Active')->get();
return response()->json($employees, 200);

Related

laravel update table returns error

I have the following issue when I try to update the laravel table I send my data via ajax everything is good. But the update returns an error.
the following function receives the data and updates the table.
public function saveCalendar(Request $request) {
$calendar = $request->calendar;
$apartment_id = $request->apartment_id;
apartments::where('Apartment_ID', $apartment_id)->update(array('calendar' => $calendar));
$confirmation = 'Календара е запазен успешно !';
return $confirmation;
}
I also tried this query:
apartments::where('Apartment_ID', $apartment_id)->update('calendar' => $calendar);
Any idea what am I doing wrong.
Error is because update function accepts array. Correct syntax is
apartments::where('Apartment_ID', $apartment_id)->update(['calendar' => $calendar]);
Also the correct syntax for fetching resquest inputs are
$calendar = $request->input('calendar');
$apartment_id = $request->input('apartment_id');

Laravel: Call to undefined method save()

I want to update all my records where the name is like the one im referencing it.
However im getting an error saying Call to undefined method save()
Here's my code
$section = Section1::where('name', 'like', 'ss123')->get();
$section->name = Input::get('name');
Please help :(
$section = Section1::where('name', 'like', 'ss123')->get() returns a collection (think of an array).
Therefore you should loop through the collection using foreach and apply the save to each object in the collection, as such:
foreach($section as $s)
{
$s->name = Input::get('name');
$s->update();
}
Then again this ain't very DB-friendly.
Therefore, the most appropriate action in your case will be a mass update:
Section1::where('name','like','ss1213')->update(['name'=>Input::get('name')]);
try dd($section) and find the available method to prevent call the undefined method.
I assume you are using $section->save();
if you want to update,
use $section->touch();
or $section->update(array('name' => Input::get'name'));

laravel Eloquent ORM - How to get compiled query?

In Laravel 4.2 I want to get Compiled Query.
This is what i have:
$product = Product::where('id', '=', '100')->get();
I want compiled query like:
select * from products where id = 100
Purpose of the question is: i want to use it as sub query in another query.
I have searched and found Class Grammer and Class MySQL But i did not found solution for that.
Is there any solution?
Your help would be appreciated.
The easiest way is probably mostly finding out what query was just executed, rather than what the query will be. Something like this:
function latestQuery()
{
$queries = DB::getQueryLog();
return end($queries);
}
Hopefully that's the same for your purposes.
You can get the SQL query like this:
use DB;//write this at the top of the file above your Class
DB::enableQueryLog();//Enable query logging
$product = Product::where('id', '=', '100')->get();
dd(DB::getQueryLog());//print the SQl query
You can register an event listener in your routes file(in development phase), which will listen for the laravel query event and var_dump the executed query.
Event::listen('illuminate.query', function($sql)
{
var_dump($sql);
});
But this will turn out to be messy. So better use something like Clockwork. It is awesome, you can view all the executed query in your browser.
You can use grammer for subqueries, See below example for reference :
$users = DB::table('users')
->where('user.id', '=', $userID)
->join('product', 'product.userid', '=', 'user.id');
$price = $users->select(array(DB::raw('SUM(price)')))
->grammar
->select($users); // Compiles the statement
$result = DB::table('users')->select(array(DB::raw("({$price}) as price)))->get();
This add a subquery to your main query.

laravel clean up empty query vars

I want to remove empty query vars from a url in my controller. my url is /search?qi=yoga&q= notice that q is empty. Sometimes qi will be empty. How can I remove these? Seems like a simple issue, but I can't seem to find a elegant solution.
function search() {
$qi = Request::get('qi');
$q = Request::get('q'));
$results = getResults($qi, $q);
return View::make('search.results', compact('results'));
}
You could do that in the next request, but you would have to Redirect::refresh() or Redirect::to($url) with a clean url, like
$items = Redirect::query();
$items = $this->removeEmptyItems($items); /// you'll have to create this method!
return Redirect::route('your.current.route', $items);
As you can see, this will clean up your url, but it requires a new request.
But this looks like something you have in your current request and I'm afraid Laravel cannot change a URL in the browser for you. If this is a form submission query, Javascript can help you prevent from sending those empty queries:
$('form').submit(function(){$('input[value=]',this).remove();return true;})
I suggest this:
function search()
{
$search = array_filter(Request::all()); // or only(..) / except(..)
$results = getResults($search);
}

Laravel Pagination with appends() Error

I am new to Laravel and am using version 4.1.
I am attempting to query database using pagination and then run the results through the appends() function to add additional parameters to my URL.
Here is the code I am using
$query = DB::table('tableName');
$query->paginate(50);
$results = $query->get();
And that runs as desired. Now when I attempt to create the pagination list (Bootstrap default) and run the following code I get an error.
$pagination = $results->appends(array('key' => 'value'))->links();
This is the error I receive.
Call to a member function appends() on a non-object
I know I'm doing something wrong, I just can't figure out what...
Thanks in advance,
SC
I'm not familiar with the appends function, but you do have an error I can see. Try changing
$query = DB::table('tableName');
$query->paginate(50);
$results = $query->get();
To...
$query = DB::table('tableName');
if(Input::has('someinput')) {
$query->where('someinput', Input::get('someinput'));
}
if(Input::has('otherinput')) {
$query->where('otherinput', Input::get('otherinput'));
}
$results = $query->paginate(50);
Once you run paginate(), an instance of Paginator is returned. I don't see a get() method for Paginator though so I'm not sure how you weren't getting an error there.

Resources