"Trying to get property 'name' of non-object - php

I had a Laravel script installed
Now I want to go to the services list page, but with the "Trying to get the property 'name' of non-object" encountered.
please help me
My error:
Trying to get property 'name' of non-object (View: /mylocalhost/core/resources/views/admin/service.blade.php)
in line 36:
category->name); ?>
service.blade.php code:
#section('page_name', 'Services')
<a class="btn btn-outline-success btn-lg" href="#" data-toggle="modal" data-target="#addService"><i
class="fa fa-plus-circle"></i> Add Service</a>
#endsection
#section('body')
<ul class="orders-ul">
<li class="text-color"><a class="btn btn-outline-secondary" href="{{ route('api.service') }}" target="_blank">API Services List</a></li>
</ul>
<div class="row">
#include('admin.layouts.flash')
<div class="col-md-12">
<div class="tile">
<h3 class="tile-title">Services</h3>
<table class="table table-hover">
<thead>
<tr>
<th>Serial</th>
<th>Name</th>
<th>Category</th>
<th>Price per 1K</th>
<th>Min</th>
<th>Max</th>
<th>Order Response</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach($items as $key => $item)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->category->name }}</td>
<td>{{ $item->price_per_k }}</td>
<td>{{ $item->min }}</td>
<td>{{ $item->max }}</td>
<td>{{ $item->service_id == 0? 'Manual' : 'API' }}</td>
<td>
{!! $item->status == 0 ? '<b style="color:#d35400">Inactive</b>' : '<b style="color:#16a085">Active</b>' !!}
</td>
<td>
<button class="btn btn-outline-info service_edit_btn"
data-toggle="modal"
data-target="#editService"
data-route="{{ route('service.update', $item->id) }}"
data-category="{{ $item->category_id }}"
data-name="{{ $item->name }}"
data-price="{{ $item->price_per_k }}"
data-min="{{ $item->min }}"
data-max="{{ $item->max }}"
data-service_id="{{ $item->service_id }}"
data-details="{{ $item->details }}"
data-status="{{ $item->status }}">
<i class="fa fa-edit`enter code here`"></i></button>
<button class="btn btn-outline-danger service_dlt_btn"
data-toggle="modal"
data-target="#delService"
data-route="{{ route('service.delete', $item->id) }}">
<i class="fa fa-trash"></i></button>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
<div class="modal fade" id="addService" tabindex="-1" role="basic" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4><b>Add Service</b></h4>
</div>
<div class="modal-body">
<div class="portlet light bordered">
<div class="portlet-body form">
<form role="form" action="{{ route('service.store') }}" method="post">
#csrf
<div class="form-body">
<div class="form-group">
<label for=""><b>Category</b></label>
<select name="category_id" class="form-control form-control-lg">
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for=""><b>Name</b></label>
<input type="text" name="name" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for=""><b>Price per 1k</b></label>
<div class="input-group">
<input type="text" name="price_per_k" class="form-control form-control-lg">
<div class="input-group-append">
<span class="input-group-text">{{ $general->currency_symbol }}</span>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label for=""><b>Min</b></label>
<input type="text" name="min" class="form-control form-control-lg">
</div>
<div class="form-group col-md-6">
<label for=""><b>Max</b></label>
<input type="text" name="max" class="form-control form-control-lg">
</div>
</div>
<div class="form-group">
<label for=""><b>Details</b></label>
<textarea class="form-control" name="details" rows="8"></textarea>
</div>
<div class="form-group">
<label for=""><b>service Id (If order process through API)</b></label>
<input type="text" name="service_id" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for=""><b>Status</b></label>
<input data-toggle="toggle" data-onstyle="success" data-offstyle="danger"
data-on="Active" data-off="Inactive" data-width="100%" type="checkbox" name="status" value="1">
</div>
</div>
<div class="tile-footer">
<button class="btn btn-primary btn-block btn-lg" type="submit"><i
class="fa fa-fw fa-lg fa-check-circle"></i>Save
</button>
</div>
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<div class="modal fade" id="editService" tabindex="-1" role="basic" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h4><b>Edit Service</b></h4>
</div>
<div class="modal-body">
<div class="portlet light bordered">
<div class="portlet-body form">
<form role="form" action="" method="post"
id="serviceEditdForm">
#csrf
#method('put')
<div class="form-body">
<div class="form-group">
<label for=""><b>Category</b></label>
<select name="category_id" id="category_id" class="form-control form-control-lg">
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for=""><b>Name</b></label>
<input type="text" name="name" id="name" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for=""><b>Price per 1k</b></label>
<div class="input-group">
<input type="text" name="price_per_k" id="price_per_k" class="form-control form-control-lg">
<div class="input-group-append">
<span class="input-group-text">{{ $general->currency_symbol }}</span>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-6">
<label for=""><b>Min</b></label>
<input type="text" name="min" id="min" class="form-control form-control-lg">
</div>
<div class="form-group col-md-6">
<label for=""><b>Max</b></label>
<input type="text" name="max" id="max" class="form-control form-control-lg">
</div>
</div>
<div class="form-group">
<label for=""><b>Details</b></label>
<textarea class="form-control" name="details" id="details" rows="8"></textarea>
</div>
<div class="form-group">
<label for=""><b>service Id (If order process through API)</b></label>
<input type="text" name="service_id" id="service_id" class="form-control form-control-lg">
</div>
<div class="form-group">
<label for=""><b>Status</b></label>
<input data-toggle="toggle" data-onstyle="success" data-offstyle="danger"
data-on="Active" data-off="Inactive" data-width="100%" type="checkbox" name="status" id="status" value="1">
</div>
</div>
<div class="tile-footer">
<button class="btn btn-primary btn-block btn-lg" type="submit"><i
class="fa fa-fw fa-lg fa-check-circle"></i>Save
</button>
</div>
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-danger" data-dismiss="modal">Cancel</button>
</div>
</div>
<!-- /.modal-content -->
</div>
<!-- /.modal-dialog -->
</div>
<div class="modal fade" id="delService">
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content">
<div class="modal-body">
<form action="" method="post" id="serviceDelForm">
#csrf
#method('delete')
<h3 class="d-flex justify-content-center">Are you want to delete this Service?</h3>
<div class="tile-footer d-flex justify-content-center">
<button class="btn btn-primary" type="submit"><i class="fa fa-fw fa-lg fa-check-circle"></i>Confirm
</button>
<a class="btn btn-danger" href="#" data-dismiss="modal"><i
class="fa fa-fw fa-lg fa-times-circle"></i>Cancel</a>
</div>
</form>
</div>
</div>
</div>
</div>
#endsection
#section('scripts')
<script type="text/javascript">
$(document).ready(function () {
$(document).on('click', '.service_edit_btn', function () {
var route = $(this).data('route');
var category = $(this).data('category');
var name = $(this).data('name');
var price = $(this).data('price');
var min = $(this).data('min');
var max = $(this).data('max');
var service_id = $(this).data('service_id');
var details = $(this).data('details');
var status = $(this).data('status');
$('#category_id').val(category).attr('selected', 'selected');
$('#name').val(name);
$('#price_per_k').val(price);
$('#min').val(min);
$('#max').val(max);
$('#service_id').val(service_id);
$('#details').val(details);
$('#serviceEditdForm').attr('action',route);
if(status == 1){
$('#status').bootstrapToggle('on');
}else{
$('#status').bootstrapToggle('off');
}
});
$(document).on('click', '.service_dlt_btn', function () {
var route = $(this).data('route');
$('#serviceDelForm').attr('action', route);
});
});
</script>
#endsection
And service list controller in Admin controller:
public function service(){
$categories = Category::orderBy('name')->get();
$items = Service::orderBy('category_id')->paginate(10);
return view('admin.service', compact('items', 'categories'));
}
public function serviceStore(Request $request){
$this->validate($request, [
'category_id' => 'required',
'name' => 'required|unique:services,name',
'price_per_k' => 'required',
'min' => 'required',
'max' => 'required',
'details' => 'required',
], [
'category_id.required' => 'Category name is required',
'price_per_k.required' => 'price per 1k is required',
]);
$excp = $request->except('_token', 'status', 'service_id');
$status = $request->status == 1? 1:0;
if(isset($request->service_id)){
$service_id = $request->service_id;
}else{
$service_id = 0;
}
$sev = Service::create($excp + ['status' => $status, 'service_id' => $service_id]);
$users = User::all();
foreach ($users as $user){
$servicePrice = new ServicePrice();
$servicePrice->category_id = $sev->category_id;
$servicePrice->service_id = $sev->id;
$servicePrice->user_id = $user->id;
$servicePrice->price = $sev->price_per_k;
$servicePrice->save();
}
session()->flash('success', 'Service Stored successfully');
return back();
}
public function serviceUpdate(Request $request, $id){
$this->validate($request, [
'category_id' => 'required',
'name' => 'required',
'price_per_k' => 'required',
'min' => 'required',
'max' => 'required',
'details' => 'required',
], [
'category_id.required' => 'Category name is required',
'price_per_k.required' => 'price per 1k is required',
]);
$excp = $request->except('_token', 'status', 'service_id');
$status = $request->status == 1? 1:0;
if(isset($request->service_id)){
$service_id = $request->service_id;
}else{
$service_id = 0;
}
Service::findOrFail($id)->update($excp + ['status' => $status, 'service_id' => $service_id]);
session()->flash('success', 'Service Updated successfully');
return back();
}
public function serviceDelete($id){
Service::findOrFail($id)->delete();
session()->flash('success', 'Service deleted successfully');
return back();
}

