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');
}}
Related
When I am trying to query with relation in following way, I am getting error Trying to get property 'name' of non-object
$billing = ServiceProviderBilling::with(['user' => function ($query) {
$query->where('parent_id', auth()->id());
}])->get();
There is no error if I use
$billing = ServiceProviderBilling::with(['user'])->get();
In my view
#foreach($billing as $bill)
{{ $bill->user->name }}
#endforeach
Relations
public function serviceProviderBillings()
{
return $this->hasMany(ServiceProviderBilling::class);
}
and
public function user()
{
return $this->belongsTo(User::class);
}
How can I solve it?
It means $bill->user return null. some bill has no user, If you want show the bills without user, you can use whereHas method:
ServiceProviderBilling::with('user')
->whereHas('user', function (Builder $query) {
$query->where('parent_id', auth()->id());
})->get()
i need help for Laravel 4.2
the models are:
//1. model kelengkapan
class Kelengkapan extends Eloquent{
public function detilKelengkapan(){
return $this->hasMany('DetilKelengkapan', 'id_kelengkapan');
}
}
// 2. model DetilKelengkapan
class DetilKelengkapan extends Eloquent{
public function tDetilKelengkapanPaket(){
return $this->hasMany('TDetilKelengkapanPaket', 'id_detil_kelengkapan');
}
public function kelengkapan(){
return $this->belongsTo('Kelengkapan', 'id_kelengkapan');
}
}
// 3. model TDetilKelengkapanPaket
class TDetilKelengkapanPaket extends Eloquent{
public function detilKelengkapan(){
return $this->belongsTo('DetilKelengkapan', 'id_detil_kelengkapan');
}
}
the controller is:
$kelengkapan = Kelengkapan::with('detilKelengkapan.tDetilKelengkapanPaket')
->whereHas('detilKelengkapan.tDetilKelengkapanPaket', function($q) use ($id){
$q->where('id_paket', $paket);
})->get();
but the result has not filtering by "id_paket" but showed all data. thanks. (newbie)
Your Code:
$kelengkapan = Kelengkapan::with('detilKelengkapan.tDetilKelengkapanPaket')
->whereHas('detilKelengkapan.tDetilKelengkapanPaket', function($q) use ($id){
$q->where('id_paket', $paket);
})->get();
The correct Code:
$kelengkapan = Kelengkapan::with('detilKelengkapan.tDetilKelengkapanPaket')
->whereHas('detilKelengkapan.tDetilKelengkapanPaket', function($q) use ($id){
//the function should return the $q variable.
return $q->where('id_paket', $id);
})->get();
Explanation:
whereHas function has 2 compulsory arguments. A relationship function name, and a closure. The closure must return a query object so that filters can be chained.
Source: http://laravel.com/docs/4.2/eloquent#querying-relations
In there whereHas function you pass $id variable in the closure but you are using $packet inside.
$kelengkapan = Kelengkapan::with('detilKelengkapan.tDetilKelengkapanPaket')
->whereHas('detilKelengkapan.tDetilKelengkapanPaket', function($q) use ($id){
$q->where('id_paket', $id);
//---------------------^
})->get();
Try it and let me know the result.
How to search by title in the ServiceType only? There is also a title field in the Package which should be avoided
For example, in the Model:
class Package extends Eloquent {
protected $table = 'package';
function serviceType()
{
return $this->belongsTo('ServiceType');
}
public static function getPackagesByServiceType($service)
{
return Package::with('serviceType')->where('title', '=', $service);
}
}
Note:
There is a service_type_id field in the Package and id, title fields in the serviceType
in the controller:
$packages = Package::getPackagesByServiceType('something')->get();
No result appeared for some reason? It should search for something in the serviceType
It seem it wouldn't work to combine with() and where(). When I remove the where() and it work.
You can't use where() like that to filter by a related model. You should use whereHas() instead:
public static function getPackagesByServiceType($service)
{
return Package::with('serviceType')->whereHas('serviceType', function($q) use ($service){
$q->where('title', '=', $service);
});
}
Note if you don't need serviceType in the packages afterwards you don't have to eager load it, ergo you can remove the with('serviceType')
Also if you call get() in the controller you should use a query scope. It offers the same functionality but it's not a static function and it's the Laravel way
public function scopeByServiceType($query, $service){
return $query->with('serviceType')->whereHas('serviceType', function($q) use ($service){
$q->where('title', '=', $service);
});
}
And you use it like this:
$packages = Package::byServiceType('something')->get();
class Package extends Eloquent {
protected $table = 'package';
function serviceType()
{
return $this->belongsTo('ServiceType');
}
public static function getPackagesByServiceType($service)
{
return Package::with('serviceType')->where('title', '=', $service)->get();
}
}
You forgot the ->get();
The ->get() should be in the Model
public static function getPackagesByServiceType($service)
{
return Package::with('serviceType')->where('title', '=', $service)->get(); // here
}
and in the controller it should be like this:
$packages = Package::getPackagesByServiceType('something');
Hope that helps... I had similar issues in my Model - Controller structure....
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.
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;
}