How to define hyperlink in Laravel Blade template? - html

There is a button,which will redirect to a new page after being clicked.
I want to define a hyperlink for this in laravel blade template.
{!! Form::button('<span class="fa fa-arrow-circle-right"></span> Start',
array('class' => 'btn btn-next next-step pull-right'))
!!}

Wrap your button with a form:
<form action="http://example.com">
// button code
</form>
Or:
{!! Form::open(['url' => 'http://example.com']) !!}
// button code
{!! Form::close() !!}
{!! Form::open(['route' => 'route.name']) !!}
// button code
{!! Form::close() !!}
{!! Form::open(['action' => 'Controller#action']) !!}
// button code
{!! Form::close() !!}

Related

Laravel 6 - How to create a table with dynamic rows using LaravelCollective?

I want to make a dynamic table using LaravelCollective, where you can add/remove rows.
I already tried to follow a few tutorials, but I keep failing. I don't know what I'm doing wrong.
Html Code for the table/form
{{ Form::open(['action' => 'TransactionsINController#store', 'method' => 'POST']) }}
<section>
<div class="panel panel-header">
<div class="form-group">
{{ Form::label('supplier_name', 'Supplier Name') }}
{{ Form::select('supplier_name', $supplierList->pluck('name', 'id'),
null, ['class' => 'form-control', 'placeholder' => 'Pick one supplier...']) }}
</div>
<div class="form-group">
{{ Form::label('transaction_in_date', 'Transcation Date') }} <br>
{{ Form::date('transaction_in_date', \Carbon\Carbon::now()->format('d M Y')) }}
</div>
</div>
<div class="panel panel-footer">
<table class="table table-hover table-bordered" id="">
<thead align="center">
<tr>
<th>Device Type</th>
<th>Spec</th>
<th>Price</th>
<th><a href="#" class="btn btn-success addRow">
<i class="fa fa-plus"></i>
</a>
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<div class="form-group">
{{ Form::select('device_type_name[]', $deviceTypeList->pluck('name_tipe_device', 'id'),
null, ['class' => 'form-control', 'placeholder' => 'Pick Device Type...', 'name' => 'device_type_name[]']) }}
</div>
</td>
<td>
<div class="form-group">
{{ Form::textarea('device_spec[]', '',
['class' => 'form-control', 'placeholder' => 'Device Spec', 'rows' => 5, 'cols' => 45, 'name' => 'device_spec[]']) }}
</div>
</td>
<td>
<div class="form-group">
{{ Form::number('device_price[]', 'value', ['name' => 'device_price[]']) }}
</div>
</td>
<td>
<a href="#" class="btn btn-danger remove">
<i class="fa fa-times"></i>
</a>
</td>
</tr>
</tbody>
</table>
</div>
</section>
{{ Form::button('<i class="far fa-save"></i> Submit', ['type' => 'submit', 'class' => 'btn btn-info'] ) }}
{{ Form::close() }}
The Script i use to add/remove Rows
<script type="text/javascript">
$('.addRow').on('click', function(){ \\Uncaught ReferenceError: happen in this line.
addRow();
});
function addRow(){
var tr = '<tr>'+
'<td>'+
'<div class="form-group">'+
'{{ Form::select('device_type_name[]', $deviceTypeList->pluck('nama_tipe_device', 'id'),
null, ['class' => 'form-control', 'placeholder' => 'Pick Device Type...', 'name' => 'device_type_name[]']) }}'+
'</div>'+
'</td>'+
'<td>'+
'<div class="form-group">'+
'{{ Form::textarea('device_spec[]', '',
['class' => 'form-control', 'placeholder' => 'Device Spec', 'rows' => 5, 'cols' => 45, 'name' => 'device_spec[]']) }}'+
'</div>'+
'</td>'+
'<td>'+
'<div class="form-group">'+
'{{ Form::number('device_price[]', 'value', ['name' => 'device_price[]']) }}'+
'</div>'+
'</td>'+
'<td>'+
'<a href="#" class="btn btn-danger remove">'+
'<i class="fa fa-times"></i>'+
'</a>'+
'</td>'+
'</tr>';
$('tbody').append(tr);
}
</script>
When I press the + button to add more rows it doesn't do anything and when I inspected the code in the browser, I found this error:
Uncaught ReferenceError: $ is not defined
$ refers to jQuery, have you added it to your project and is it available in this scope?
Note: it needs to be present before your script
jQuery may not have loaded when that script runs, you can try wrapping your script with $(document).ready({ }) so it runs it when jQuery has loaded