This means your item don't have category, So you can do something like this.
<td>
#if(!empty($item->category)) {{ $item->category->name }} #endif
</td>

Other way to solve this problem using php7 is to use null coalescing operator which:
It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.
<td>{{ $item->category->name ?? '' }}</td>

Related

Laravel 8 safe (think problem datetime-local input field to mysql)

When I click save on my form, the page reloads. Which is correct until then. Unfortunately, it does not save the data for me in the database. So I tried to find the error with dd($request->all()). Unfortunately, I haven't found a real error as far as the correct data is loaded on the form. However, they never end up in the database. I currently only have one guess that it is due to the wrong format of DateTime_local input. But can't say for sure.
I don't get any error messages or anything like that.
Here is a small snippet of my code:
app/Http/Controllers/TasksController
public function store(Request $request)
{
// validate all required Data
$request->validate([
'title' => 'required',
'user' => 'required',
'labels' => 'required',
'work' => 'required',
'start_work' => 'required',
'problems' => 'required',
'stop_work' => 'required',
]);
$input = $request->all();
Task::create($input);
return back()->with('success', 'Successfully saved.');
}
app/Models/Task.php
class Task extends Model
{
use HasFactory;
public $fillable = ['title', 'user', 'labels', 'work', 'start_work', 'problems', 'stop_work'];
}
Migration
public function up()
{
Schema::create('task', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('user');
$table->string('label');
$table->string('work');
$table->string('problems');
$table->dateTime('start_work');
$table->dateTime('stop_work');
$table->timestamps();
});
}
View snippet
#extends('layouts.app')
#section('content')
<!-- Success message -->
#if(Session::has('success'))
<div class="alert alert-success">
{{Session::get('success')}}
</div>
#endif
<form method="post" action="{{ route('screate') }}" enctype="multipart/form-data">
{{ csrf_field() }}
<section class="person">
<div class="container-fluid">
<div class="card card-default">
<div class="card-header">
<h3 class="card-title">Daten</h3>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse"><i class="fas fa-minus"></i></button>
</div>
</div>
<!-- /.card-header -->
<div class="card-body">
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="title">title</label>
<input type="text" class="form-control {{ $errors->has('title') ? 'error' : '' }}" id="title" name="title" required>
</div>
<!-- /.form-group -->
</div>
<div class="col-md-4">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control {{ $errors->has('username') ? 'error' : '' }}" id="username" name="username">
</div>
</div>
<!-- /.form-group -->
<div class="col-md-4">
<div class="form-group">
<label for="labels">Labels</label>
<select id="labels" name="label" class="select2 select2-hidden-accessible {{ $errors->has('label') ? 'error' : '' }}" multiple="" name="label[]" style="width: 100%;" data-select2-id="5" tabindex="-1" aria-hidden="true">
#foreach($labels as $label)
<option value="{{ $label->id }}">{{ $label->name }}</option>
#endforeach
</select>
</div>
</div>
<!-- /.form-group -->
<div class="col-md-4">
<div class="form-group">
<label for="work">work</label>
<input type="text" class="form-control {{ $errors->has('work') ? 'error' : '' }}" id="work" name="work">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="start_work">Start-Work</label>
<input type="datetime-local" class="form-control {{ $errors->has('start_work') ? 'error' : '' }}" id="start_work" name="start_work">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="problems">Problems</label>
<input type="text" class="form-control {{ $errors->has('problems') ? 'error' : '' }}" id="problems" name="problems">
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="stop_work">Stop-Work</label>
<input type="datetime-local" class="form-control {{ $errors->has('start_work') ? 'error' : '' }}" id="stop_work" name="stop_work">
</div>
</div>
</div>
<!-- /.row -->
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div><!-- /.container-fluid -->
</section>
<section class="options">
<div class="row">
<div class="col-md-2 centered">
<button class="btn btn-danger" href="{{ route('home') }}">
<i class="fas fa-times">
</i>
Abbrechen
</button>
</div>
<div class="col-md-2">
<button class="btn btn-success" type="submit" name="submit">
<i class="fas fa-plus">
</i>
Speichern
</button>
</div>
</div>
</section>
</form>
#endsection
I hope you can help me with this small problem.
edit:
Here is my dd output:
dd() output

