Turn Codeigniter Query to Laravel - php

Sorry for broken english.
I've codeigniter query like this :
$this->db->select('tbl_news.id','tbl_news.news_title','tbl_news.news_desc','tbl_news.date_created','tbl_source.source as source','tbl_news.thumb','tbl_category.category');
$this->db->from('tbl_news');
$this->db->join('tbl_category','tbl_category.id','=','tbl_news.news_category')
$this->db->join('tbl_source','tbl_source.id','=','tbl_news.source')
$data = $this->db->get('', $size, $page)->result_array();
and i try to turn that query to my laravel using query builder, like this :
$data= DB::table('tbl_news')
->select('tbl_news.id','tbl_news.news_title','tbl_news.news_desc','tbl_news.date_created','tbl_source.source as source','tbl_news.thumb','tbl_category.category')
->join('tbl_category','tbl_category.id','=','tbl_news.news_category')
->join('tbl_source','tbl_source.id','=','tbl_news.source')
->get('',$size,$page)
->toArray();
is it posible in laravel to have 'get' with parameter ? because i want to make pagination, but i dont want to use paginate because it will affect the response that i used in my android.
->get('',$size,$page)

Referring to the API documentation for the query builder indicates that the get() method does not support these arguments.
Instead, you should either use the paginate() method or manually paginate the response.

Related

Laravel's Illuminate paginator can't find query string

I'm working on a old PHP project, that is running in legacy SQL query, which is good but I like to use query builders like Laravel Illuminate SQL package!
So i have added all required package dependencies to run Illuminate SQL, and this query builder seems to work fine with pagination!
$users = Capsule::table('users')->paginate(1)->toArray();
But, the paginator seems not to be able to listen the query string! For example, by running the above code it would give some properties like, next_page , previous_page etc...
And when I try to pass the query string in the URL it can't fetch the data from query string(From the GET request)!
Visiting this page http://app.app/?page=2 would give the same result set.
How i should configure the Illuminate sql package so it can also listen to the query strings?
EDIT
Also, i've tried to use the illuminate/http package, but the $request->all() method always returns an empty array! Here is the code:
<?php
require_once './vendor/autoload.php';
use \Illuminate\Http\Request;
$req = new Request();
echo '<pre>';
print_r($req->all());
echo '</pre>';
It returns empty input data array,
What i am missing to use the http package, any idea would be helpful.
You have to set a page resolver:
\Illuminate\Pagination\Paginator::currentPageResolver(function ($pageName = 'page') {
return (int) ($_GET[$pageName] ?? 1);
});
Laravel uses this resolver.
You need to notice, that paginator won't use other parameters for query string - it just uses page parameter to get valid results page. But for example if you use:
http://app.app/?page=2&price=2
it won't take from database results with price = 2 - it's your job to do this.
However the 2nd thing are urls generated by paginator.
If you do it like this:
$users = Capsule::table('users')->paginate(1);
You can also use in next line
$users->appends(request()->except('page'));
This will add all other parameters from query string (except page) to urls, so first_page_url will then contain all other parameters from request.
You can also wrap it using fluent syntax like this:
$users = Capsule::table('users')->paginate(1)->appends(request()->except('page'));
You need to fetch the query string in the controller to pass the get parameter to
paginate.
You can use the Illuminate\Http\Request class to receive and use the HTTP payload.
Your controller file:
<?php
use Illuminate\Http\Request;
class YourControllerClassName{
public function someFunction(Request $request){
echo "<pre>";
print_r($request->all());
print_r($request->input('page'));
$users = Capsule::table('users')->paginate($request->input('page'))->toArray();
}
}

Laravel: How can i pass data from controller to blade using yajra/laravel-datatables

I am using yajra/laravel-datatables. It's successfully installed. Now, how can i pass data Laravel Controller to blade??
$contacts = Contact::Where('is_deleted',0)->get();
This is the code. There are lots of data. For those reason, It takes lots of time to load in dataTable. I want to load and pass data to dataTable using yajra/laravel-datatables.
There are many option which you can use
compact method
$data = Contact::Where('is_deleted',0)->get();
return view('home',compact('data'))
with method
$data = Contact::Where('is_deleted',0)->get()
->with('data',$data);
For more information, read the documentation

UnexpectedValueException in Response.php line 403 - Laravel 5.1

