Foreach only return first value - laravel

Not sure why it is not getting all the value
In my view
<?php $sum_selfmark =0?>
#foreach
($criteria_criteriamarks as $criteria_criteriamark)
SelfMark:{{$criteria_criteriamark->selfmark}}
<?php $sum_selfmark+= $criteria_criteriamark->selfmark ?>
#endforeach
<p>Total:{{$sum_selfmark}}</p>
In my controller
public function go_to_self_marking($id){
$criteria=Criteria::find($id);
$criteria_criteriamarks =$criteria->criteriamarks;
return view('criterias/self-marking')
->with('criteria_criteriamarks',$criteria_criteriamarks);
}

You are trying to for each a 1 row. Because $criteria=Criteria::find($id); returns only 1 row.
If you want to for each then use $criteria=Criteria::all(); It will return all the row in your Criterias Table.

First and foremost, did you try to die and dump (dd) the $criteria->criteriamarks? So that we can check what's going on with your loop.
Seems like the #foreach loop has no issue.

Related

How to get Unique value in blade file laravel

I want to fetch unique value for filters of all products.
My db structure as follows
id Product category_id format attribute
1 demo1 5 HD test1
2 demmo2 4 SD test3
3 demmo3 4 HD test2
4 demmo4 3 HD test3
I want add filters of format and attribute in product page. But in that HD comming 3 times. I want to display that one only.
I am not getting how to display only single time.
Below is my controller code:
$item = Item::where('active_status', 1)->where('status', "1");
$data['item_count'] = $item;
$data['item'] = $item->paginate(20);
return view('frontend.pages.explore', compact('data'));
Below is blade file
<div class="filter-btn">
#foreach($data['item'] as $resolution)
<a class="btn btn-white-outline display-4" href="">{{array_unique($resolution->format)}}</a>
#endforeach
</div>
I am not getting how to display unique value only. Anyone have idea then let me know
since you are paginating your data, your "first page" might not have all the formats, so you have to do another query to your database:
$formats = DB::table('items')->select('format')->distinct()->get()
...
view(..., compact('data', 'formats'))
in the blade table:
#foreach($formats as $resolution)
<a class="btn btn-white-outline display-4" href="">{{$resolution->format}}</a>
#endforeach
If I am correct about your query, then you need to use groupby to list the items in your controller.
$items = Item::groupBy('format')->get();
The solution would be to create a sperate model for Formatand relationship between it and Product or whatever model that needs format , then fetch formats from its table and apply eager load .
this may look longer than your solution , but this is standards shoiuld be taken for vrious reasons :
less sql queries
more flexibility and options regarding our new table
better and less code to crud format
single source for change
less database data and faster load
...
Product.php
public function format(){
return $this->belongsTo(Format::class);
}
Format.php
public function products(){
return $this->hasMany(Product::class);
}
Usage
in controller
// get all formts eager loaded with products
$formats = Format::with('products')->get();
//get all products
$product = Format::latest()->paginate(20);
return view('frontend.pages.explore', compact( 'products' ,'formats'));
in your view
// all prodcuts
#foreach($formats as $format)
<a class="btn btn-white-outline display-4" href="">{{$format->name}}</a>
#endforeach
// in filter ( at clicking ) no additional query
#foreach($format->products as $product)
...
#endforeach

cannot use foreach more than one in view variable passed by controller

$std=Student::get();
return view(std, compact('std'));
#foreach($std as $std)
{Laravelp{$std->id}}
#endforeach
The above code work but the below code not work. If use the same variable in foreach more than one..?? can anyone tell me how can I use foreah more than one in view
$std=Student::get();
return view(std, compact('std'));
#foreach($std as $std)
{{$std->id}}
#endforeach
#foreach($std as $std)
{{$std->id}}
#endforeach
You're overwriting your collection with the loop variable. At the end of the loop, $std will be the last element from the original $std looped. change the variable name and it will work.
#foreach($std as $singleStd)
{{$singleStd->id}}
#endforeach
#foreach($std as $singleStd)
{{$singleStd->id}}
#endforeach

How to get array value that get from checkbox input in Laravel's controller?

