Laravel blade table to AJAX - ajax

Hi I have users with many receipts and partners. I need one table like this.
ID | Receipt Number | Username | Receipt Date | Partner name| Partner Phone | Operations
Here is my controller. User has relationship with receipts and partners they returns result with i need.
public function index()
{
$user = Auth::id();
$datas = Operation::with('user','receipt', 'partner')->where('user_id', $user)->get();
return view('operations.index', compact('datas'));
}
Here is view
<table class="datatables-basic table" id="example">
<thead>
<tr>
<th>#</th>
<th></th>
<th>User</th>
<th>Date</th>
<th>Partner</th>
<th>Phone</th>
<th>Amallar</th>
</tr>
</thead>
<tbody>
#foreach($datas as $idx => $data)
<tr>
<td>{{$idx+1}}</td>
<td>{{$data->receipt->number}}</td>
<td>{{$data->user->username}}</td>
<td>{{$data->receipt->date}}</td>
<td>{{$data->partner->username}}</td>
<td>{{$data->partner->phone}}</td>
<td>
<a href="#" class="btn btn-icon btn-flat-primary">
<i class="fas fa-edit"></i>
Edit
</a>
<a href="#" class="btn btn-icon btn-flat-primary">
<i class="fas fa-view"></i>
View
</a>
<a href="#" class="btn btn-icon btn-flat-primary">
<i class="fas fa-trash"></i>
Delete
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
Problem is {{$data->receipt->number}}, {{$data->receipt->date}}, {{$data->partner->username}} says "Property [date] does not exist on this collection".
And second problem how to convert this table to AJAX?
Here is Operation table
id| user_id | parner_id | receipt_id | date | amount | price
Here is receipt table
id | date | type | user_id
Thanks

Related

from 2 modals, open one modal base on if else condition

I have 2 modals (Modal1 & Modal2), in table after clicked on action button "edit" base on data, i need to open any one modal.
Say if age is less than 36 months then Modal 1 Else Modal 2 should open. Both modals have few data common and few are added newly.
This is my table code
<table id="cowcalfsummery" class="table table-bordered table-hover table" style="width:100%">
<thead>
<tr>
<th>ID</th>
<th>Breed Type</th>
<th>Gender</th>
<th>Birth Date</th>
<th>Weight (Kg)</th>
<th>Age (in Months)</th>
<th>Maturity Date</th>
<th>Health Status</th>
<th>Action</th>
</tr>
<tbody>
#foreach ($cowcalfdata as $item )
<tr id="item{{$item->id}}">
<td>{{$item->calfID}}</td>
<td>{{$item->breedtype}}</td>
<td>{{$item->gendertype}}</td>
<td>{{ date('d-m-Y', strtotime ($item->birthdate) ) }}</td>
<td>{{$item->weight}}</td>
<td>{{$item->age}}</td>
<td>{{ date('d-m-Y', strtotime ($item->maturitydate) ) }}</td>
<td>{{$item->health}}</td>
<td>
<i class="fas fa-eye" title="View"></i>
**<button type="button"** class="editcowcalfmodal btn btn-success" data-info="{{$item->id}},{{$item->calfID}},{{$item->birthdate}},{{$item->age}},{{$item->breedtype}},{{$item->gendertype}},{{$item->health}},{{$item->status}},{{$item->color}},{{$item->weight}},{{$item->height}},{{$item->previousvaccinedone}},{{$item->maturitydate}}" data-toggle="modal" data-target="#editcowcalfmodal"><i class="far fa-edit" title="Edit"></i></button>
<button type="button" class="btn btn-danger deletecowcalfdatabtn" data-cowcalfid="{{$item->calfID}}"><i class="fas fa-trash" title="Delete"></i></button>
</td>
</tr>
#endforeach
</tbody>
</thead>
</table>
Thanks in Advance.
You want to create a blade If statement and check if $item->age is below 36.
if it is you want to show the first button, else show the second button with a different data-target to your second modal.
#if($item->age < 36)
<button type="button"
class="editcowcalfmodal btn btn-success"
data-info="
{{$item->id}},
{{$item->calfID}},
{{$item->birthdate}},
{{$item->age}},
{{$item->breedtype}},
{{$item->gendertype}},
{{$item->health}},
{{$item->status}},
{{$item->color}},
{{$item->weight}},
{{$item->height}},
{{$item->previousvaccinedone}},
{{$item->maturitydate}}"
data-toggle="modal"
data-target="#editcowcalfmodal">
<i class="far fa-edit" title="Edit"></i>
</button>
#else
<button type="button"
class="editcowcalfmodal btn btn-success"
data-info="
{{$item->id}},
{{$item->calfID}},
{{$item->birthdate}},
{{$item->age}},
{{$item->breedtype}},
{{$item->gendertype}},
{{$item->health}},
{{$item->status}},
{{$item->color}},
{{$item->weight}},
{{$item->height}},
{{$item->previousvaccinedone}},
{{$item->maturitydate}}"
data-toggle="modal"
data-target="#MySecondModal">
<i class="far fa-edit" title="Edit"></i>
</button>
#endif
You also have your table wrong.
You first want to create a <thead> (table header). fill it with table titles, close the <thead> and after that you create the <tbody> (table body) with the content of the item.
This should create a structure like this:
<table>
<thead>
<tr>
<th>Age</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>12</td>
<td>Cow 1</td>
</tr>
</tbody>
</table>
More information about tables and how to use them