Treeview with --- select option

I'm building a treeview inside my Laravel application but I have a problem with the view. I'm currently getting everything under the same select box, for example:
demo1
demo2
demo3
However, I want this inside select options like:
demo1
--- demo2
------ demo3
Controller:
$categories = Pool::where('parent_id', '=', 0)->get();
$allCategories = Pool::pluck('title','id')->all();
return view('admin.accept', compact(['categories','allCategories']));
View:
<div class="form-group {{ $errors->has('parent_id') ? 'has-error' : '' }}">
{!! Form::label('Pool:') !!}
{!! Form::select('parent_id',$allCategories, old('parent_id'), ['class'=>'form-control', 'placeholder'=>'Kies uw Pool']) !!}
<span class="text-danger">{{ $errors->first('parent_id') }}</span>
</div>
<div class="form-group {{ $errors->has('title') ? 'has-error' : '' }}">
{!! Form::label('Naam:') !!}
{!! Form::text('title', old('title'), ['class'=>'form-control', 'placeholder'=>'Enter Title']) !!}
<span class="text-danger">{{ $errors->first('title') }}</span>
</div>
How can I do this?
How can I build a treeview inside my Laravel application?

Submit multiple forms created by foreach loop with one button

In my blade template, I use a foreach to create one form per iteration. Currently, each form has a submit button, but I want to use one submit button to submit all the form, because now I cannot submit all forms at the same time. How can I fix this?
#if(count($task_criterias) > 0)
#foreach($task_criterias as $task_criteria)
<td class="card">
<div>Description:{{$task_criteria->criteriadescription}}</div>
<div>Maximum Mark:{{$task_criteria->maximummark}}</div>
{!! Form::open([
'action' => 'CriteriaMarksController#store',
'method' => 'POST',
'name' => "form"
]) !!}
<div class="form-group" hidden >
{{ Form::label('criteria_id', 'criteria_id') }}
{{ Form::text('criteria_id', $task_criteria->id, ['class'=>'form-control']) }}
</div>
<div>
{{ Form::label('selfmark','Mark ') }}
{{ Form::number('selfmark', '',['placeholder'=>'', 'class' => 'col-lg-3 control-label']) }}
</div>
{!! Form::close() !!}
</td>
#endforeach
<input type="button" class="btn btn-info " value="Submit" onclick="submitForms()" />
<script>
submitForms = function() {
$("form").each(function(){
$.ajax({
method:'POST',
url:'/criteria_marks/post',
data: $(this).serialize(),
success: function(r){
//...
}
});
});
}
</script>
#endif
There can be only one post submit per request. One of the easiest solution is to perfom multiple requests using ajax:
<script>
submitForms = function() {
$("form").each(function(){
$.ajax({
method:'POST',
url:'* route to CriteriaMarksController#store *',
data: $(this).serialize(),
success: function(r){
//...
}
});
});
}
</script>
Or you can make one big form:
{!! Form::open([
'action' => 'CriteriaMarksController#store',
'method' => 'POST',
'name' => "form"
]) !!}
#foreach($task_criterias as $key => $task_criteria)
<td class="card">
<div>Description:{{$task_criteria->criteriadescription}}</div>
<div>Maximum Mark:{{$task_criteria->maximummark}}</div>
<div>
{{ Form::label('selfmark','Mark ') }}
{{ Form::number('selfmark['.$task_criteria->id.']', '',
['placeholder'=>'', 'class' => 'col-lg-3 control-label']) }}
</div>
</td>
#endforeach
<input type="submit" class="btn btn-info " value="Submit" />
{!! Form::close() !!}
This will form associative array selfmark in post with id => value pairs
Not sure how to do it with the Form facade.
What you have now is:
<input name="criteria_id" value="{{ $task_criteria->id }}" />
<input name="selfmark" value="{{ $task_criteria->id }}" />
But what you need to have is:
<input name="criteria_id[]" value="{{ $task_criteria->id }}" />
<input name="selfmark[]" />
Note the [] in the input names.

