Increase variable in column name Laravel - php

I have those columns in my database table :
value_day_1 | value_day_2| value_day_3 |......|value_day_36
I'm trying to display each value in a view using a for loop
#for ($n=1;$n<37;n++)
{{ $day->value_day_? }}
#endfor
How can i replace the ? by $n ?

One solution would be
#foreach(range(1,37) as $n)
#php($column = 'value_day_' . $n;)
{{ $day->$column }}
#endforeach
I prefer to use range instead of the for syntax but it is not neccessary for your problem

#for ($n=1;$n<37;n++)
$d='value_day_'.$n;
{{ $day->$d }}
#endfor
Just assign to a new variable before

You can do this easily inline:
$day->{'value_day_'. $n}

Related

Translating a DB variable on a laravel Blade

I'm trying to display a translated word on the blade in a laravel application.
my language variable is "texts"
and I have the following on my blade
#foreach($permission as $value)
<li><label>{{ Form::checkbox('permission[]', $value->id, false, array('class' => 'name')) }}
{{ ('$value->name') }}</label></li>
<br/>
#endforeach
I'm trying to translate this
{{ ('$value->name') }}
This should give a result like, user-edit, user-view...
In my language file, I have the translated texts for those outputs.
I've tried this on my blade
{{ __('texts.$value->name') }}
But it just only printing
texts.$value->name
What is the correct way of translating this,
{{ ('$value->name') }}
You are using single quotes in {{ __('texts.$value->name') }}. Variables don't expand inside single quotes. Concatenate the two strings instead:
{ __('texts.' . $value->name) }}

Start retrieved records at 0 MySQL Laravel

I have a forum I'm building and replies are always incrementing with their unique id's.
This means that I can have a thread and have the reply number at 550 when the thread starts, not displaying the correct reply number in relation to the thread.
Is there anyway that when I retrieve these records, I can reset them to 1 and have them increment respective to their threads?
Thanks for any help.
Here is what I have so far:
#for($i = 0; $i < $thread->replies->count(); $i++)
Post {{ $i }} of {{ $thread->replies->count() }}
#endfor
It seems to work except this is stuck in my $replies as $reply loop and it proliferates all records.
I would use a #foreach loop and the $loop variable to do the counting, See the $loop doc
#foreach($thread->replies as $post)
Post {{ $loop->iteration }} of {{ $loop->count }}
#endfor
In the case of spanning multiple pages, you should implement Pagination instead See the Pagination doc
{{ ($replies->currentPage()-1) * $replies->perPage() + $loop->index + 1 }} of {{ $replies->total() }}
Found here: Laravel - Use $loop->iteration with pagination

print comma separated value from database

I have a table column which is separate with comma. Column name is 'Intro' and the values are:
Rumi, school,2018,lecturer
I need to show this comma separated value one by one. The output should be like this :
Rumi
I tried this code in my blade file :
#foreach ($evaluation as $client)
#foreach (explode(',', $client->Intro) as $client_intro)
{{ $client_intro[0]}}
#endforeach
#endforeach
but this shows nothing. what is the problem ?
Just simply remove the index
{{ $client_intro[0]}} ---->> should be --> {{ $client_intro}}
Try This:
#foreach ($evaluation as $client)
#foreach (explode(',', $client->Intro) as $client_intro)
{{ $client_intro }}
#endforeach
#endforeach

Combine multiple column for to do a foreach

I have 5 columns in my database. Here are the names of the columns:
promo1
promo2
promo3
promo4
promo5
I would like to combine these columns to be able to make a foreach in my view.
I tried this:
$promos = Building::select('promo1', 'promo2', 'promo3', 'promo4', 'promo5')->where('id', $card->id)->get()->toArray();
and in my view :
#foreach($promos as $promo)
<span>{{ $promo['promo1'] }}</span>
#endforeach
But I have only one result for my foreach. Instead of 5. And I can only select one by one ($ promo ['promo1'], $ promo ['promo2']) so doing a foreach would not help
Is there a way to do that? thank you very much
When you're using get() method, you're getting a collection. Use the find() method to get an object instead of collection:
$promos = Building::select('promo1', 'promo2', 'promo3', 'promo4', 'promo5')->find($card->id)->toArray();
Then you'll be able to iterate over promos:
#foreach ($promos as $promo)
<span>{{ $promo }}</span>
#endforeach
First get the building object
$building = Building::select('promo1', 'promo2', 'promo3', 'promo4', 'promo5')->findOrFail($card->id);
then loop through the promos
#foreach ($building->getAttributes() as $key => $value)
<span>{{ $key }} : {{ $value }}</span>
#endforeach
note: using the $key here is optional

Get selected value in form, seletbox

I have a problem and I can't resolve it, please help me. So I have my form :
{{ Form::open(array('url'=>'/administration/student/addMarks','method' => 'post')) }}
#foreach($aObjectsInGroupe as $object)
{{ Form::hidden('id_object[]',$object->id) }}
{{ Form::label($object->name) }}
{{ Form::select('note[]', $aMarks, null, array('class'=>'form-control')) }}
<br />
#endforeach
{{ Form::hidden('id',$aOneStudent['id']) }}
{{ Form::submit('Add mark',array('class'=>'btn btn-primary')) }}
{{ Form::close() }}
In my StudentController I have a method for get the mark from student_id and object_id:
public function getMarkByStudentAndObject($nIdStudent, $nIdObject){
$aMark = \Mark::where('student_id', '=', $nIdStudent)
->and('object_id', $nIdObject)
->get()
->toArray();
}
$aMarsks it's a table :
$aMarks = array(
'0'=>'0',
'1'=>'1',
'2'=>'2',
'3'=>'3',
'4'=>'4',
'5'=>'5',
'6'=>'6',
'7'=>'7',
'8'=>'8',
'9'=>'9',
'10'=>'10',
);
It's possible to call the method getMarkByStudentAndObject in :
{{ Form::select('note[]', $aMarks, null, array('class'=>'form-control')) }}
to get the selected value?
Help me please. Thx in advance.
maybe you need to do this:
<select name="note[]" class="form-control">
#foreach($aMarks as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
#endforeach
</select>
You should also check your Eloquent query, instead of ->and use another ->where
Try the lists() function of the Query Builder
\Mark::where('student_id', '=', $nIdStudent)
->and('object_id', $nIdObject)
->lists('column_1', 'column_2');
You then get the values of column_1 as the keys of the array and column_2 as the values.
http://laravel.com/docs/4.2/queries#selects
You should be able to directly call this from the Form::select. A call to
Mark::getMarkByStudentAndObject($aOneStudent['id'], $object->id)->note
would give you the Mark object and then you would need to get the note column which stores the value in the Mark object. Resulting in a call like this:
{{
Form::select(
'note[]',
$aMarks,
Mark::getMarkByStudentAndObject(
$aOneStudent['id'],
$object->id
)->note, array('class'=>'form-control')
)
}}
At the moment I can't verify if this is working because I have no possibility to test it but it should work as Blade is just a wrapper for PHP calls.

Categories