How to use if else statement to check that table has this value

This is my member's light table
0 mean no light, 1 mean light on
|id| sx_ID | gylight|
---------------------
|1 |SX00001| 0 |
|2 |SX00002| 1 |
|3 |SX00003| 1 |
---------------------
This is my guanyin table
|id| sx_ID | gylight_number|
----------------------------
|1 |SX00002| A1-1 |
|2 |SX00002| A1-17 |
|3 |SX00003| A78 |
----------------------------
It have the data in the table, but it still display the msg 'No data'
This is my view.blade.php
<thead>
<th></th>
<th>Receipt</th>
<th>Amount</th>
<th>Number</th>
<th>Label</th>
<th>Remark</th>
<th>Start Date</th>
<th>Due Date</th>
<th>CreatedBy</th>
<th>UpdatedBy</th>
</thead>
<tbody id="gyFee">
#forelse ($gylight as $item)
#if($item->sx_ID == $light->sx_ID )
<tr id="gyPost_{{$item->id}}">
<td id="btncz">
#if($permission->role_id == 1)
<a href="#"
data-id="{{$item->id}}"
data-gylight_receipt="{{$item->gylight_receipt}}"
data-gylight_amount="{{$item->gylight_amount}}"
data-gylight_number="{{$item->gylight_number}}"
data-gylight_label="{{$item->gylight_label}}"
data-gylight_remark="{{$item->gylight_remark}}"
data-gylight_sdate_string="{{$item->gylight_sdate_string}}"
data-gylight_edate_string="{{$item->gylight_edate_string}}"
class="btn btn-success gyEdit"
data-toggle="modal">Edit</a>
#else
Edit
#endif
</td>
<td id="receipt">{{ $item->gylight_receipt }}</td>
<td id="amount">RM{{ $item->gylight_amount }}</td>
<td id="number">{{ $item->gylight_number }}</td>
<td id="label">{{ $item->gylight_label }}</td>
<td id="remark">{{ $item->gylight_remark }}</td>
<td id="sdate">{{ $item->gylight_sdate_string }}</td>
<td id="edate">{{ $item->gylight_edate_string }}</td>
<td id="created">{{ $item->created_by }}</td>
<td id="updated">{{ $item->updated_by }}</td>
</tr>
#endif
#empty
#endforelse
</tbody>
<tfoot>
<tr>
#if($light->sx_ID == $gylight->first()->sx_ID)
<td><button class="btn btn-primary" data-toggle="modal" data-target="#guanyinModal">Add</button></td>
<td style="text-align:center;vertical-align:middle" colspan="7">
</td>
#else
<td><button class="btn btn-primary" data-toggle="modal" data-target="#guanyinModal">Add</button></td>
<td id="lastgy" style="text-align:center;vertical-align:middle" colspan="7">
No data
</td>
#endif
</tr>
</tfoot>
This is my view controller
public function view(Light $light){
$gylight = Guanyin::latest()->first()->orderBy('id','desc')->get();
$permission = Auth::user();
return view('admin.light.view',[
'light' => $light,
'gylight' => $gylight,
'permission' =>$permission,
]);
}
First in your controller,
$gylight = Guanyin::latest()->first();
this will return the latest w.r.t created_at column, & first will fetch only one value. you need not use ->orderBy('id','desc')->get(); again.
If you want to get orderby descending to id, you can use other variable for that say,
$gylightOrderById = Guanyin::orderBy('id','desc')->get();
& use it separately.
So, for your #forelse ($gylight as $item) would be changed to #forelse ($gylightOrderById as $item)
&
you can use #if($light->sx_ID == $gylight->sx_ID) for later comparison.