QueryException (23000) SQLSTATE[23000]: Integrity constraint v1452

I am sucked by this error since this week could any one help me please !
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (practice2.posts, CONSTRAINT posts_category_id_foreign FOREIGN KEY (category_id) REFERENCES categories (id)) (SQL: insert into posts (author_id, updated_at, created_at) values (2, 2017-11-13 05:48:53, 2017-11-13 05:48:53))
my codes are:
Blog controller :
public function store(Requests\PostRequest $request)
{
$request->user()->posts()->create($request->all());
redirect('/backend/blog')->with('message', 'Your post was created successfully!');
}
**migration:**
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('author_id')->unsigned();
$table->foreign('author_id')->references('id')->on('users')->onDelete('restrict');
$table->string('tittle');
$table->string('slug')->unique;
$table->text('excerpt');
$table->text('body');
$table->string('image')-> nullable();
$table->timestamps();
});
}
alter_post_migration :
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories')->onDelete('restrict');
//
});
}
create post form:
<div class="box-body ">
{!! Form::model($post, [
'method' => 'POST',
'route' => 'back.store'
]) !!}
<div class="form-group {{ $errors->has('tittle') ? 'has-error' : '' }}">
{!! Form::label('tittle') !!}
{!! Form::text('tittle', null, ['class' => 'form-control']) !!}
#if($errors->has('tittle'))
<span class="help-block">{{ $errors->first('tittle') }}</span>
#endif
</div>
<div class="form-group {{ $errors->has('slug') ? 'has-error' : '' }}">
{!! Form::label('slug') !!}
{!! Form::text('slug', null, ['class' => 'form-control']) !!}
#if($errors->has('slug'))
<span class="help-block">{{ $errors->first('slug') }}</span>
#endif
</div>
<div class="form-group">
{!! Form::label('excerpt') !!}
{!! Form::textarea('excerpt', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group {{ $errors->has('body') ? 'has-error' : '' }}">
{!! Form::label('body') !!}
{!! Form::textarea('body', null, ['class' => 'form-control']) !!}
#if($errors->has('body'))
<span class="help-block">{{ $errors->first('body') }}</span>
#endif
</div>
<div class="form-group {{ $errors->has('published_at') ? 'has-error' : '' }}">
{!! Form::label('published_at', 'Publish Date') !!}
{!! Form::text('published_at', null, ['class' => 'form-control', 'placeholder' => 'Y-m-d H:i:s']) !!}
#if($errors->has('published_at'))
<span class="help-block">{{ $errors->first('published_at') }}</span>
#endif
</div>
<div class="form-group {{ $errors->has('category_id') ? 'has-error' : '' }}">
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', App\Category::pluck('tittle', 'id'), null, ['class' => 'form-control', 'placeholder' => 'Choose category']) !!}
#if($errors->has('category_id'))
<span class="help-block">{{ $errors->first('category_id') }}</span>
#endif
</div>
<hr>
{!! Form::submit('Create new post', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
</div>
The error:
Integrity constraint violation: 1452 Cannot add or update a child row:
a foreign key constraint fails (practice2.posts, CONSTRAINT
posts_category_id_foreign FOREIGN KEY (category_id) REFERENCES
categories (id))
comes when two table share a relationship of foreign key and you are trying to add some data in child table and its associated record does not exist in the parent table. So check the data accordingly and try again.

Use <span> inside Form Label in Blade template

In html I can use <span> tag inside form label like this:
<label for="name" class="class-name">Name:<span class="required"></span></label>
Using Laravel Blade, the code for label is like this:
{!! Form::label('name','Name:',['class'=>'class-name']) !!}
How can I use <span> inside form label using blade template?
Try this one code:
{!! Html::decode(Form::label('name','Name: <span class="required"></span>')) !!}
Just set the 4th parameter of Form::label to false which is escape_html = false.
{!! Form::label('name','Name: <span class="required"></span>', [], false) !!}