Set the value for input label in laravel 5 form - laravel-5

This is the code where I need to set the value for the label :
{!! Form::label('unit',$value=$fileN) !!}
When I coded that it will display the following error.
ErrorException in UploadController.php line 51:
Undefined index: unit
I changed that to {{ Form::label('unit', $fileN) }}. But above same error will display. Below is the output.
<label for="unit">rainforest.pdf</label>
Controller method look likes below.
public function edit1()
{
$grade = $_POST['grade'];
$subject = $_POST['subject'];
$id = $_POST['id'];
$title = $_POST['title'];
$unit = $_POST['unit'];
$uplds = Upldtbl::findOrFail($id);
DB::table('upldtbls')
->where('id', $id)
->update(['title' => $title, 'subject' => $subject,'url' => $unit, 'grade' => $grade]);
return redirect('/hgh');
}
{!! Form::open(array('url'=>'hgh1','method'=>'POST', 'files'=>true, 'class'=>'upldform')) !!}
<table>
<div class="control-group">
<div class="controls">
<tr>
<td> <b>{!! Form::label('Grade', 'Grade') !!}</b></td>
<td> {!! Form::select('grade', array('Grade' => 'Grade','2' => '2', '3' => '3','4' => '4'), $value=$grade) !!}</td>
</tr>
#if(Session::has('errorUpldGrade'))
<tr>
<td><ul class="alert alert-danger" style="width: 250px;height: 40px">{!! Session::get('errorUpldGrade') !!}</ul></td>
</tr>
#endif
<tr>
<td><b>{!! Form::label('Subject', 'Subject') !!}</b></td>
<td>{!! Form::select('subject', array('Subject' => 'Subject','English' => 'English', 'Mathematics' => 'Mathematics','Environmental Studies' => 'Environmental Studies'), $value=$sub) !!}</td>
</tr>
#if(Session::has('errorUpldSubj'))
<tr>
<td><ul class="alert alert-danger" style="width: 250px;height: 40px"><p class="errors">{!! Session::get('errorUpldSubj') !!}</p></ul></td>
</tr>
#endif
<tr>
<td> <b>{!! Form::label('Title', 'Title') !!}</b></td>
<td> {!! Form::text('title',$value=$title) !!}</td>
</tr>
#if(Session::has('errorUpldTitle'))
<tr>
<td><ul class="alert alert-danger" style="width: 250px;height: 40px"><p class="errors">{!! Session::get('errorUpldTitle') !!}</p></ul></td>
</tr>
#endif
<tr>
<td>{!! Form::file('image') !!}{{ Form::label('unit', $fileN) }}</td>
<td><p class="errors">{!!$errors->first('image')!!}</p></td>
</tr>
#if(Session::has('errorUpldFile'))
<tr>
<td><ul class="alert alert-danger" style="width: 250px;height: 40px"><p class="errors">{!! Session::get('errorUpldFile') !!}</p></ul></td>
</tr>
#endif
<td>{!! Form::hidden('id',$id ) !!}</td><br>
</div>
</div>
<tr>
<td> {!! Form::submit('Update', array('class'=>'send-btn')) !!}</td>
</tr>
</table>
{!! Form::close() !!}
Can anybody help me that out?

You have a wrong declaration of label, instead of :
{!! Form::label('unit',$value=$fileN) !!}
It should be :
{{ Form::label('unit', $fileN) }}
And you're getting the error Undefined index: unit because you don't have any field with name unit and you're trying to capture it in :
$unit = $_POST['unit'];
You could use hidden field to send value of unit like :
{!! Form::hidden('unit',$fileN) !!}
Hope this helps.

Related

Laravel Problem Call to undefined method App\Models\User::getRoleNames()