I am new to Laravel and am building a small project as my schooling. I have struck the above error and I cannot seem to fix it. Any help is appreciated.
In my controller I make the following call when first loading the page. The getDetails method works perfectly on page load, but fails with above error when I call it with Ajax as the user interacts with the page after load.
On page load the method is called and passed to it resulting values from earlier other method calls ($CSpec[0]->id and $CSize[0]->size_mm, which I can see as values 1 and 0.5) as follows:
$CD = CS::getDetails($CSpec[0]->id, $CSize[0]->size_mm);
Also in the controller I have the following function which triggered via a route used in an Ajax call:
public function itemDetails($ct, $cs)
{
return CS::getDetails($ct, $s);
}
The getDetails method looks like this:
public static function getDetails($ct, $cs)
{
return DB::table('recommend_view')
->where('recommend_view.id', '=', $ct)
->where('recommend_view.size_mm', '=', $cs)
->first();
}
I have this route:
Route::get('vd/cd/{cd}/{cs}',
['uses' => 'Application\VDController#itemDetails', 'as' => 'application.vdcdetails']);
When I use Ajax to call the same method it fails with the above error. The Ajax code produces the following url in the $.getJSON call:
http://my.app/vd/cd/1/0.5
If I dd() inside getDetails for each variable I can see the correct values passed to the method from the url / controller.
If I execute the query in MySQL Workbench, it works as expected:
select *
from `recommender_view`
where `recommender_view`.`ct_id` = 1
and `recommender_view`.`cs_size_mm` = 0.50
limit 1;
So the method works perfectly when called on initial page load with variables fed to it directly from other method calls, but when the variables are passed via the URL / Ajax it fails and I cannot seem to understand what the difference is.
UnexpectedValueException in Response.php line 403:
The Response content must be a string or object implementing __toString(), "object" given.
I believe I know what your issue is, however, please feel free to correct me.
What you're using is the Laravel query builder, which returns a stdClass object type in PHP, assuming it finds a result set for your query. This is effectively a "dumb" object that has no methods to "echo" or return. You can't cast to array, nor json etc.
So when the Laravel response object is trying to handle it, it can't use the __toString() method to deconstruct an appropriate response.
What you'd be better off doing is calling the query against a Model that extends the Illuminate Eloquent Model class.
In this instance, it COULD be something like this
CableRecommenderView::where('recommend_view.id', $ct)
->where('recommend_view.size_mm', $cs)
->first();
Eloquent is able to respond accordingly when being passed to the response object.
As an FYI, when using equals ('=') as the comparison operator in queries Eloquent (and possibly the query builder, although you'd have to check on that), you can forego explicitly defining it in the ->where() clause and simply pass the comparison variable as the second argument.
Hope that helps!
The ajax call expects a response to be passed back to it. It is unable to recognize the object you are trying to pass to it.
Look into creating responses http://laravel.com/docs/5.1/responses.
If you return a json response e.g
public function itemDetails($ct, $cs)
{
$item = CS::getDetails($ct, $s);
return response()->json(['data' => $item]);
}
The ajax call should work. Then you can retrieve from the data variable from your ajax.

pagination and factory in laravel4

Consider this code taken from here.
public function getIndex()
{
$posts = Post::orderBy('id','desc')->paginate(10);
// For Laravel 4.2 use getFactory() instead of getEnvironment() method.
$posts->getEnvironment()->setViewName('pagination::simple');
$this->layout->title = 'Home Page | Laravel 4 Blog';
$this->layout->main = View::make('home')->nest('content','index',compact('posts'));
}
As I understand it, pagination limits the number of rows, so I think paginate(10) means select first ten rows in the database. But I absolutely don't understand this.
// For Laravel 4.2 use getFactory() instead of getEnvironment() method.
$posts->getEnvironment()->setViewName('pagination::simple');
or
$posts->getFactory()->setViewName('pagination::simple');
And everything below. Mainly I don't understand what factory means and how it relates to pagination. I went to the laravel docs on Illuminate\Pagination\Factory and Illuminate\View\View but I can't find the meaning of factory. Can anyone explain the code above?
You are essentially setting how the pagination is output in HTML by selecting a specific paginator view, this allows you to have more than one type in an application or use different to the default.
Using multiple pagination types in the same application
Sometimes, you may want to use different pagination types across your
application. By default, Laravel will use the type specified in your
app/config/view.php file, so you need to override this setting when
you wish to use another type. Here is how to do so.
// This code should be in a controller or a route Closure.
// Let’s use the good old example of a list of blog posts.
$articles = Article::paginate(5);
Paginator::setViewName('pagination::simple');
/*
Alternatively, you could also use this to achieve the same result:
$articles->getEnvironment()->setViewName('pagination::simple');
For those who would like to know what’s happening under the hood, here is a more
detailed explanation:
1. Calling paginate() on an Eloquent model or a query builder will return an
instance of \Illuminate\Pagination\Paginator
2. Then, we need to get the related \Illuminate\Pagination\Environment of this
paginator via the well-named getEnvironment() method.
3. Finally, we can specify the pagination type we need. The default value is
'pagination::slider'.
The pagination types that are available by default are located in the
vendor/laravel/framework/src/Illuminate/Pagination/views directory.
*/
Source: http://laravel-tricks.com/tricks/using-multiple-pagination-types-in-the-same-application

Call to undefined method Illuminate\Pagination\Paginator::make()

Hi a Laravel beginner here, I have a a manual select query which retrieve the data correctly via DB::select.
Now I want to paginate the result, however manual pagination doesn't work
$pagination = Paginator::make($book, count($book), 5);
and returning the following error
Call to undefined method Illuminate\Pagination\Paginator::make()
I am using Laravel 4.2
Please help
Thanks
You are using the wrong Paginator class. You should change your import to:
use Illuminate\Support\Facades\Paginator;
Or remove the use statement completely since there's an alias registered for just Paginator

Categories