Dynamic V-model name in a v-for loop Vue - laravel

I am developing an application and I am using Vue and laravel , inside a v-for loop I need error.id in the loop to be bound to the v-model name of the elements, this my code:
<tbody class="bodycol">
<tr v-for="error in errors.error" :key="error.id" >
<td>{{ error.client }}</td>
<td>{{ error.session }}</td>
<td>{{ error.info_session }}</td>
<td>{{ error.UPROC }}</td>
<td>{{ error.num_session }}</td>
<td>{{ error.num_exec }}</td>
<td>{{ error.date_debut }}</td>
<td>{{ error.heure_debut }}</td>
<td>{{ error.date_fin }}</td>
<td>{{ error.heure_fin }}</td>
<td>{{ error.status }}
<form #submit="addComment(error.id)" >
<div class="form-group">
<input v-model="form.comment[error.id]" type="text" name="comment"
class="form-control area" :class="{ 'is-invalid': form.errors.has('comment') }"
required>
<has-error :form="form" field="comment"></has-error>
</div>
</form>
</td>
</tr>
data(){
return{
comment:[],
}
}
the problem is the output of the errors.status is:
[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,"the comment is here"]

Change your data to this. You are referencing form.comment not comment
export default {
data() {
return {
form: {
comment: []
}
}
}
}

Related

Laravel how to display day and month only without year from date type?

I would wan to display month and data for payment_date only in a foreach loop in view.blade.php.
#foreach ($alert as $key=>$data)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $data->tenant_name}}</td>
<td>{{ $data->property_name}}</td>
<td>RM {{ $data->amount}}</td>
<td>{{ $data->payment_date }}</td>
<td><span class="badge bg-danger">{{ $data->status }}</span></td>
<td> <i class="bi bi-envelope-fill"></i>
</td>
</tr>
#endforeach
Hereby I include the datatype:
Short way (not recommended)
#foreach ($alert as $key=>$data)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $data->tenant_name}}</td>
<td>{{ $data->property_name}}</td>
<td>RM {{ $data->amount}}</td>
<td>{{ \Carbon\Carbon::createFromFormat('Y-m-d', $data->payment_date)->format('m-d') }}</td>
<td><span class="badge bg-danger">{{ $data->status }}</span></td>
<td> <i class="bi bi-envelope-fill"></i>
</td>
</tr>
#endforeach
Laravel way (Recommended)
Add cast to the eloquent model class.
protected $casts = [
'payment_date' => 'date',
];
after that you can do this
#foreach ($alert as $key=>$data)
<tr>
<td>{{ ++$key }}</td>
<td>{{ $data->tenant_name}}</td>
<td>{{ $data->property_name}}</td>
<td>RM {{ $data->amount}}</td>
<td>{{ $data->payment_date->format('m-d') }}</td>
<td><span class="badge bg-danger">{{ $data->status }}</span></td>
<td> <i class="bi bi-envelope-fill"></i>
</td>
</tr>
#endforeach

Laravel 5.8 - Invalid argument supplied for foreach()?

