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.
Related
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);
I'm grabbing some data in Laravel using the find method and only grabbing the id's I want:
$my_ids = array(1,4,5,10);
$results = Model::find($my_ids);
However, if I try to paginate via Model::find($my_ids)->paginate(10), it throws the error: Call to undefined method Illuminate\Database\Eloquent\Collection::paginate().
How can I query to only get specific model Id's back from the database while also using pagination?
Turns out I can use this syntax:
$results = Model::whereIn('id', $my_ids)->paginate(10);
I suggest use the eloquent query builder:
$results = Model::whereIn('id', $my_ids)->paginate(10);
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'));
I am new to laravel and I am trying to get a pagination function into my result pages, so I have the following function to generate results from query and I would like to have a pagination on the results page, but I don't seem to get it work correctly
public function showResults()
{
$selectedquery = Input::get('Annonces');
$what = Input::get('what');
$where = Input::get('where');
$results = DB::table('annonces')->where($selectedquery,'LIKE', '%'.$what.'%')
->where('Lieu','LIKE', '%'.$where.'%')
->get();
return View::make('results',array('results' => $results));
}
Any Help?
Well, for one, you're missing the call to ->paginate(n). Right now, your closure is ->get(), which returns all results for your annonces table. This is good, but doesn't work for pagination. Change the function like so:
$results = DB::table('annonces')->where($selectedquery,'LIKE', '%'.$what.'%')
->where('Lieu','LIKE', '%'.$where.'%')
->paginate(10);
This will return all results grouped into 10 results per page. Feel free to change that as you see fit.
Lastly, somewhere on your view where you display the results, you will need to use this code to display a page-viewer:
<?php echo $results->links(); ?>
<!-- OR -->
{{ $results->links(); }}
Also, be sure to check out the docs on Laravel's pagination. You'll find it's pretty comprehensive!
Laravel Pagination
Hope that helps!
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.