The table distributor_requisition_items has the columns :requisition_id,product_id ,measuring_unit_id,quantity
In the model DistributorRequisition.php, I have:
class DistributorRequisition extends Model
{
public function items()
{
return $this->belongsToMany('App\Product','distributor_requisition_items',
'requisition_id','product_id')->withPivot('measuring_unit_id','quantity');
}
}
In the controller I have :
class CurrentInventoryController extends Controller
{
public function distributor_inventory(Request $request)
{
$distributor_requisition=DistributorRequisition::get();
$distributor_requisitions_all= $distributor_requisition->items;
// this line produces an error
return view('admin.current_inventory.distributor-inventory',
compact('distributor_requisitions_all','stores',.....));
}
}
In the distributor-inventory.blade.php, I have :
#foreach($distributor_requisitions_all as $aP)
{{$aP->items->measuring_unit_id}}
#endforeach
I get the following error :
Exception in Collection.php line 1527: Property [items] does not exist on this collection instance.
in Collection.php line 1527 at Collection->__get('items') in CurrentInventoryController.php line 64
And the line 64 in the controller has :
$distributor_requisitions_all= $distributor_requisition->items;
So how to use the eloquent model method items in the controller so that I can access the distributor_requisitions_all variable in the view to extract values from it ?
use with() method instead of get().
public function distributor_inventory(Request $request)
{
$distributor_requisition=DistributorRequisition::with('items');
return view('admin.current_inventory.distributor-inventory', ['distributor_requisitions_all' => $distributor_requisition]);
}
$distributor_requisition=DistributorRequisition::get(); is a collection of Model DistributorRequisition, while items is a method inside the model that is why it is giving you an error .. you can't directly call a Model method from a collection .. and to solve your issue what you have to do is
in your function
public function distributor_inventory(Request $request)
{
$distributor_requisition=DistributorRequisition::get();
return view('admin.current_inventory.distributor-inventory',
compact('distributor_requisition','stores',.....));
}
and in your view
#foreach($distributor_requisition as $aP)
// {{ $aP->items->measuring_unit_id }}
// that will give you error since $aP->items is another collection of many model item what you can do is add another loop
#foreach($aP->items as $item)
$item->pivot->measuring_unit_id }}
#endforeach
#endforeach
Related
I am using Laravel/Passport and already can get data by id:
api.php/Controller.php
Route::get('members/{member}', 'MemberController#show');
public function show(Member $member)
{
return $member;
}
This just return all data by id, now I want to get member by phone number, so I created:
Route::get('members/{phone}', 'MemberController#phone');
public function phone(Member $member)
{
return $member;
}
Error:
exception:
"Symfony\Component\HttpKernel\Exception\NotFoundHttpException" file:
"C:\Users\xx\xx\xx\xx\vendor\laravel\framework\src\Illuminate\Foundation\Exceptions\Handler.php"
line: 204 message: "No query results for model [App\Member]
2929292222"
route
Route::get('members/{phone}', 'MemberController#phone');
controller
public function phone(Member $member)
{
return $member;
}
Go to Member.php model
and add this method getRouteKeyName
public function getRouteKeyName()
{
return 'phone';
}
Laravel by default it will automatically inject the model instance that has an ID matching the corresponding value from the request URI.
If you would like model binding to use a database column other than id when retrieving a given model class, you may override the getRouteKeyName method on the Eloquent model:
for more information check the documentation
Student.php
It has many fees
class student extends Model{
public function fees($round){
return $this->hasMany(fee::class)->where('payment_round','=',$round);
}
}
Fee.blade.php
#foreach($student->fees("parameter") as $fee)
{{$fee->myproperty}}
#endforeach
How I can pass a parameter to $student->fees("parameter")?
You can keep relationship as simple as possible and add a second method which uses that to get the data you want :
<?php
class student extends Model{
public function fees(){
return $this->hasMany(fee::class);
}
public function getFeeByRound($round)
{
return $this->fees()->where('payment_round','=',$round)->get();
}
}
And then you can use this as :
#foreach($student->getFeeByRound($parameter) as $fee)
{{ $fee->myproperty }}
#endforeach
Try something like that:
#foreach($student->fees($parameter)->get() as $fee)
{{ $fee->myproperty }}
#endforeach
I think you had confused the collection of related models fees with the query constructor fees().
I have the following table structure in my database:
products
id
product_formats
id
product_id
product_prices
id
product_format_id
When I'm trying to do $this->format->product; inside my ProductPrice class, I get an error:
LogicException: App\ProductPrice::product must return a relationship instance.
When I perform a dd inside my function:
dd($this->format()->first()->product);
I get the instance of the product. However, removing the dd would still throw the exception.
Why am I getting the LogicException?
ProductPrice
class ProductPrice extends Model
{
public function format()
{
return $this->belongsTo(ProductFormat::class, 'product_format_id');
}
public function product()
{
return $this->format->product;
}
}
ProductFormat
class ProductFormat extends Model
{
public function product()
{
return $this->belongsTo(Product::class);
}
}
Update
The result of dd($this->format); returns an instance of ProductFormat.
After investigating the HasAttributes::getRelationshipFromMethod(), I've noticed that if the property does not exist in the properties attribute of the class, it will try to retrieve a relation, hence the error.
To fix it, I had to add the following to my class:
protected $attributes = ['product'];
Otherwise, I could call product as a function instead of attribute:
$price->product();
Hey i have three table like this
--table plan--
id
name
....
----table letter---
id
plan_id
....
---table person----
id
plan_id
name
.....
Model i have :
---Model plan---
class plan extends Model
{
protected $table = 'plan';
public function letter(){
return $this->hasOne('App\letter');
}
public function person(){
return $this->hasMany('App\person');
}
}
--Model person--
class person extends Model
{
public function plan(){
return $this->belongsTo('App\plan');
}
}
--Model letter--
class letter extends Model
{
public function plan(){
return $this->belongsTo('App\plan');
}
}
And in controller i write code like this :
$letter = letter::find($id) // $id from url parameter and it's work
return view('letter',['letter' => $letter]);
Nah in view i wanna acces person name from letter model as distinct , so i write code like this
{{ #foreach ($letter->plan()->person()->groupBy('name')->get) as $person }}
but it return error like this :
Call to undefined method Illuminate\Database\Query\Builder::person()
Where is my mistake(s)?
There is a difference between $letter->plan() and $letter->plan. If you call it like a method, Laravel will return the Query Builder. If you call it like an attribute Laravel will return the model from that relation.
So you're trying to call your model on the Query Builder, which is a method that doesn't exists and creates the error. This will fix your problem:
$letter->plan->person()->groupBy('name')->get()
In your controller you can do:
$letter = letter::find($id) // $id from url parameter and it's work
$persons = $letter->plan->person()->groupBy('name')->get();
return view('letter', compact('letter', 'persons'));
And in your view:
#foreach($persons as $person)
Confused about why this isn't working, trying to pass a database query function to my controller. I am receiving the error Call to a member function getCompanyData() on a non-object
Review.php (Model)
class Review extends Eloquent {
public function getCompanyData($company)
{
return $this->select('head', 'body', 'logo', 'name')
->where('company', '=', $company)
->firstOrFail();
}
}
ReviewController.php
class ReviewController extends BaseController {
protected $review;
public function __construct(Review $review)
{
$this->beforeFilter('csrf', array('on' => 'post'));
$this->review = $review;
}
public function show($company)
{
$data = $this->review->getCompanyData($company);
return View::make('reviews.show', compact('data'));
}
}
I'm not sure about the problem but since you mentioned you want be able to call Review::method() but don't want to declare the method as static, so, in this case, it's possible to call using static :: syntax a non-static method declared in an Eloquent Model in Laravel using scope like this:
class Review extends Eloquent {
public function scopeGetCompanyData($query, $company)
{
return $query->select('head', 'body', 'logo', 'name')
->where('company', '=', $company);
}
}
Now you can call the getCompanyData method from your controller like this way:
$data = Review::getCompanyData($company)->firstOrFail();
So review is not an object, and you are trying to call its method like it is object. It is probably array. I see you provided Request $request reference and you expect it to be and object. It sometimes happens to me while passing values to view. Try to track type of review.
Update:
Some other things to consider are: Why did you pass Review through constructor, when it is model that extends Eloquent and it should be visible ? Does Review really extends Eloquent ?