#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Users Management</h2>
</div>
<div class="pull-right">
<a class="btn btn-success" href="{{ route('users.create') }}"> Create New User</a>
</div>
</div>
</div>
#if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
#endif
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Name</th>
<th>Email</th>
<th>Roles</th>
<th width="280px">Action</th>
</tr>
#foreach ($data as $key => $user)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>
#if(!empty($user->getRoleNames()))
#foreach($user->getRoleNames() as $v)
<label class="badge badge-success">{{ $v }}</label>
#endforeach
#endif
</td>
<td>
<a class="btn btn-info" href="{{ route('users.show',$user->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('users.edit',$user->id) }}">Edit</a>
{!! Form::open(['method' => 'DELETE','route' => ['users.destroy', $user->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</table>
{!! $data->render() !!}
<p class="text-center text-primary"><small>Tutorial by ItSolutionStuff.com</small></p>
#endsection
Im agree with Gary, if you are using Spatie add the next code to User.php
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, HasRoles;
Please add this method into User.php model file
public function getRoleNames()
{
return $this->belongsToMany(Role::class);
}
If this method not work then share you table structure and user model file code.
In the controller that call this view, you should to do:
$data = Model::all(); // ---> this solved that for me
return view('model.view',compact('data'));

Missing required parameters for [Route: roles.destroy]

i found this error
Missing required parameters for [Route: roles.destroy] [URI: roles/{role}]. (View: E:\wamp64\www\student_system\resources\views\roles\table.blade.php)
<div class="table-responsive">
<table class="table" id="roles-table">
<thead>
<tr>
<th>Name</th>
<th colspan="3">Action</th>
</tr>
</thead>
<tbody>
#foreach($roles as $role)
<tr>
<td>{{ $role->name }}</td>
<td>
{!! Form::open(['route' => ['roles.destroy', $role->id], 'method' => 'delete']) !!}
<div class='btn-group'>
</i>
</i>
{!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
</div>
{!! Form::close() !!}
</td>
</tr>
#endforeach
</tbody>
</table>
I never used Form class, but I think you could try just passing in $role.

Error "Trying to get property 'pertanyaan' of non-object" on Dompdf Laravel 5.8

I have a view and I want to have a link that downloads a PDF.
View
<div class="col-lg-12 col-xs-12">
<div class="box">
Download PDF
<div class="box-header">
<div style="text-align:center"><h3 class="box-title">PEMELIHARAAN DAN PERAWATAN ALAT UJI </h3></div>
<div style="text-align:center">Jln. Kabupaten Sragen</div>
</div>
<p> Laporan : {{ $pemeliharaan->status }} </p>
<p> Tanggal : {{ $pemeliharaan->created_at }} </p>
<p> Jenis Alat : {{ $pemeliharaan->alat->nama_alat }} </p>
<p> User : {{ $pemeliharaan->user->name }} </p>
<div class="box">
<div class="box-header">
<h3 class="box-title"></h3>
</div>
<div class="box-body no-padding">
<table class="table table-condensed">
<tbody>
<tr>
<th style="width: 10px">#</th>
<th>Pertanyaan</th>
<th>Hasil</th>
</tr>
<tr>
<td>1.</td>
<td>{{ $pemeliharaan->pertanyaan['question1'] }}</td>
<td>{{ $pemeliharaan->pertanyaan['answer1'] }}</td>
</tr>
<tr>
<td>2.</td>
<td>{{ $pemeliharaan->pertanyaan['question2'] }}</td>
<td>{{ $pemeliharaan->pertanyaan['answer2'] }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
The link 'download' has an error.
Just Showing 404
Controller
public function showQuestion(Request $request, $id)
{
$pemeliharaan = Pemeliharaan::findOrFail($id);
$pemeliharaan->pertanyaan = json_decode($pemeliharaan->pertanyaan, true);
if ($request->has('download')) {
$pdf = PDF::loadView('users.view_question', $pemeliharaan);
return $pdf->download('view_question.pdf');
}
return view('users.view_question', compact('pemeliharaan'));
}
Routes
Route::get('/user/show/question/pdf/{id}','userController#showQuestion')->name('pdf');
Route::get('user/show/question/{id}', 'userController#showQuestion')->name('usershowQuestion');
Can someone help me with the code for the download?
You are not supplying the id route parameter to the named route 'pdf'. That's causing the line $pemeliharaan = Pemeliharaan::find($id) to yield null and later an error.
Try this:
{{ route('pdf', [ $id ] }}
In order for it to work you must supply the variable $id to the view. You can do that something like
return view('your-view')->with([
"id" => $id
]);

Nested html formBuilder with Laravel

cart.blade.php
#extends('master')
#section('content')
<div class="container">
#if(Cart::isEmpty())
<b>The card is empty</b> Shopping now
#else
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th>item id</th>
....
</tr>
</thead>
<tbody>
{!! Form::open(["url"=>"/pay"]) !!}
<?php $i = 0; $totalCart_price = 0; ?>
#foreach($cart_total_items as $item)
<tr>
<td>{{ $item['id'] }}</td>
....
<td>
{!! Form::open(["url"=>"/my-cart/".$item['id'], "method"=>"DELETE", "style"=>"display: inline"]) !!}
<button type="submit" class="btn btn-danger" aria-label="Left Align">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
{!! Form::close() !!}
</td>
</tr>
<?php $i++; $totalCart_price += Cart::get($item['id'])->getPriceSum(); ?>
#endforeach
</tbody>
</table>
<b>Total price: ${{ $totalCart_price }} {{ Form::hidden("total_price", $totalCart_price) }}</b>
<br><br>
{!! Form::submit('check out (paypal)', ["class"=>"btn btn-primary"]) !!}
{!! Form::close() !!}
Clear cart
#endif
</div>
#stop
Issue: I can delete any item, but during click check out nothing happens, but when I remove clear item form, check out run successfully.
I want to run two operations, How Can I solve it?
Thanks
You can have several forms in a page but they should not be nested.
As given in html5 working draft:
4.10.3 The form element
Content model:
Flow content, but with no form element descendants.
As far as I know form is just the html tags which can be used to send a group(array) of data at once to the server.
In your case your best bet would be to use the ajax query.(though it would make our page javascript dependent)
Or, as I observed you can just seperate the two form like below:
#extends('master')
#section('content')
<div class="container">
#if(Cart::isEmpty())
<b>The card is empty</b> Shopping now
#else
<table class="table table-condensed table-bordered">
<thead>
<tr>
<th>item id</th>
....
</tr>
</thead>
<tbody>
<<!-- Remove the Form tag from here and insert it below -->>
<?php $i = 0; $totalCart_price = 0; ?>
#foreach($cart_total_items as $item)
<tr>
<td>{{ $item['id'] }}</td>
....
<td>
{!! Form::open(["url"=>"/my-cart/".$item['id'], "method"=>"DELETE", "style"=>"display: inline"]) !!}
<button type="submit" class="btn btn-danger" aria-label="Left Align">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
</button>
{!! Form::close() !!}
</td>
</tr>
<?php $i++; $totalCart_price += Cart::get($item['id'])->getPriceSum(); ?>
#endforeach
</tbody>
</table>
<<!-- Remove the Form tag from above and insert it below -->>
{!! Form::open(["url"=>"/pay"]) !!}
<b>Total price: ${{ $totalCart_price }} {{ Form::hidden("total_price", $totalCart_price) }}</b>
<br><br>
{!! Form::submit('check out (paypal)', ["class"=>"btn btn-primary"]) !!}
{!! Form::close() !!}
Clear cart
#endif
</div>
#stop

How to show image in index view page in laravel 5?

index.blade.php
I am using img src="{{ URL::asset($task->image) }}" to display image but its not working...please suggest me on how to achieve it..
any help???
#extends ('welcome')
#section ('content')
<h1 class="headng"> Form List </h1>
<table class="table table-hover ">
<tr>
<th><h4>Title</h4></th>
<th><h4>Description</h4></th>
<th><h4>Image</h4></th>
<th><h4>Printable</h4></th>
<th><h4>Actions</h4></th>
</tr>
#foreach($tasks as $task)
<tr>
<td>{{ $task->title }}</td>
<td>{{ $task->description }}</td>
<td><img src="{{ URL::asset($task->image) }}" /></td>
<td>{{ $task->printable }}</td>
<td>
<a href = '{{ action('TasksController#edit',[$task->id]) }}'>Edit</a> |
{!! Form::open([
'method' => 'DELETE',
'route' => ['tasks.destroy', $task->id]
]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</table>
#stop

Resources