How to make Laravel 4 pagination - php

Hello everyone I'm trying to make pagination in Laravel 4 but my code doesn't work.
I have controller with action:
public function getSingleProduct($prodName, $id)
{
$singleProduct = Product::getOne($id);
$getAllReviews = Review::getAllBelongsToProduct($id);
$this->layout->content = View::make('products.single')
->with('reviews', $getAllReviews)
->with('products', $singleProduct);
}
and I want to paginate getAllReviews (5 per page). I tried like this:
$getAllReviews = Review::getAllBelongsToProduct($id)->paginate(5); but it doesn't work for me. Here is also my Review model
public static function getAllBelongsToProduct($id) {
return self::where('product_id', '=', $id)
->join('products', 'reviews.product_id', '=', 'products.id')
->select('reviews.*', 'products.photo')
->orderBy('created_at', 'desc')
->get();
}
Where I have a mistake?

Instead of that static method on your model use query scope, this will be flexible:
// Review model
public function scopeForProduct($query, $id)
{
$query->where('product_id', $id);
}
public function scopeWithProductPhoto($query)
{
$query->join('products', 'reviews.product_id', '=', 'products.id')
->select('reviews.*', 'products.photo');
}
Then use it:
// get all
$reviews = Review::forProduct($id)->withProductPhoto()->latest()->get();
// get paginated
$reviews = Review::forProduct($id)->withProductPhoto()->latest()->paginate(5);
latest is built-in method for orderBy('created_at', 'desc').
If you want to have just a single call in your controller, then chain the above and wrap it in methods on your model.

Related

how to use Join with Where condition in laravel controller?

i have this index function that will show data from two different tables:
public function index()
{
$complaints = DB::table('complaint')
->select(['complaint.id','complaint.createdDate','complaint.user_id','complaint.createdDate','complaint.complaint_title','tbl_users.phone','tbl_users.email'])
->join('tbl_users', 'complaint.user_id', '=', 'tbl_users.id')
->get();
return view('admin.complaints',compact('complaints'));
}
and in the next function i want to show a single row using the same thing above by 'id'
i tired this:
public function show($id)
{
$complaints = DB::table('complaint')
->select(['complaint.id','complaint.createdDate','complaint.user_id','complaint.createdDate','complaint.complaint_title','tbl_users.phone','tbl_users.email'])
->join('tbl_users', 'complaint.user_id', '=', 'tbl_users.id')
->where('id', $id)->first()
->get();
return $complaints;
}
but i'm getting this error
Call to undefined method stdClass::get()
For creating where statements, you can use get() and first() methods. The first() method will return only one record, while the get() method will return an array of records , so you should delete first() , so the code should be like that .
public function show($id)
{
$complaints = DB::table('complaint')
->select(['complaint.id','complaint.createdDate','complaint.user_id','complaint.createdDate','complaint.complaint_title','tbl_users.phone','tbl_users.email'])
->join('tbl_users', 'complaint.user_id', '=', 'tbl_users.id')
->where('id', $id)
->get();
return $complaints;
}

custom query function laravel

I often need to perform this query:
$Op = JobCardOp::where([
['JobCardNum', '=', $JobCardNum ],
['OpNum', '=', $OpNum ]
])->first();
So rather than writing this out every time I want a function like:
public function getOp($JobCardNum, $OpNum)
{
$Op = JobCardOp::where([
['JobCardNum', '=', $JobCardNum ],
['OpNum', '=', $OpNum ]
])->first();
return $Op;
}
That I can call in my controller. Where should I define my function, at the moment the I only need it in one controller but I may need it an another if thats possible. Any help appreciated.
You may define your function in JobCardOpt model as static:
public static function getOp($JobCardNum, $OpNum)
{
$Op = static::where([
['JobCardNum', '=', $JobCardNum],
['OpNum', '=', $OpNum]
])->first();
return $Op;
}
And use it like this in your controllers:
$jobCardOpt = JobCardOpt::getOp(1, 2);
You could put this method on your Model if you wanted to as a static function.
public static function getOp($cardNum, $opNum)
{
return static::where([
['JobCardNum', '=', $cardNum],
['OpNum', '=', $opNum]
])->first();
}
// controller
$res = YourModel::getOp($cardNum, $opNum);
Or add a query scope to the model
public function scopeGetOp($query, $cardNum, $opNum)
{
return $query->where([
['JobCardNum', '=', $cardNum],
['OpNum', '=', $opNum]
]);
}
// controller
$res = YourModel::with(...)->getOp($cardNum, $opNum)->first();
Kinda depends how you want to use it.

Passing parameters from Controller to Model in Laravel 5