I tried to display data inside a table for admin.
Here is my admin.blade.php:
<form method="post" action="post_namaadmin">
<input type="text" name="txtnamaadmin" id="txtnamadmin" value="1">
<input type="submit" name="btnnamadmin" id="btnamaadmin">
</form>
<div class="card-body">
<table border="1">
#foreach($datalama as $rows)
<tr>
<td>ID : </td>
<td>{{ $rows->userid }}</td>
<td>Nama : </td>
<td>{{ $rows->nama }}</td>
</tr>
<tr>
<td>Section</td>
<td>IM</td>
<td>CO</td>
<td>SH</td>
<td>PL</td>
<td>RI</td>
<td>ME</td>
<td>TB</td>
<td>CF</td>
</tr>
<tr>
<!-- //1 -->
<td>{{ $rows->a1 }}</td>
<td>{{ $rows->b1 }}</td>
<td>{{ $rows->c1 }}</td>
<td>{{ $rows->d1 }}</td>
<td>{{ $rows->f1 }}</td>
<td>{{ $rows->g1 }}</td>
<td>{{ $rows->h1 }}</td>
<!-- //1 -->
<td>{{ $rows->a2 }}</td>
<td>{{ $rows->b2 }}</td>
<td>{{ $rows->c2 }}</td>
<td>{{ $rows->d2 }}</td>
<td>{{ $rows->f2 }}</td>
<td>{{ $rows->g2 }}</td>
<td>{{ $rows->h2 }}</td>
<!-- //3 -->
<td>{{ $rows->a3 }}</td>
<td>{{ $rows->b3 }}</td>
<td>{{ $rows->c3 }}</td>
<td>{{ $rows->d3 }}</td>
<td>{{ $rows->f3 }}</td>
<td>{{ $rows->g3 }}</td>
<td>{{ $rows->h3 }}</td>
<!-- //4 -->
<td>{{ $rows->a4 }}</td>
<td>{{ $rows->b4 }}</td>
<td>{{ $rows->c4 }}</td>
<td>{{ $rows->d4 }}</td>
<td>{{ $rows->f4 }}</td>
<td>{{ $rows->g4 }}</td>
<td>{{ $rows->h4 }}</td>
<!-- //5 -->
<td>{{ $rows->a5 }}</td>
<td>{{ $rows->b5 }}</td>
<td>{{ $rows->c5 }}</td>
<td>{{ $rows->d5 }}</td>
<td>{{ $rows->f5 }}</td>
<td>{{ $rows->g5 }}</td>
<td>{{ $rows->h5 }}</td>
<!-- //6 -->
<td>{{ $rows->a6 }}</td>
<td>{{ $rows->b6 }}</td>
<td>{{ $rows->c6 }}</td>
<td>{{ $rows->d6 }}</td>
<td>{{ $rows->f6 }}</td>
<td>{{ $rows->g6 }}</td>
<td>{{ $rows->h6 }}</td>
<!-- //7 -->
<td>{{ $rows->a7 }}</td>
<td>{{ $rows->b7 }}</td>
<td>{{ $rows->c7 }}</td>
<td>{{ $rows->d7 }}</td>
<td>{{ $rows->f7 }}</td>
<td>{{ $rows->g7 }}</td>
<td>{{ $rows->h7 }}</td>
</tr>
#endforeach
</table>
</div>
The code is not yet completed, but it should give a rough picture of the table.
Here is my route in web.php:
Route::post('/post_namaadmin', 'DataController#admin' ) ->name ('postnamaadmin');
Function in DataController.php :
public function admin(Request $request){
$mk = new data();
$param['datalama'] = $mk->ambilData($request->txtnamaadmin);
return view('admin',$param);
}
Function in data class :
public function ambilData($nama){
$query = data::orderby('userid')->where('nama','=' ,'$nama')->first();
return $query;
}
Here is the error:
> ErrorException {#230 ▼ #message: "Invalid argument supplied for
> foreach()" #code: 0 #file:
> "C:\xampp\htdocs\kuesioner1\storage\framework\views\5c1b0932d9bc5a402e110419f0ce7e166c7a2109.php"
> #line: 22 #severity: E_WARNING } 1
I'm new in Laravel. For why I use 5.8, my superior demand me so. All help and suggestion is highly appreciated.
When you try to dd($param['datalama']), you will get null, because your query found nothing, using ->where('nama','=' ,'$nama').
You need to change data class ambilData method
public function ambilData($nama){
return data::orderby('userid')
->where('nama', $nama)
->get();
}
I think you should put $param['datalama'] in the foreach instead
and change the view function to view('admin',compact('param'))
and in the form tag you need to change the action to
<form action="{{route('postnamaadmin')}}">

Delete function in view | Laravel

I have this function to delete only a row from a table in my database.
public function delete($id)
{
DB::table('user')->where('userID', '=', $id)->delete();
return redirect('userAdmin');
}
And I have a button which gets created for every row.
#foreach ($scores as $score)
<tr>
<td>{{ $score->id }}</td>
<td>{{ $score->serialnumber }}</td>
<td>{{ $score->name }}</td>
<td>{{ $score->created_at }}</td>
<td></td>
<td>
<button class="btn btn-danger" type="submit">Delete this Row</button>
{{ csrf_field() }}
</td>
</tr>
How do I get a delete functionality behind it?
In your view:
<form action="{{ route('yourmodel.delete', $score->id) }}" method="post">
{{ csrf_field() }}
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
Add a route with a name you like, and a parameter for the id. Let that route call the method in your example and you should be good.
There isn't really such a thing as a DELETE function in HTML forms, so you have to spoof it. Take a look at the docs.
In your view:
<form action="{{ route('yourmodel.delete', $score->id) }}" method="post">
{{ csrf_field() }}
#foreach ($scores as $score)
<tr>
<td>{{ $score->id }}</td>
<td>{{ $score->serialnumber }}</td>
<td>{{ $score->name }}</td>
<td>{{ $score->created_at }}</td>
<td></td>
<td>
<button class="btn btn-danger" type="submit">Delete this Row</button>
</td>
#endforeach
</form>
In your routes:
Route::post('Test/{id}', 'TestController#delete')->name('yourmodel.delete');

Laravel Resource controller not getting delete route on amchor click

In laravel, I have resource controller :
Route::resource('/student','StudentController');
here is my view listing code :
<tr>
<td>{{ $key + $models->firstItem() }}</td>
<td>{{ $model->name }}</td>
<td>{{ $model->email }}</td>
<td>{{ $model->roll_no }}</td>
<td>{{ $model->StudentDetail->phone }}</td>
<td>{{ $model->StudentDetail->course }}</td>
<td>{{ $model->StudentDetail->city }}</td>
<td>{{ $model->StudentDetail->state->state_name }}</td>
<td>
Edit |
Link
</td>
</tr>
conytroller function i have :
public function destroy($id)
{
echo "ok";
}
My problem is that when I click on anchor button(delete ) it not
found the resource route for the delete what I am doing wrong here can
anyone please help me elated this.
when I click on the delete anchor it calls the show function of resource but I want the destroy function to call on click.
destroy function use DELETE method
in your blade view
#foreach($students as $model)
<tr>
<td>{{ $key + $models->firstItem() }}</td>
<td>{{ $model->name }}</td>
<td>{{ $model->email }}</td>
<td>{{ $model->roll_no }}</td>
<td>{{ $model->StudentDetail->phone }}</td>
<td>{{ $model->StudentDetail->course }}</td>
<td>{{ $model->StudentDetail->city }}</td>
<td>{{ $model->StudentDetail->state->state_name }}</td>
<td>
Edit |
DELETE
<form id="delete_form" style="display:none" action="{{ route('student.destroy',$model->id)}}" method="POST" >
{{ csrf_field() }}
<input type="hidden" name="_method" value="delete" />
</form>
</td>
</tr>
#endforeach

Can't display blank table cells in laravel

I've got the following code which shows my clubs fixtures/results list for upcoming games:
<div class="row">
<div class="col-md-10 col-md-offset-1">
<span id="hide" class="first">1st XV Fixtures/Results</span><span id="show">2nd XV Fixtures/Results</span>
<table class="table table-bordered table-hover">
<tr>
<th>Date</th>
<th>Venue</th>
<th>Opposition</th>
<th>Kick Off</th>
<th>Type</th>
<th>Score</th>
<th>Result</th>
</tr>
#foreach ($fixtures as $fix)
#if($fix['team'] === 1)
<tr class="clickableRow success league_table" href="{{ action('DynamicPages#match',[$fix['id']]) }}">
<td>{{ date("d/m/Y",strtotime($fix['mdate'])) }}</td>
<td>{{ $fix['venue'] }}</td>
<td>{{ $fix['opposition'] }}</td>
<td>{{ date("H:i",strtotime($fix['mdate'])) }}</td>
<td>{{ $fix['type'] }}</td>
#foreach($results as $result)
#if($result['match_id']=== $fix['id'])
#if(!empty($result['score']))
<td>{{ $result['score'] }}</td>
<td>{{ $result['result'] }}</td>
#else
<td> </td>
<td> </td>
#endif
#endif
#endforeach
</tr>
#else
<tr class="clickableRow danger merit-table" style="display:none;" href="{{ action('DynamicPages#match',[$fix['id']]) }}">
<td>{{ date("d/m/Y",strtotime($fix['mdate'])) }}</td>
<td>{{ $fix['venue'] }}</td>
<td>{{ $fix['opposition'] }}</td>
<td>{{ date("H:i",strtotime($fix['mdate'])) }}</td>
<td>{{ $fix['type'] }}</td>
#foreach($results as $result)
#if($result['match_id']=== $fix['id'])
#if(isset($result['score']))
<td>{{ $result['score'] }}</td>
<td>{{ $result['result'] }}</td>
#else
<td> </td>
<td> </td>
#endif
#endif
#endforeach
</tr>
#endif
#endforeach
</table>
</div>
</div>
I want it to show the score from the DB, but if thats blank, then just to show a blank table with a background colour. However, it's simple showing nothing so when I look at it, it shows a white space.
Can anyone help?

Resources