print the last 5 articles in the dashboard

I would like to know how to put a list of the last 5 items in the dashboard using backpack?
I made the table
<div class="table-responsive">
<table class="table table-hover m-0 table-actions-bar">
<thead>
<tr>
<th>
<div class="btn-group dropdown">
<button type="button" class="btn btn-light btn-xs dropdown-toggle waves-effect waves-light"
data-toggle="dropdown" aria-expanded="false"><i class="mdi mdi-chevron-down"></i></button>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">Dropdown link</a>
<a class="dropdown-item" href="#">Dropdown link</a>
</div>
</div>
</th>
<th>Titolo Post</th>
<th>Data</th>
<th>Stato</th>
<th>Categria</th>
<th>Azione</th>
</tr>
</thead>
<tbody>
<tr>
<td>
</td>
<td>
<h5 class="m-0 font-weight-medium"></h5>
</td>
<td>
<i class="mdi mdi-map-marker text-primary"></i>
</td>
<td>
<i class="mdi mdi-clock-outline text-success"></i>
</td>
<td>
<i class="mdi mdi-currency-usd text-warning"></i>
</td>
<td>
<i class="mdi mdi-pencil"></i>
<i class="mdi mdi-close"></i>
</td>
</tr>
</tbody>
</table>
</div>
I made the table inside the jumbotron.blade.php then the function that prints you on screen the last 5 posts I put it here? within the dashboard method?
$recentPost : Article::orderBy('id', 'desc')>limit(5)->get()
any other solution?
This line will get you the last 5 articles.
$articles = Article::orderBy('id', 'desc')->limit(5)->get();
You need to pass it to the view. Ex:
return view('dashboard', ['articles' => $articles]);
And loop the articles on the blade file table. Ex:
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Title</th>
<th scope="col">Author</th>
<th scope="col">Date</th>
</tr>
</thead>
<tbody>
#foreach($articles as $article)
<tr>
<th scope="row">{{ $article->id }}</th>
<td>{{ $article->title }}</td>
<td>{{ $article->author }}</td>
<td>{{ $article->created_at }}</td>
</tr>
#endforeach
</tbody>
</table>

Laravel eloquent delete