I want to send 'Date' array from a page to another page through checkbox input for display the data and get more data from database.
I try to dd() the array from input, it's still normal but the data just only show 1 value when I use 'foreach' loop. How do I do?
<input name="isInvoices[]" type="checkbox" value="{{ $date }}">
$invoices = $request->input('isInvoices');
// dd($invoice); It's show array of date
foreach($invoices as $invoice) {
dd($invoice); //It's just only 1 value
}
I expected the output to show all value in the array, but the actual output is show 1 value.
dd means dump and die. Which after the first iteration stops, that's why you see only one item. Try this:
foreach($invoices as $invoice) {
dump($invoice);
}
die;
before such would function, you have to have series of checkbox like:
this is display series of date values on checkbox. so when you select any dates and submit
foreach($datevalue as $date){
<input name="isInvoices[]" type="checkbox" value="{{ $date }}">
}
//this gets the date value
$invoices = $request->input('isInvoices');
//this will display all the selected checkboxes
foreach($invoices as $invoice) {
dd($invoice);
}
dd() – stands for “Dump and Die”, and it means as soon as loop gets to this command, it will stop executing the program, in this case in first interation. To display all data in foreach loop, use echo or print_r():
foreach($invoices as $invoice) {
echo $invoice;
}
This means that the program will print $invoice for each iteration. Then you can access your data like this:
$invoice['value']
You can read more about different types of echoing here.

Can't use data in template's #foreach anymore

I've got a controller exposing object-data to a view:
$artifacts = Artifact::with('product');
return View::make('index', compact('artifacts'));
This used to work fine until last week. For some reasons now I can't use the data anymore in the template itself.
//this works
<?php
print_r($artifacts->first());
?>
//this doesnt
#foreach ($artifacts as $artifact)
<?php
print_r($artifact);
?>
#endforeach
when using eager loading, you need to get the actual resultset.
$artifacts = Artifact::with('product')->get();

model relationship and routes get model id

i have the following route:
Route::get('notes/main', function(){
$destinations = Destination::where('show', '=','1')->get();
$notes = Destination::find($destination->id)->notes()->get();
return View::make('notes.main')
->with('destinations', $destinations);
});
//the relationship models:
<?php
class Destination extends Eloquent {
public function notes()
{
return $this->has_many('Note');
}
}
<?php
class Note extends Eloquent
{
public function destination()
{
return $this->belongs_to('Destination');
}
}
//View:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
{ $notes->destination->text }} // this isn't echoed
#endforeach
what's the correct way to filter this and define $destination->id
thanks
How would i Filter the notes in an if statement inside the loop ?
#if (isset($note->title) != 'shorttext')
#else
<p> {{ $note->text }} </p>
#endif
#endforeach
You use $destination->id in your Route but it seems to be not defined yet. The question is, what do you want to achieve with your code?
With:
$destinations = Destination::where('show', '=','1')->get();
$notes = Destination::find($destination->id)->notes()->get();
you are getting only the Notes of one specific destination (so far, $destination is not defined. You could use Destination::all()->first() to get the first, or Destination::find(id), with ID being replaced with the primary key value of the destination you need).
But I guess you don't want it that way. From your Output it seems like you want to have an output with each destination and below each destination the corresponding Notes.
Controller:
//gets all the destinations
$destinations = Destination::where('show', '=','1')->get();
return View::make('notes.main')
->with('destinations', $destinations);
View:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
#foreach($destination->notes as $note)
{{ $note->text }} <br>
#endforeach
<hr>
#endforeach
Didn't test this, but this way your View would show you all your Destinations and for each Destination all the Notes.
In your route, your not sending $notes variable to the view.
How I would do it:
$destinations = Destination::where('show', '=','1')->get();
$destinations->notes = Destination::find($destination->id)->notes()->get();
Then in view:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
{{ $destination->notes->text }}
#endforeach
So first you asked a question, I gave you a correct answer.
You marked my answer as accepted, but later on you edit your initial question to add another question (for which you should create a new thread instead).
Then you create your own answer which is only answering part of your initial question and mark that as the good solution?
I could tell you why your isset and empty did not work, but why should I if you don't value my help at all?
OK
for the last part this worked out at the end
function isset and empty didn't work , strange:
#if ($note->title == 'shorttext')

Resources