Laravel Monthly Wise Data Access - laravel

Hi I want to show my database data as monthly wise so I create a query with groupBy() function.
When I try
#foreach($report as $date=>$data)
{{$date}}
#endforeach
I got the month name
March
May
But when I want to get data as monthly wise I try
#foreach($report as$data)
{{$data->fiscal_year}}
#endforeach
I got
Property [fiscal_year] does not exist on this collection instance. (View: C:\xampp\htdocs\report-management\resources\views\adcr\report\rent_certificate.blade.php)
I don't understand how can I access the data in monthly wise
This is my controller
public function RentCertificate(){
$data['report'] = Report::get()
->groupBy(function($date) {
return Carbon::parse($date->created_at)->format('Y-m');
});
return view('adcr.report.rent_certificate', $data);
}

Related

Problem in retrieving data from different tables in laravel 6

I am retrieving data from 3 tables account, product and stock. Account and product has one to many relation and product and stock has one to many relation, I am trying to retrieve data using account table, here is my query:
$account=Account::find(1);
$result=$account->product->stock;
echo($result);
I am getting result but problem is that I need some specific columns from stock table and when I write following code I am getting error property does not exist:
$result=$account->product->stock->model;
//in controller first get account data
$account=Account::find(1);
$products=$account->product;
foreach($products as $product){
$productData=$product->stock;
}
//in account model
use App\Product;//model name
function product(){
return $this->hasMany(Product::class, 'account_id');
}
//in product model
use App\Stock //model name
function stock(){
return $this->hasMany(Stock::class, 'product_id');
}

ManytoMany relations in Laravel, retrieve data from related tables and display in blade

I have two tables related by many to many relation in Laravel Framework. I can display data from each table separately, but not through relation by taking one record from the 1st table and checking related records in the 2nd table. In tinker it accesses data fine.
Relations:
public function underperformances() {
return $this->belongsToMany(Underperformance::Class);
}
...
public function procedures() {
return $this->belongsToMany(Procedure::class);
}
My resource controller part:
...
use App\Underperformance;
use App\Procedure;
...
public function index()
{
$books = Underperformance::orderBy('id','desc')->paginate(9);
$procedures = Procedure::all();
return view('underpcon.underps', compact('books', 'procedures'));
}
Route:
Route::get('/underps', 'UnderpsController#index');
If I try to display data like this:
#foreach($procedures as $procedure)
<li>{{$procedure->underperformances}}</li>
#endforeach
I get such format to the browser:
[{"id":1,"title":"Spare part not taken before service","description":"tekstas","level":"1","costs":600 ...
This is correct data from related table, but I cannot select further the specific column from that table. For example this does not work:
#foreach($procedures as $procedure)
<li>{{$procedure->underperformances->id}}</li>
#endforeach
Nor this one:
#foreach ($procedures->underperformances as $underperformance)
<li>{{$underperformance->id}}</li>
#endforeach
How do I select records of the related table and display specific data from that table?
What would be a conventional way to do this?
#foreach($procedures as $procedure)
<li>{{$procedure->underperformances->id}}</li>
#endforeach
^ This right there $procedure->underperformances will return a collection, not a single item, so you need to treat it as array, you will not be able to access the id directly, you can either #foreach that, or use the pluck method in Laravel Collections.

How to retrieve data with composite primary key in Laravel

I am working with four tables in Laravel and trying to display data to a view. I believe I am stuck because one of the tables has a composite primary key.
I have the following in my controller:
public function show($id)
{
//Get application for drug
$application = PharmaApplication::where('ApplNo', $id)->first();
//Return all products for application
$drugs = $application->products;
return view('profiles.drug', compact('drugs'));
}
I have the following in my PharmaApplication model:
public function products()
{
return $this->hasMany('App\PharmaProduct', 'ApplNo', 'ApplNo');
}
I have the following in my view (which I cannot complete)
#foreach ($drugs as $drug)
<li>{{$drug->Strength}}</li>
<li>{{$drug->Form}}</li>
<li>{{$drug->Form}}</li>
#endforeach
I am trying to accomplish the following:
Get the application for a drug - this part of my code works
Return an array of objects for the products (i.e. 7 products that belong to one application). I can do this but get stuck going to the next part.
Next, I have to use the array of objects and search a table with the following columns: MarketingStatusID, ApplNo, ProductNo. I know how to query this table and get one row using DB Query, but then how do I get the proper result to that query within the for loop in my view?
Finally, I use the MarketingStatusID to retrieve the MarketingStatusDescription which I will know how to do.

is there a way to query and map datas with their correspondance in laravel

How to map data from one table to may other data from other table;
example we have three table region, market, categories, products using eloquent in laravel 5.1
i have tried this but only gives me the last
public function look(Request $request)
{
``$sector_id=$request->input('category');
$cells=Cell::where('sector_id',"=",$sector_id)->get();
foreach ($cells as $cell){
$a=$cell->id;
$markets=Market::where('cell_id',"=",$a)->get();
}
foreach ($markets as $market){
$b=$market->id;
$prices=Price::where('market_id',"=",$b)->get();
}
return view('reports.sector')->with('cells',$cells)->with('markets',$markets)-`>with('prices',$prices);
}
i need to display only the names in above table but what i need is that one elements in each table maps with their corresponding elements from other table how can i do that query.i need all of those data from database please help me i am stuck here.
If you mean three separate tables:
$cells = Cell::where('sector_id', $sector_id)->get();
$markets = Market::whereIn('cell_id', $cells->pluck('id'))->get();
$prices = Price::whereIn('market_id', $markets->pluck('id'))->get();
If you want to display the data in a tree-like structure, and if you have defined your model relations properly you can use eager loading
$cells = Cell::where('sector_id', $sector_id)->with('markets.prices')->get()
Pass cells to the view and you can use the following in your blade template to display the data
#foreach ($cells as $cell)
#foreach ($cell->markets as $market)
#foreach ($market->prices as $price)

how to insert multiple data into a single column into the database

Am learning laravel and I encountered a problem saving data into my database from my form.
My instance
when a user tries to make a multiple purchase of products ie.when a user purchases more than one product,i wanted to save the names of products that belongs to the purchase user made into my 'PURCHASES' table having an 'ID' of '1'.
Names of product to be save;
1.productA
2.productB
3.productC
Codes
FORM IN MY VIEW
<input type='hidden' name='product_name'
#foreach($order as $order)
value='{{$order->product_name}}'
#endforeach >
MY purchase CONTROLLER
Saving the names;
$purchase = new Purchase;
$purchase->product_name = $posted['product_name'];
$purchase->save();
When i initiate the function i get an error exception reading 'Trying to get property of non-object' from my view from the line;
#foreach($order as $order)
value='{{$order- >product_name}}'
#endforeach >
How do i go about this problem?
There are a lot of strange things in your code unless I'm reading it wrong, but most likely when you're seeing that error in a view it's because you didn't pass that data into the view. Take this example:
Controller
public function showProducts() {
// assuming $order_array is a set of product IDs that is part of an order
$orders = array('products' => Products::whereIn('id', $order_array);
return View::make('your/view' compact('orders'));
}
I don't quite understand what you're trying to do but passing the object array 'orders' to the view allows you to then call your line:
#foreach($orders as $order)
code here
#endforeach

Resources