I am having trouble passing parameters from my Controller to my Model in Laravel 5.
My Model:
class Widget extends Model {
protected $fillable = array('type');
public function widget_fields_with_data($id)
{
return DB::table('widget_fields')
->join('banner_data', function($join) {
$join->on('banner_data.widget_field_id', '=', 'widget_fields.id')
->where('banner_data.banner_id', '=', $id);
})
->select('widget_fields.*', 'banner_data.value');
}}
In My Controller
$widget->widget_fields_with_data('54')->get();
This seems to return an Undefined variable: id error and I can't figure out why.
If i hardcode the value in the Model everything works okay.
Use use() statement:
function($join) use($id)
Here is your answer. You haven't passed the $id in inner function.
public function widget_fields_with_data($id)
{
return DB::table('widget_fields')
->join('banner_data', function($join) use($id) { // pass $id here
$join->on('banner_data.widget_field_id', '=', 'widget_fields.id')
->where('banner_data.banner_id', '=', $id);
})
->select('widget_fields.*', 'banner_data.value');
}}

Laravel post data to function within the controller

Currently my HomeController looks like this:
class HomeController extends BaseController {
public function getHome()
{
$scripts = Script::select('script.*', DB::raw('COALESCE(SUM(vote.rating), 0) as rating'))
->leftJoin('script_vote as vote', 'vote.script_id', '=', 'script.id')
->with('tags')
->orderBy('rating', 'desc')
->orderBy('views', 'desc')
->groupBy('id')
->paginate(8);
return View::make('home')->with('scripts', $scripts);
}
public function postSearch()
{
$input = array(
'query' => Input::get('query'),
'sort_col' => Input::get('sort_col'),
'sort_dir' => Input::get('sort_dir'),
);
$scripts = Script::select('script.*', DB::raw('COALESCE(SUM(vote.rating), 0) as rating'))
->leftJoin('script_vote as vote', 'vote.script_id', '=', 'script.id')
->where('title', 'LIKE', '%' . $input['query'] . '%')
->orderBy($input['sort_col'], $input['sort_dir'])
->orderBy('views', 'desc')
->groupBy('id')
->with('tags')
->paginate(8);
Input::flash();
return View::make('home')->with('scripts', $scripts);
}
}
As you can see, I'm using (almost) the same big query twice. I would like to call the postSearch() function within the getHome() function and give the three parameters (query = '', sort_col = 'rating', sort_dir = 'desc') with it. Is this possible?
If you plan on using this frequently I would move this out of your controller and put it in your Model as a Custom Query Scope. This really doesn't have a place in the Controller even as a private function.
public function scopeRating($query)
{
return $query->select('script.*', DB::raw('COALESCE(SUM(vote.rating), 0) as rating'))
->leftJoin('script_vote as vote', 'vote.script_id', '=', 'script.id')
->with('tags')
->orderBy('rating', 'desc')
->orderBy('views', 'desc')
->groupBy('id');
}
This could then be called like this
Script::rating();
here are a few possibilities:
write a private function getScripts(...) within the controller (not so sexy)
add a getScripts(...) function on your Scripts model (so-lala sexy)
create a service provider to encapsulate the model(s) and inject them into the controller

Laravel 4 model controller view

how can i use this in the model
now example 1 this work just fine... on the controller
$songs = DB::table('songlist')
->join('historylist', 'songlist.id', '=', 'historylist.songID')
->select('songlist.*', 'historylist.*')
->orderBy('historylist.date_played', 'DESC')
->first();
return View::make('currentsong')
->with('songs', $songs);
but i want to get that from the model my model is
class Radio extends Eloquent
{
public static $timestamps = true;
}
i want my model to look like
class Radio extends Eloquent
{
public static function getSonginfo()
{
$songs = DB::table('songlist')
->join('historylist', 'songlist.id', '=', 'historylist.songID')
->select('songlist.*', 'historylist.*')
->orderBy('historylist.date_played', 'DESC')
->first();
}
}
now how can i pass this model into my controller and view and call the varible as i do right now $songs->title on my view
the part i dont really get is how can i call the model function on the controller and parse it into the view something like this maybe
$songs = Radio::getSonginfo();
return View::make('currentsong')->with('songs', $songs);
now when i call this on my view {{ $songs->title}} i get undefined variable $songs.
any help Thanks.
sorry for my rusty english.
you need to return the variable in your model.
public static function getSonginfo()
{
$songs = DB::table('songlist')
->join('historylist', 'songlist.id', '=', 'historylist.songID')
->select('songlist.*', 'historylist.*')
->orderBy('historylist.date_played', 'DESC')
->first();
return $songs;
}

Categories