Trying to get property 'nama_member' of non-object in my Laravel project

I have trouble in my view Blade, what's wrong with my code, am I wrong to write code in the controller or in my code model was I declare?
the error said
Trying to get property 'nama_member' of non-object (View:
C:\xampp\htdocs\rezkastore\resources\views\pages\daftar_pelanggan.blade.php)
Blade/View
#extends('layouts.app')
#section('title', 'Daftar Pelanggan')
#section('content')
<div class="header bg-primary pb-6">
<div class="container-fluid">
<div class="header-body"> </div>
</div>
</div>
<div class="container-fluid mt--6">
<!-- Table -->
<div class="row">
<div class="col">
<div class="card">
<!-- Card header -->
<div class="card-header">
<div class="row align-items-center py-0">
<div class="col-lg-6 col-7">
<h6 class="h2 d-inline-block mb-0">Data Pelanggan</h6>
</div>
<div class="col-lg-6 col-5 text-right">
<button class="btn btn-icon btn-primary" type="button" data-toggle="modal" data-target="#addModal">
<span class="btn-inner--icon"><i class="fa fa-plus-circle" aria-hidden="true"></i></span>
<span class="btn-inner--text">Tambah Data</span>
</button>
</div>
</div>
</div>
<div class="table-responsive py-4">
<table class="table table-flush" id="datatable-basic">
<thead class="thead-light">
<tr>
<th width="30px">No</th>
<th>Nama Produk</th>
<th>Alamat</th>
<th>No.Telp</th>
<th>Member</th>
<th>Menu</th>
</tr>
</thead>
<tfoot>
<tr>
<th width="20px">No</th>
<th>Nama</th>
<th>Alamat</th>
<th>No.Telp</th>
<th>Member</th>
<th>Menu</th>
</tr>
</tfoot>
<tbody>
#php
$no = 1;
#endphp
#foreach($daftar_pelanggan as $pelanggan)
<tr>
<td>{{$no++ }}</td>
<td>{{ $pelanggan->nama_pelanggan }}</td>
<td>{{ $pelanggan->alamat }}</td>
<td>{{ $pelanggan->no_telp }}</td>
<td> {{ $pelanggan->diskon->nama_member }}</td>
<td>
<button data-toggle="modal" data-target="#editModal-{{ $pelanggan->id }}" class="btn btn-sm btn-primary"><i class="fa fa-edit"></i></button>
<button class="btn btn-sm btn-danger" type="button" onclick="deletepelanggan({{ $pelanggan->id }})"> <i class="fa fa-trash"></i>
</button>
<form id="delete-form-{{ $pelanggan->id }}" action="{{ route('daftar_pelanggan.delete',$pelanggan->id) }}" method="POST" style="display: none;">
#csrf
#method('DELETE')
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<!-- Modal Add -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title mb-0" id="addModalLabel">Tambah Data Pelanggan</h5>
</div>
<div class="modal-body">
<!-- Card body -->
<form role="form" action="{{ route('daftar_pelanggan.create') }}" method="POST">
#csrf
#method('POST')
<!-- Input groups with icon -->
<div class="form-group row">
<label for="addNamaPelanggan" class="col-md-4 col-form-label form-control-label">Nama <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="nama" placeholder="Nama Lengkap" id="addNamaPelanggan" name="addNamaPelanggan" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')">
</div>
</div>
<div class="form-group row">
<label for="addAlamat" class="col-md-4 col-form-label form-control-label">Alamat <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="alamat" placeholder="Jatibarang" id="addAlamat" name="addAlamat" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')">
</div>
</div>
<div class="form-group row">
<label for="addNoTelp" class="col-md-4 col-form-label form-control-label">No.Telp <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="notelp" placeholder="083XXXXXXXXX" id="addNoTelp" name="addNoTelp" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')">
</div>
</div>
<div class="form-group row">
<label for="addNoTelp" class="col-md-4 col-form-label form-control-label">diskon Member <span class="text-danger">*</span></label>
<div class="col-md-8">
<select class="form-control" name="AddDiskonid" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')"">
<option disabled selected>-- Pilih Member --</option>
#foreach($diskons as $diskon)
<option value="{{ $diskon->id }}">{{ $diskon->nama_member }}</option>
#endforeach
</select>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Tambah Data</button>
</div>
</form>
</div>
</div>
</div>
<!-- Modal edit -->
#foreach($daftar_pelanggan as $pelanggan)
<div class="modal fade" id="editModal-{{ $pelanggan->id }}" tabindex="-1" role="dialog" aria-labelledby="editModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title mb-0" id="editModalLabel">Update Data Pelanggan</h5>
</div>
<div class="modal-body">
<!-- Card body -->
<form role="form" action="{{ route('daftar_pelanggan.update', $pelanggan->id) }}" method="POST" id="editForm">
#csrf
#method('PUT')
<!-- Input groups with icon -->
<div class="form-group row">
<label for="updateNamaPelanggan" class="col-md-4 col-form-label form-control-label">Nama <span class="text-danger">*</span></label>
<div class="col-md-8">
<input type="hidden" name="id" value="{{ $pelanggan->id }}">
<input class="form-control" type="nama" value="{{ $pelanggan->nama_pelanggan }}" name="updateNamaPelanggan" required >
</div>
</div>
<div class="form-group row">
<label for="updateAlamat" class="col-md-4 col-form-label form-control-label">Alamat <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="alamat" value="{{ $pelanggan->alamat }}" name="updateAlamat" required>
</div>
</div>
<div class="form-group row">
<label for="updateNoTelp" class="col-md-4 col-form-label form-control-label">No.Telp <span class="text-danger">*</span></label>
<div class="col-md-8">
<input class="form-control" type="notelp" value="{{ $pelanggan->no_telp }}" name="updateNoTelp" required>
</div>
</div>
<div class="form-group row">
<label for="addNoTelp" class="col-md-4 col-form-label form-control-label">Diskon Member <span class="text-danger">*</span></label>
<div class="col-md-8">
<select class="form-control" name="diskon_id" required oninvalid="this.setCustomValidity('data tidak boleh kosong')" oninput="setCustomValidity('')">
<option disabled selected>-- Kategori Member --</option>
#foreach($diskons as $diskon)
<option
#if($produk->diskon_id == $diskon->id)
selected="selected"
#endif
value="{{ $diskon->id }}">{{ $diskon->nama_member }}</option>
#endforeach
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Update Data</button>
</div>
</form>
</div>
</div>
</div>
#endforeach
#include('layouts.footers.auth')
#section('scripts')
<script type="text/javascript">
function deletepelanggan(id) {
swal({
title: 'Yakin Ingin Hapus Data ini?',
text: "Data Tidak Bisa Dikembalikan Setelah Dihapus!",
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Ya, Hapus!',
cancelButtonText: 'Tidak',
confirmButtonClass: 'btn btn-success',
cancelButtonClass: 'btn btn-danger',
buttonsStyling: false,
reverseButtons: true
}).then((result) => {
if (result.value) {
event.preventDefault();
document.getElementById('delete-form-'+id).submit();
swal(
'Deleted!',
'Your file has been deleted.',
'success')
} else (
result.dismiss === swal.DismissReason.cancel
)
})
}
</script>
#endsection
#endsection
Controller
class DaftarPelangganController extends Controller
{
public function index()
{
$daftar_pelanggan = DaftarPelanggan::all();
$diskons = Diskon::all();
return view('pages.daftar_pelanggan', compact('daftar_pelanggan', 'diskons'));
}
public function update(Request $request, $id)
{
$update_pelanggan = DaftarPelanggan::findOrFail($id);
$update_pelanggan->nama_pelanggan = $request->updateNamaPelanggan;
$update_pelanggan->alamat = $request->updateAlamat;
$update_pelanggan->no_telp = $request->updateNoTelp;
$update_pelanggan->diskon_id = $request->diskon_id;
$update_pelanggan->save();
if ($update_pelanggan) {
Alert::success(' Berhasil Update Data ', ' Silahkan dicek kembali');
} elseif (!$update_pelanggan) {
Alert::error('Data Sudah Ada', ' Silahkan coba lagi');
}
return redirect()->back();
}
public function create(Request $request)
{
$simpan = DB::table('daftar_pelanggans')->insert([
'nama_pelanggan' => $request->post('addNamaPelanggan'),
'alamat' => $request->post('addAlamat'),
'no_telp' => $request->post('addNoTelp'),
'diskon_id' => $request->post('addDiskonid'),
]);
if ($simpan) {
Alert::success(' Berhasil Tambah data ', ' Silahkan dicek kembali');
} else {
Alert::error('data gagal disimpan ', ' Silahkan coba lagi');
}
return redirect()->back();
}
public function delete($id)
{
DB::table('daftar_pelanggans')->where('id', $id)->delete();
return redirect()->back();
}
}
Model
class DaftarPelanggan extends Model
{
use HasFactory;
protected $table = "daftar_pelanggans";
protected $primaryKey = 'id';
protected $fillable = [
'nama_pelanggan',
'alamat',
'no_telp',
'poin',
'diskon_id',
];
public function diskon()
{
return $this->belongsTo(Diskon::class, 'diskon_id');
}
}
Eager load the diskon relation, it will also help prevent the N+1 issue.
public function index()
{
$daftar_pelanggan = DaftarPelanggan::with('diskon')->get();
$diskons = Diskon::all();
//Since $diskons is required for selects where in only id and nama_member is required
//You can select the two columns only to save on memory
$diskons = Diskon::select('id', 'nama_member')->get();
return view('pages.daftar_pelanggan',compact('daftar_pelanggan','diskons'));
}
To avoid getting error when a relation/related model does not exist, when displaying the related model's in a loop in blade, we can define the relation with a default
public function diskon(){
return $this->belongsTo(Diskon::class,'diskon_id')->withDefault([
'nama_member' => 'Guest',
]);
}