I am looking for some assistance on how to understand Laravel Eloquent model.
I need to delete User by Id, and I have tables Main_info, Personal_info, Holidays, other_info, fm_day, oth_daysoff.
On the tables, I have inserted UserId field, so I need to delete selected UserId from all of the tables? May someone help me on how I can do this with Laravel Eloquent model?
Here is my view with delete submit button and post method:
<div class="container">
<div class="row">
<div class="col">
<div class="card">
<div class="card-head-man">
<span>Filter type: </span>
Position
Active/No
Employment type
Location
</div>
</div>
</div>
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title">List of employees</h3>
</div>
<div class="table-responsive">
<table class="table card-table table-vcenter text-nowrap">
<thead>
<tr>
<th class="w-1">ID<span class="caret"></span></th>
<th>First Name<span class="caret"></span></th>
<th>Last Name</th>
<th>Position</th>
<th>Active/no</th>
<th>Employment type</th>
<th>Location</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
#foreach($allData as $data)
<tr>
<td><span class="text-muted">{{ $data->id }}</span></td>
<td>{{ $data->first_name }}</td>
<td>
{{ $data->last_name }}
</td>
<td>
{{ $data->position }}
</td>
<td>
{{ $data->employment_type }}
</td>
<td>
<span class="status-icon bg-success"></span> Active
<span class="status-icon bg-danger"></span> No
</td>
<td>{{ $data->location }}</td>
<td class="text-right">
Manage
<div class="dropdown">
<form action="/deleteFromHome" method="post">
{{csrf_field()}}
<input style="display: none;" type="text" name="deleteUserId" class="form-control" id="inputName3" value="{{ $data->id }}">
<button type="submit" class="btn btn-secondary btn-sm">Delete</button>
</form>
</div>
</td>
<td>
<a class="icon" href="javascript:void(0)">
<i class="fe fe-edit"></i>
</a>
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
Here is how my controller doing it now:
public function deleteUserFromHome(Request $req)
{
$userId = $req->input('deleteUserId');
OtherInfo::where('id', $userId)->delete();
OthDaysInfo::where('id', $userId)->delete();
HolidaysInfo::where('id', $userId)->delete();
FMdaysInfo::where('id', $userId)->delete();
PersonalInfo::where('id', $userId)->delete();
PersonalInfo::where('id', $userId)->delete();
MainInfo::where('id', $userId)->delete();
return redirect('home');
}
Maybe there is some better way to delete all data from all tables about selected userId?
I need some example how I can make Laravel Eloquent model to delete with function HasMany or some else?
You can use ->onDelete('cascade')when you create your table migrations.
For instance,
// consider a sample migration below for a table with a foreign key on your users table
Schema::create('main_info', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->timestamps();
});
And then you can easily just delete the specified user, and Laravel will take care of deleting it's child records.
$user = User::find($id);
$user->delete(); // this will also delete any record for this user on main_info table
Read more here

pagination in laravel 5.3 when count of pages = 1

I have cities table and it has 7 cities I have a view page to display these cities with 10 cities per page
Controller:
$cities = City::orderBy('id', 'desc')->paginate(10);
return view('cities.home', compact('cities'));
View:
<div class="table-responsive panel panel-sky">
<table class="table table-striped table-hover">
<tr>
<th>#</th>
<th>Name</th>
<th>Created At</th>
<th>Action</th>
</tr>
#foreach($cities as $city)
<tr id="product{{$city->id}}">
<td>{{$city->id}}</td>
<td>{{$city->name}}</td>
<td>{{ $city->created_at }}</td>
<td>
<i class="glyphicon glyphicon-edit action-icon"></i>
<a type="button" class="pointer" data-toggle="modal" data-target="#deleteModal" data-type="btn-danger" data-action="delete-product" data-id="{{ $city->id }}" data-msg="Are you sure you want to delete this city?" data-title="Confirm Deletion">
<span class='glyphicon glyphicon-remove action-icon'></span>
</a>
</td>
</tr>
#endforeach
</table>
<div>
<div class="col-md-12"><?php echo $cities->render(); ?> </div>
but the issue is the paginate render is displayed with page#1, this issue occurred in laravel5.3 and did not find in laravel5.2
If you want to render pagination in your own way, you can use paginator methods. For example, to check if there is just one page, you could try:
#if ($cities->total() > 10)
// Render pagination
#endif

Resources