GET not supported although I am using PUT in Laravel

I am using Laravel 7 and I can add entries and view them from the database. When I try to edit or update edited changes, I either get a warning from Laravel saying that the The GET method is not supported for this route. Supported methods: PUT. However, I am using PUT in both the web.php route as well as in my method calls. Surely I am doing something wrong.
Here is a view of my Routes calling artisan route:list
in my Route Group in web.php Here are the controllers I am calling:
Route::group(['middleware' => ['auth', 'isAdmin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('registered-user', 'Admin\RegisteredController#index');
Route::get('registered-empresa', 'Admin\EmpresaController#index');
Route::get('role-edit/{id}', 'Admin\RegisteredController#edit');
Route::put('role-update/{id}', 'Admin\RegisteredController#updaterole');
Route::post('save-empresa', 'Admin\EmpresaController#store');
Route::put('edit-empresa/{id}', 'Admin\EmpresaController#update');
});
Here is the update function I created in EmpresaController.php:
public function update(Request $request, $id)
{
$this->validate($request, [
'erfc' => 'required',
'enombre' => 'required',
'ecalle' => 'required',
'ecolonia' => 'required',
'eciudad' => 'required',
'eestado' => 'required',
'ecpostal' => 'required',
'epais' => 'required',
]);
$empr = Empresa::find($id);
$empr->erfc = $request->input('erfc');
$empr->enombre = $request->input('enombre');
$empr->ecalle = $request->input('ecalle');
$empr->ecolonia = $request->input('ecolonia');
$empr->eciudad = $request->input('eciudad');
$empr->eestado = $request->input('eestado');
$empr->ecpostal = $request->input('ecpostal');
$empr->epais = $request->input('epais');
$empr->update();
return redirect('/registered-empresa')->with('status', 'Empresa se actualizó correctamente.');
}
And finally, here is the location of my empresas table where I both add, view and update my table in my index.blade.php file:
#extends('layouts.admin')
#section('content')
<div class="container-fluid mt-5">
<!-- Heading -->
<div class="card mb-4 wow fadeIn">
<!--Card content-->
<div class="card-body d-sm-flex justify-content-between">
<h4 class="mb-2 mb-sm-0 pt-1">
Home Page
<span>/</span>
<span>Empresas Registradas</span>
</h4>
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
<div class="modal fade" id="modalRegisterForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h4 class="modal-title w-100 font-weight-bold">Añadir Empresa</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="/save-empresa" method="POST">
{{ csrf_field() }}
<div class="modal-body mx-3">
<div class="md-form mb-1">
<input type="text" name="erfc" id="orangeForm-erfc" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-erfc">RFC</label>
</div>
<div class="md-form mb-1">
<input type="text" name="enombre" id="orangeForm-enombre" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-enombre">Nombre</label>
</div>
<div class="md-form mb-1">
<input type="text" name="ecalle" id="orangeForm-ecalle" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-ecalle">Calle</label>
</div>
<div class="md-form mb-1">
<input type="text" name="ecolonia" id="orangeForm-ecolonia" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-ecolonia">Colonia</label>
</div>
<div class="md-form mb-1">
<input type="text" name="eciudad" id="orangeForm-eciudad" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-eciudad">Ciudad</label>
</div>
<div class="md-form mb-1">
<input type="text" name="eestado" id="orangeForm-eestado" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-eestado">Estado</label>
</div>
<div class="md-form mb-1">
<input type="text" name="ecpostal" id="orangeForm-ecpostal" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-ecpostal">Codigo Postal</label>
</div>
<div class="md-form mb-1">
<input type="text" name="epais" id="orangeForm-epais" class="form-control validate">
<label data-error="wrong" data-success="right" for="orangeForm-epais">País</label>
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="euser" readonly id="orangeForm-euser" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="eregby" readonly id="orangeForm-eregby" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
</div>
<div class="modal-footer d-flex justify-content-center">
<button type="submit" class="btn btn-deep-orange">Añadir</button>
</div>
</form>
</div>
</div>
</div>
<div class="text-center">
<i class="fa fa-plus" aria-hidden="true"></i> Add
</div>
<!--edit modal start-->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header text-center">
<h4 class="modal-title w-100 font-weight-bold">Editar Empresa</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form action="edit-empresa/" id="editForm">
{{ csrf_field() }}
#method('PUT')
<div class="modal-body mx-3">
<div class="md-form mb-1">
<input placeholder="RFC" type="text" name="erfc" id="erfc" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Nombre" type="text" name="enombre" id="enombre" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Calle" type="text" name="ecalle" id="ecalle" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Colonia" type="text" name="ecolonia" id="ecolonia" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Ciudad" type="text" name="eciudad" id="eciudad" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Estado" type="text" name="eestado" id="eestado" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="Codigo Postal" type="text" name="ecpostal" id="ecpostal" class="form-control validate">
</div>
<div class="md-form mb-1">
<input placeholder="País" type="text" name="epais" id="epais" class="form-control validate">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="euser" readonly id="euser" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="eregby" readonly id="eregby" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
</div>
{{-- <div class="modal-footer d-flex justify-content-center">
<button type="submit" class="btn btn-deep-orange">Editar</button>
</div> --}}
<div class="modal-footer d-flex justify-content-center">
<button type="submit" class="btn btn-deep-orange">Editar</button>
</div>
</form>
</div>
</div>
</div>
<!--end edit modal-->
</div>
</div>
<!-- Heading -->
<!--Grid row-->
<!--Grid column-->
<div class="row">
<!--Card-->
<div class="col-md-12 mb-4">
<!--Card content-->
<div class="card">
<!-- List group links -->
<div class="card-body">
<table id="datatable2" class="table table-bordered">
<thead>
<tr>
<th>RFC</th>
<th>Nombre</th>
<th>Calle</th>
<th>Colonia</th>
<th>Ciudad</th>
<th>Estado</th>
<th>Codigo Postal</th>
<th>País</th>
<th>Acción</th>
</tr>
</thead>
<tbody>
#foreach ($empresas as $empresa)
<tr>
<td>{{ $empresa->erfc }}</td>
<td>{{ $empresa->enombre }}</td>
<td>{{ $empresa->ecalle }}</td>
<td>{{ $empresa->ecolonia }}</td>
<td>{{ $empresa->eciudad }}</td>
<td>{{ $empresa->eestado }}</td>
<td>{{ $empresa->ecpostal }}</td>
<td>{{ $empresa->epais }}</td>
<td>
<div class="text-center">
Editar
<a class="badge badge-pill btn-danger px-3 py-2" href="">Borrar</a>
</div>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<!-- List group links -->
</div>
</div>
<!--/.Card-->
</div>
<!--Grid row-->
</div>
#endsection
#section('scripts')
<script>
$(document).ready(function() {
let table = $('#datatable2').DataTable();
// Start edit record
table.on('click', '.edit', function() {
$tr = $(this).closest('tr');
if($($tr).hasClass('child')) {
$tr = $tr.prev('.parent');
}
let data = table.row($tr).data();
console.log(data);
$('#erfc').val(data[0]);
$('#enombre').val(data[1]);
$('#ecalle').val(data[2]);
$('#ecolonia').val(data[3]);
$('#eciudad').val(data[4]);
$('#eestado').val(data[5]);
$('#ecpostal').val(data[6]);
$('#epais').val(data[7]);
$('#editForm').attr('action', '/edit-empresa/'+data[0]);
$('#editModal').modal('show');
});
// End edit record
});
</script>
#endsection
I am pretty sure it is in this file that I am doing something wrong. Any help on how I can do this better or if I missed something, I would surely appreciate it. Thank you in advance.
in web.php
Route::patch('edit-empresa/{id}', 'Admin\EmpresaController#update');
index.blade.php
#method('PATCH')
You need to specify method as POST when defining the form even though you are including the #method('PUT') inside the form. That is because HTML does not support PUT method directly, and by default it will be a GET. So to correct:
Change this:
<form action="edit-empresa/" id="editForm">
TO
<form action="edit-empresa/" id="editForm" method="POST">
For some reason I could not get this to work using the modal and jquery method so I eliminated the datatables jquery from the bottom of the index.blade.php. My first error was to not call the data first. I created a seperate file called edit.blade.php within the view-admin-empresa folder. Here is the code:
#extends('layouts.admin')
#section('content')
<div class="container-fluid mt-5">
<!-- Heading -->
<div class="card mb-4 wow fadeIn">
<!--Card content-->
<div class="card-body d-sm-flex justify-content-between">
<h4 class="mb-2 mb-sm-0 pt-1">
<span>Empresa Registrada - Editar Empresa</span>
</h4>
</div>
</div>
<!-- Heading -->
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-header">
<h4 class="card-title">Editar Empresa</h4>
<form action="{{ url('empresa-update/'.$empresa->id) }}" id="editForm" method="POST">
{{ csrf_field() }}
{{ method_field('PUT') }}
<div class="modal-body mx-3">
<div class="md-form mb-1">
<label for="erfc">RFC</label>
<input value="{{ $empresa->erfc }}" type="text" name="erfc" id="erfc" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="enombre">Nombre</label>
<input value="{{ $empresa->enombre }}" type="text" name="enombre" id="enombre" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="ecalle">Calle</label>
<input value="{{ $empresa->ecalle }}" type="text" name="ecalle" id="ecalle" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="ecolonia">Colonia</label>
<input value="{{ $empresa->ecolonia }}" type="text" name="ecolonia" id="ecolonia" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="ecuidad">Ciudad</label>
<input value="{{ $empresa->eciudad }}" type="text" name="eciudad" id="eciudad" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="eestado">Estado</label>
<input value="{{ $empresa->eestado }}" type="text" name="eestado" id="eestado" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="ecpostal">Codigo Postal</label>
<input value="{{ $empresa->ecpostal }}" type="text" name="ecpostal" id="ecpostal" class="form-control validate">
</div>
<div class="md-form mb-1">
<label for="epais">País</label>
<input value="{{ $empresa->epais }}" type="text" name="epais" id="epais" class="form-control validate">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="euser" readonly id="euser" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
<div style="display: none;" class="md-form mb-1">
<input type="text" name="eregby" readonly id="eregby" class="form-control validate" value="{{ Auth::user()->id }}">
</div>
</div>
<div class="modal-footer d-flex justify-content-center">
Cancelar
<button type="submit" class="btn btn-deep-orange">Editar</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Then in the web.php, I created the following routes:
Route::group(['middleware' => ['auth', 'isAdmin']], function () {
Route::get('/dashboard', function () {
return view('admin.dashboard');
});
Route::get('registered-user', 'Admin\RegisteredController#index');
Route::get('registered-empresa', 'Admin\EmpresaController#index');
Route::get('role-edit/{id}', 'Admin\RegisteredController#edit');
Route::put('role-update/{id}', 'Admin\RegisteredController#updaterole');
Route::post('save-empresa', 'Admin\EmpresaController#store');
Route::get('/edit-empresa/{id}', 'Admin\EmpresaController#edit');
Route::put('/empresa-update/{id}', 'Admin\EmpresaController#update');
});
As mentioned earlier, I eliminated the edit modal and redirected to the empresa edit.blade.php file.
Now, I am able to edit without any problems. Thank you to Arjun bhati and user3532758 for taking a stab at this issue. I really appreciate it.

Show part of page body to paid users in laravel

I've store in my application (LARAVEL 5.4) and I want to share some download links in bottom of my product body but to hide it from everyone except who paid for that specific product.
Here is explanation of how I get user data and their request for specific product:
Form of their request:
<form class="form-horizontal" action="{{route('buy_course')}}" method="POST" id="contact_form">
{{ csrf_field() }}
<input name="user_id" value="{{ Auth::user()->id }}" class="form-control" type="hidden">
<input name="course_id" value="{{ $course->id }}" class="form-control" type="hidden">
<div class="form-group">
<label class="col-md-3 control-label">UserName</label>
<div class="col-md-9 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input name="username" value="{{ Auth::user()->username }}" class="form-control" type="text" readonly>
</div>
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="col-md-3 control-label">E-Mail</label>
<div class="col-md-9 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-envelope"></i></span>
<input name="email" value="{{ Auth::user()->email }}" class="form-control" type="text" readonly>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Course</label>
<div class="col-md-9 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-graduation-cap"></i></span>
<input name="course" value="{{ $course->course_name }}" class="form-control" type="text" readonly>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label" for="type">Type</label>
<div class="col-md-9 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<select class="form-control" id="type" name="type">
<option value="">Select Type</option>
<option value="Download Link">Download Link</option>
<option value="Physical Disk">Physical Disk</option>
</select>
</div>
</div>
</div>
#if (!empty($course->extra_description))
<div class="form-group">
<label class="col-md-3 control-label" for="type">{{ $course->extra_title }}</label>
<div class="col-md-9 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<select class="form-control" id="type" name="extra_price">
<option value="">Do you need extra?</option>
<option value="{{$course->extra_price}}">Yes - Add {{$course->extra_price}} Rp</option>
<option value="0">No</option>
</select>
</div>
</div>
</div>
#endif
<div class="form-group">
<label class="col-md-3 control-label">Note to seller</label>
<div class="col-md-9 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-comment-o"></i></span>
<textarea name="note" id="text" placeholder="Your note to seller here..." class="form-control" rows="8" ></textarea>
</div>
<h6 class="pull-right" id="count_message"></h6>
</div>
</div>
<div class="text-center">
<input type="submit" class="btn btn-block btn-success" value="Send">
</div>
</form>
The controller:
public function postbuycourse(Request $request) {
$this->validate($request, array(
'user_id' => 'required',
'username' => 'required',
'email' => 'required|email',
'course' => 'required',
'note' => 'sometimes|max:500',
'type' => 'required',
));
DB::table('purchases')->insert([
'user_id' => $request->user_id,
'course_id' => $request->course_id,
'note' => $request->note,
'type' => $request->type,
'status' => 0,
'invoice_nu' => str_random(15),
'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
]);
$data = array(
'username' => $request->username,
'email' => $request->email,
'course' => $request->course,
'note' => $request->note,
'type' => $request->type,
);
Mail::send('emails.buycourse', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('xxxxxx#xxxxxx.com');
$message->subject($data['course']);
});
$id = $request->course_id;
$course = Course::findOrFail($id);
Mail::to($request->user())->send(new CourseReceived($course));
Session::flash('flash_message', 'Your Order was sent. Our sell team will contact you shortly.');
return redirect()->back();
}
And here is how I show them their order and their order status that if is paid or not:
#if (Auth::user()->purchase->count() )
<h4><i class="fa fa-graduation-cap"></i> Courses Orders</h4>
<table class="mt-20 table table-bordered table-hover table-responsive">
<thead>
<tr class="bg-primary">
<th class="text-center">ID</th>
<th class="text-center">Invoice Number</th>
<th class="text-center">Course Name</th>
<th class="text-center">Note</th>
<th class="text-center">Sum</th>
<th class="text-center">Status</th>
<th class="text-center">Time</th>
<th class="text-center">Expire</th>
<th class="text-center">Options</th>
</tr>
</thead>
<tbody class="text-center">
#foreach ($purchases as $purchase)
<tr>
<td id="primary">{{$purchase->id}}</td>
<td>{{$purchase->invoice_nu}}</td>
<td>{{$purchase->course->course_name}}</td>
<td>
#if(!empty($purchase->note))
{{ $purchase->note }}
#else
-
#endif
</td>
<td>
#if( ! empty($purchase->course->course_disscount))
<span class="text-primary">Price: <del>{{$purchase->course->course_price}}</del></span><br>
<span class="text-danger">disscount: {{$purchase->course->course_disscount}}</span>
<hr>
<span class="text-success">Total: {{ number_format($purchase->course->course_price - $purchase->course->course_disscount, 0) }} Rp</span>
#else
{{ number_format($purchase->course->course_price, 0) }} Rp
#endif
</td>
<td>
#if($purchase->status == 0)
<span class="text-danger">Waiting Payment</span>
#else
<span class="text-success">Paid</span>
#endif
</td>
<td>{{ $purchase->created_at->format('d, M, Y | h:i A') }}</td>
<td>
#if($purchase->status == 0)
<span class="text-danger">-</span>
#else
<span class="text-success">{{ $purchase->created_at->format('d, M, Y | h:i A') }}</span>
#endif
</td>
<td>
Payment
<i class="fa fa-pencil"></i> Edit
{!! Form::open(['method' => 'DELETE', 'route' => ['userscourses.destroy', $purchase->id] ]) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-sm btn-danger btn-block mt-20']) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
</tbody>
</table>
Thanks.
You can use laravel Gate and policies features for achieving that, You can read the documentation once.

How to Add new tr in each table?

Can anyone please please Help me. I am using laarvel framework. I want to add tr when user click on ADD NEW Service Button. The data is displayed in foreach Loop.
when user click on Add New Service
here is my Html Code:-
enter code here
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="col-lg-8 col-md-8 col-sm-8 col-xs-12">
#foreach($alldata as $data)
<div class="services-name">
<h4>{{ $data['name'] }} <button id="{{ $data['id']}}" type="button" class="btn btn-default btn-nail getid" data-toggle="modal" data-target="#myModal-nail" >
<i class="fa fa-plus" aria-hidden="true"> Add New Service</i>
</button>
</h4>
</div>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="row">
<div class="table-class">
<table class="table table-bordered" id="append-{{ $data['id'] }}">
<tr>
<th style="width:33.33%">Services</th>
<th style="width:33.33%">Duration</th>
<th style="width:33.33%">£ Price </th>
</tr>
#foreach($data['sub_services'] as $subservices)
<tr>
<td>{{ $subservices['name']}}</td>
<input type="hidden" name="service_id[]" value="{{ $subservices['service_id'] }}">
<input type="hidden" name="name[]" value="{{ $subservices['name'] }}">
<input type="hidden" name="service_is[]" value="{{ $subservices['service_is'] }}">
<td>
<div class="form-group">
<input class="form-control" type="text" name="duration[]" placeholder="Duration">
</div>
</td>
<td>
<div class="form-group">
<input class="form-control" type="text" name="price[]" placeholder="price">
</div>
</td>
</tr>
#endforeach
</table>
</div>
</div>
</div>
#endforeach
</div>
And here is My modal
enter code here
<div class="modal fade" id="myModal-nail" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="add-cat"> Add Service</h4>
</div>
<form class="servicedata" role="form" method="POST" action="Javascript:;">
{{ csrf_field() }}
<div class="modal-body">
<div class="form-group row">
<span class="col-xs-3 add-cate-model">Add Service</span>
<div class="col-xs-8">
<input name="name" class="form-control mdl-txt txtfield m-tb-10" type="text" placeholder="Add Service" >
<input type="hidden" name="list_id" value="{{ $listid }}">
<input type="hidden" class="service" name="service_id" value="">
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary addService">Submit</a>
</div>
</form>
</div>
</div>
Jquery Code:-
enter code here
$(document).on('click','.getid', function(){
var serviceid = $(this).attr('id');
alert(serviceid);
$('.addService').addClass(serviceid);
$('.'+serviceid).on('click',function(){
$('.service').val(serviceid);
if(flag==1) {
$.ajax({
type: "POST",
url: "add-category-service",
data: $('.servicedata').serialize(),
success:function(resp){
if($.trim(resp)){
$("#append-"+serviceid).append(resp);
$('.addService').removeClass(serviceid);
$('#myModal-nail').modal('hide');
} else{
alert("error"); return false;
}
}
});
}
});
});
And My laravel function:-
enter code here
ublic function addCategoryService(Request $request){
if($request->ajax()){
$data = $request->input();
//echo "<pre>"; print_r($data); die;
unset($data['_token']);
$subservice = new SessionSubService;
$subservice->name = $data['name'];
$subservice->list_id = 1;
$subservice->service_id = $data['service_id'];
$subservice->service_is = "top";
$subservice->save();
echo '<tr>
<td>'. $data['name'].'</td>
<input type=hidden name=service_id[] value=' .$data['service_id'].'>
<input type=hidden name=name[] value=' . $data['name']. '>
<input type=hidden name=service_is[] value=top>
<td>
<div class=form-group>
<input class="form-control" type="text" name="duration[]" placeholder="Duration">
</div>
</td>
<td>
<div class=form-group>
<input class=form-control type=text name=price[] placeholder=price>
</div>
</td>
</tr>'; die;
}
}

Categories