Laravel Ajax updating variable - laravel-5

<div class="panel panel-primary">
<div class="panel-heading">Add user Status</div>
<form action="{{ route('post.create') }}" method="post">
<div class="panel-body">
<div class="form-group">
<textarea class="form-control" name="body" id="new-post" rows="5" placeholder="Your Post"></textarea>
</div>
</div>
<div class="panel-footer clearfix">
<button class="btn btn-info pull-right"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></i> Post</button>
</div>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
</div>
<div id="main" class="main">
<header><h3>What other people say...</h3></header>
#foreach($top_15_post as $status)
{!!
view ('layouts.app-internal.user-status-layaout',[
'status' =>$status,
'user' =>\App\User::find($status->user_id),
'comments'=>\App\Comment::where('status_id',$status->id)->get(),
'countcomment'=>\App\Comment::where('status_id',$status->id)->count(),
'countlike'=>\App\Like::where('like',1)->where('post_id',$status->id)->count() ,
'countdislike'=>\App\Like::where('like',0)->where('post_id',$status->id)->count(),
])->render()
!!}
#endforeach
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Right part -->
</div>
</div>
</div>
This is my home.blade.php in laravel. im simply implementing a posting text system which user can comment and like to the posts.I'm passing above variables to the user-status-layaout.blade.php, it shows the comments and likes as well as the posts.
This is my user-status-layaout.blade.php.My like and dislike functions are working properly. but i want update like count using ajax.How do i update countlike and countdislike variable instantly using ajax?
<div class="panel panel-default">
<div class="panel-heading">{{ $status->user->name }} Posted on {{ $status->created_at }}</div>
<div class="panel-body">
<div class="row">
<div class="col-md-1">
<img src="{{Auth::user()->getavatar()}}" class="img-responsive">
</div>
<div class="post" data-postid="{{ $status->id }}">
<p>{{ $status->body }}</p>
<div class="interaction">
{{ Auth::user()->likes()->where('post_id', $status->id)->first() ? Auth::user()->likes()->where('post_id', $status->id)->first()->like == 1 ? 'You like this post' : 'Like' : 'Like' }} |
{{ Auth::user()->likes()->where('post_id', $status->id)->first() ? Auth::user()->likes()->where('post_id', $status->id)->first()->like == 0 ? 'You don\'t like this post' : 'Dislike' : 'Dislike' }}
#if(Auth::user() == $status->user)
|
Edit |
<form method="get" action="{{ route('post.delete', ['post_id' => $status->id]) }}" accept-charset="UTF-8" style="display:inline">
<button class="btn btn-xs btn-danger" type="button" data-toggle="modal" data-target="#confirmDelete" data-title="Delete Post" data-message="Are you sure you want to delete this post ?">
<i class="glyphicon glyphicon-trash"></i> Delete
</button>
</form>
#endif
<ul class="list-unstyled list-inline ">
<li>
<button class="btn btn-xs btn-info" type="button" data-toggle="collapse" data-target="#view-comments-{{$status->id}}" aria-expanded="false" aria-controls="collapseExample">
<i class="fa fa-comments"></i>View & Comment </button>
</li>
<li>{{$countcomment}} comments</li>
<li >{{$countlike}} likes</li>
<li>{{$countdislike}} dislikes</li>
</ul>
</div>
</div>
</div>
</div>
<div class="panel-footer clearfix">
<div class="form-group">
<form action="{{ route('comment.create') }}" method="post">
<div class="input-group">
<input type="text" class="form-control" name="comment-text" id="comment-text" placeholder="Add Comment">
<span class="input-group-btn">
<button class="btn btn-info btn-xs" type="submit"><span class="glyphicon glyphicon-plus"></span>Add </button>
</span>
<input type="hidden" value="{{ $status->id }}" name="id" id="id">
</div><!-- /input-group -->
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
</div>
<div class="collapse" id="view-comments-{{$status->id}}">
#if($comments->first())
#foreach($comments as $comment)
<blockquote >
<div class="row" >
<div class="col-md-1" >
<img src="{{\App\User::find($comment->user_id)->getavatar()}}" class="img-responsive">
</div>
<div class="col-md-11">
<ul class=" list-inline list-unstyled ">
<li>
{{\App\User::find($comment->user_id)->name}}
</li>
</ul>
<div class="comment" data-commentid="{{ $comment->id }}">
<p> {{$comment->comment_text}}</p>
<div class="interaction1">
#if(Auth::user() == $comment->user)
Edit |
<form method="get" action="{{ route('comment.delete', ['comment_id' => $comment->id]) }}" accept-charset="UTF-8" style="display:inline">
<button class="btn btn-xs btn-danger" type="button" data-toggle="modal" data-target="#confirmDeleteComment" data-title="Delete Comment" data-message="Are you sure you want to delete this comment ?">
<i class="glyphicon glyphicon-trash"></i> Delete
</button>
</form>
#endif
</div>
</div>
</div>
</div>
<p> posted {{$comment->created_at->diffForHumans()}}</p>
</blockquote>
#endforeach
#else
<p>This status no comment</p>
#endif
</div>
</div>

You could create a timer function using WindowTimers.setTimeout()
$('some-selector').load(function(){
function likeTimer() {
setTimeout(function(){
// Some JSON request that updates the elements' values.
likeTimer();
}, 1000);
}
});
Or you could poll the server periodically with WindowTimers.setInterval()
$('some-selector').load(function(){
setInterval(function() {
// Some JSON request that updates the elements' values.
}, 1000);
});
Then just define a route in Laravel that returns JSON for the data you want to update.

Related

Laravel post from modal tries to GET instead of POSt

The modal gets data from the #foreach and displays them. And from the form it posts the data entered to the next POST route {{ route('user.give') }}. However, it keeps trying to perform a GET request to a different route(precisely the route just above it). Please help, thank you for helping
#section('content')
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
#if(count($orgs) > 0)
#foreach($orgs as $org)
<div class="card m-1 col-md-3 p-0">
<img class="card-img-top" src="{{ asset('storage/'.$org->logo) }}" alt="{{ $org->name }} logo" />
<div class="card-body">
<span class="card-title"><strong><i class="fa fa-church"></i> {{ $org->name }}</strong></span>
<div class="small"><i class="fa fa-exchange-alt"></i> {{ $org->alias }}</div>
<p class="card-text"></p>
<button class="btn btn-outline-primary rounded-0" data-toggle="modal" data-target="#exampleModalCenter">
<i class="fa fa-credit-card"></i>
Give
</button>
<i class="fa fa-cogs"></i>
</div>
</div>
#endforeach
#else
<p>Sorry, there's no match for your search.</p>
#endif
and right below is the modal. It is actually continuation of the code above. The modal actually comes up, the issue only occurs at the point of posting
<!-- form modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<cite>You are about to give to {{ $org->name }}</cite>
<!-- form -->
<form method="POST" action="{{ route('user.give') }}">
#csrf
<input type="hidden" name="organisation_name" value="{{ $org->name }}">
<input type="hidden" name="organisation_id" value="{{ $org->id }}">
<input type="hidden" name="organisation_email" value="{{ $org->email }}">
<input type="hidden" name="organisation_logo" value="{{ $org->logo }}">
<div class="form-group">
<label for="amount">Amount</label>
<input type="number" class="form-control rounded-0" name="amount" aria-describedby="amount" placeholder="e.g 5000" required>
</div>
<div class="form-group">
<label for="purpose">Purpose</label>
<input type="text" class="form-control rounded-0" name="purpose" placeholder="e.g tithe">
</div>
<button type="submit" class="btn btn-outline border-dark text-dark rounded-0" id="proceed">
Proceed
</button>
</form>
<!-- //form -->
</div>
<div class="modal-footer">
<button type="button" class="btn btn-outline border-dark text-dark rounded-0" data-dismiss="modal">Close</button>
<!-- <button type="button" class="btn btn-primary">Save changes</button> -->
</div>
</div>
</div>
</div>
<!-- //form modal -->
#endsection
Here are my routes
Route::middleware('auth')->group(function (){
Route::post('/user/search', 'HomeController#search')->name('user.search');
Route::post('/user/give/', 'HomeController#give')->name('user.give');
Route::get('/user/verify/{reference}', 'HomeController#verify')->name('user.verify');
It visits user.search instead of user.give and it does that with a GET
$org is not what you want in your modal: it is the last $org from your loop over your $orgs array since it is not inside the #foreach loop.
You need to generate your modal dynamically (through javaScript or other) when you click the button showing the modal.
Found it. Apparently I added a validation rule that was failing when the form was submitted. Since the validation error could not be sent to the modal it was performing a GET.
Breathing fresh air now. Thanks for your support y’all

Laravel 5.5 - MethodNotAllowedHttpException

I have an task using Laravel. Which one on my task, user can edit the data on master data table. I'm using modal pop when user want to edit the data. So the user no need to move the page for edit data. On my task I'm using Laravel 5.5
Here is my code :
This is for tabel modal edit
<div class="modal fade" id="edit-modal" tabindex="-1" role="dialog" aria-labelledby="largeModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header no-bd">
<h5 class="modal-title">
<span class="fw-mediumbold">Detail</span>
<span class="fw-light">Data</span>
</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form class="form-material form-horizontal" method=post action="{{route('masterdataobjek.update', 'test')}}">
{{ csrf_field() }}
<div class="row">
<div class="col-sm-12">
<input type="hidden" name="id_objek" id="id_objek_modal" value="">
<div class="form-group form-group-default">
<label>Nama Objek</label>
<input id="objek_nama_modal" type="text" name="objek_nama_modal" class="form-control">
</div>
</div>
<div class="col-sm-12">
<div class="form-group form-group-default">
<label for="comment">Deskripsi Objek</label>
<textarea class="form-control" name="objek_desc_modal" id="objek_desc_modal" rows="5"></textarea>
</div>
</div>
<div class="col-sm-12">
<label for="exampleFormControlFile1">Upload WTO File</label>
<div class="form-group form-group-default">
<input type="file" class="form-control-file" name="objek_wto" id="objek_wto">
</div>
</div>
</div>
<div class="modal-footer no-bd">
<button type="submit" class="btn btn-primary">Update</button>
<button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
</div>
For the button clicked code ;
<tbody>
#foreach ($tabelobjek as $ta => $data)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $data->objek_nama}}</td>
<td style="">{{str_limit($data->objek_desc,15)}}</td>
<td>
<div class="form-button-action">
<button type="button" data-toggle="modal"
class="btn btn-link btn-success btn-lg" data-original-title="Edit"
data-target="#edit-modal" id="edit-button"
data-idobjek="{{$data->id_objek}}"
data-namaobjek="{{$data->objek_nama}}"
data-objekdeskripsi="{{$data->objek_desc}}">
<i class="fa fa-edit"></i>
</button>
</div>
</td>
</tr>
#endforeach
</tbody>
for trigger button :
<script>
$(document).ready(function () {
$(document).on('click', '#edit-button', function () {
var namaobjek = $(this).data('namaobjek');
var objekdeskripsi = $(this).data('objekdeskripsi');
var idobjek = $(this).data(idobjek)
$('#id_objek_modal').val(idobjek)
$('#objek_nama_modal').val(namaobjek);
$('#objek_desc_modal').val(objekdeskripsi);
})
})
MasterDataObjekController.php
public function update(Request $request)
{
$updateObjek = TbObjek::findOrFail($request->id_objek);
$updateObjek->update($request->all());
return back();
}
POST requests are for creating new records, PUT and PATCH are for updating records. Refer to the table of HTTP methods on Laravel RESTful containers here
You need to change your form tag from this
<form class="form-material form-horizontal" method=post action="{{route('masterdataobjek.update', 'test')}}">
to this
<form class="form-material form-horizontal" method="put" action="{{route('masterdataobjek.update', 'test')}}">

Laravel ajax store request errormessage, data append to view

I'm new to ajax and I have two little problems.
With my code I can write the data in the database. I get no error messages and the data are not synonymous directly loaded into the view.
how can I load the data directly into the view without reload the page?
if the privacy is 1, the code should be displayed in the right column, and if the privacy is 0, the code should be displayed in the left column.
How can I output the error messages with my errormessage code?
view
<div id="content" class="dashboard padding-10">
<div class="row">
<div class="col-md-offset-3 col-md-6">
<a data-toggle="modal" data-target=".todolist-create-modal" class="btn btn-success btn-block btn-sm margin-bottom-10">Neue Liste erstellen</a>
</div>
<div class="col-md-6">
<div id="panel-misc-portlet-l3" class="panel panel-default text-center">
<div class="panel-heading nohover">
<span class="elipsis">
<strong>Private Tasks</strong>
</span>
</div>
</div>
<div class="alert alert-danger margin-bottom-30 {{ $todolistpublic->count() ? 'hidden' : '' }}">
Es wurden keine <strong>Einträge</strong> gefunden.
</div>
#foreach ($todolistpublic as $list)
<div id="todo-list-{{$list->id}}" class="panel panel-default panel-primary margin-bottom-0">
<div class="panel-heading panel-pointer">
<span class="elipsis"><!-- panel title -->
<strong>{{ $list->title }}</strong> <span class="label label-info white">0</span>
</span>
<ul class="options pull-right relative list-unstyled hover-visible">
<li><a data-toggle="modal" data-target=".task-modal" class="btn btn-success btn-xs white hover-hidden">
<i class="fa fa-plus"></i> Erstellen
</a>
</li>
<li><a data-toggle="modal" data-target=".todolist-modal" data-id="{{ $list->id }}" data-title="{{ $list->title }}" data-description="{{ $list->description }}" class="btn btn-info btn-xs white hover-hidden">
<i class="fa fa-edit"></i> Bearbeiten
</a>
</li>
<li><a data-toggle="modal" data-target=".todolist-delete-modal" data-id="{{ $list->id }}" data-title="{{ $list->title }}" data-description="{{ $list->description }}" class="btn btn-danger btn-xs white hover-hidden">
<i class="fa fa-times"></i> Löschen
</a>
</li>
<li></li>
</ul>
</div>
<div class="panel-body">
<div class="slimscroll" data-always-visible="false" data-rail-visible="false" data-railOpacity="1" data-height="100">
{{ $list->description }}
</div>
</div>
</div>
#endforeach
<div class="panel-footer mtm-10">
<span id="todo-list-counter-public">{{ $todolistpublic->count() }}</span> <span>{{ $todolistpublic->count() > 1? 'Listen' : 'Liste' }}</span>
</div>
</div>
<div class="col-md-6">
<div id="panel-misc-portlet-l3" class="panel panel-default text-center">
<div class="panel-heading nohover">
<span class="elipsis">
<strong>Öffentliche Tasks</strong>
</span>
</div>
</div>
<div class="alert alert-danger margin-bottom-30 {{ $todolistprivate->count() ? 'hidden' : '' }}">
Es wurden keine <strong>Einträge</strong> gefunden.
</div>
#foreach ($todolistprivate as $list)
<div id="todo-list-{{$list->id}}" class="panel panel-default panel-primary margin-bottom-0">
<div class="panel-heading panel-pointer">
<span class="elipsis"><!-- panel title -->
<strong>{{ $list->title }}</strong> <span class="label label-info white">0</span>
</span>
<ul class="options pull-right relative list-unstyled hover-visible">
<li><a data-toggle="modal" data-target=".task-modal" class="btn btn-success btn-xs white hover-hidden"><i class="fa fa-plus"></i> Erstellen</a></li>
<li><a data-toggle="modal" data-target=".todolist-modal" class="btn btn-info btn-xs white hover-hidden"><i class="fa fa-edit"></i> Bearbeiten</a></li>
<li><i class="fa fa-times"></i> Löschen</li>
<li></li>
</ul>
</div>
<div class="panel-body">
<div class="slimscroll" data-always-visible="false" data-rail-visible="false" data-railOpacity="1" data-height="100">
{{ $list->description }}
</div>
</div>
</div>
#endforeach
<div class="panel-footer mtm-10">
<span id="todo-list-counter-private">{{ $todolistprivate->count() }}</span> <span>{{ $todolistprivate->count() > 1? 'Listen' : 'Liste' }}</span>
</div>
</div>
#include('elements.addTodoList')
#include('elements.createTodoList')
#include('elements.addTask')
</div>
</div>
$(document).ready(function () {
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');
$('#add-todo-list').click(function(e) {
e.preventDefault();
var _token = $("input[name='_token']").val(); // get csrf field.
var title = $("input[name='title']").val();
var description = $("textarea[name='description']").val();
var privacy = $("select[name='privacy']").val();
$.ajax({
url:'{{ route('todolists.store') }}',
type: 'POST',
data: {_token:_token, title:title, description:description, privacy:privacy},
success: function (data) {
console.log(data);
}
});
});
});
Controller
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'title' => 'required|min:5',
'description' => 'required|min:10',
'privacy' => 'required|integer'
]);
$attributeNames = array(
'title' => 'Title',
'description' => 'Description',
);
$validator->setAttributeNames($attributeNames);
//Redirect back if validation fails
if($validator->fails()) {
return response()->json(['error'=>$validator->errors()->all()]);
}
else{
$todolists = new Todolists;
$todolists->admin_id = auth::user();
$todolists->title = $request->title;
$todolists->description = $request->description;
$todolists->privacy = $request->privacy;
$todolists->save();
return response()->json(['success'=>'Your enquiry has been successfully submitted!']);
}
}
modal
<div class="modal fade todolist-create-modal" tabindex="-1" role="dialog">
<div class="modal-dialog">
<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="modal-title">Erstelle eine neue Liste</h4>
</div>
<div class="modal-body">
<form action="{{ route('todolists.store') }}" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="" class="control-label">Titelname</label>
<div class="fancy-form">
<i class="fa fa-header"></i>
<input id="title" name="title" type="text" class="form-control input-lg" placeholder="Titel">
</div>
</div>
<div class="form-group">
<label for="" class="control-label">Beschreibung</label>
<div class="fancy-form">
<textarea id="description" name="description" rows="2" class="form-control" placeholder="Beschreibe deine Aufgabe"></textarea>
<i class="fa fa-comments"><!-- icon --></i>
</div>
</div>
<div class="form-group">
<label for="" class="control-label">Privatsphäre</label>
<div class="fancy-form fancy-form-select">
<select id="privacy" name="privacy" class="form-control">
<option selected value="0">Öffentlich</option>
<option value="1">Privat</option>
</select>
<i class="fancy-arrow"></i>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-success" id="add-todo-list">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
errormessages
#if (count($errors) > 0)
<div class="toastr-notify"></div>
#foreach ($errors -> all() as $e)
<script type="text/javascript">
_toastr("{{$e}}","top-right","error",false);
</script>
#endforeach
#endif
#if (session('fail'))
<div class="toastr-notify"></div>
<script type="text/javascript">
_toastr("{{ session('fail') }}","top-full-width","warning",false);
</script>
#endif
#if (session('status'))
<div class="toastr-notify"></div>
<script type="text/javascript">
_toastr("{{ session('status') }}","top-full-width","info",false);
</script>
#endif
#if (session('success'))
<div class="btn btn-info toastr-notify"></div>
<script type="text/javascript">
_toastr("{{ session('success') }}","top-full-width","success", false);
</script>
#endif
#if (session('error'))
<div class="toastr-notify"></div>
<script type="text/javascript">
_toastr("{{ session('error') }}","top-full-width","error",false);
</script>
#endif
I believe you have a typo in your ajax backend code; this line to be specific:
$todolists->admin_id = auth::user();
This should be:
$todolists->admin_id = auth()->user()->id;
The typo is generating a Throwable error, which is getting caught by the laravel exception handler and it's detecting that the code is being executed under a ajax request and hence returning a 500 internal server error.

bootstrap-vue not passing array in checkbox

I'm using bootstrap-vue in my laravel project to pass data from the view to the database with checkboxes (b-form-checkbox), i want to select from the permissions passed from the database and assign it to a role, which means a role can have more than one permission, unfortunately the data is not persisting to the database as an array because if i select more than one in the checkbox it only shows the first one clicked. Please i need help as i have spent too much time on this issue. This is my code:
edit.blade.php
#extends('layouts.master')
#section('content')
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Admin
<small>Edit</small>
</h1>
<ol class="breadcrumb">
<li><i class="fa fa-dashboard"></i> Home</li>
<li>Role</li>
<li class="active">Edit</li>
</ol>
</section>
<!-- Main content -->
<section class="content">
#include('layouts.partials.message')
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header with-border">
<h3 class="boxtitle">Edit Role</h3>
</div>
<!-- /.box-header -->
<div class="box-body">
<form action="{{ route('role.update', ['id' => $role->id]) }}" enctype="multipart/form-data" method="post" accept-charset="utf-8">
{{csrf_field()}}
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" name="display_name" value="{{ old('display_name', $role->display_name) }}" class="form-control" placeholder="Name (Human Readable)" required>
<span class="help-block text-red">
#if($errors->has('display_name'))
{{ $errors->first('display_name')}}
#endif
</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" name="name" value="{{ old('name', $role->name) }}" class="form-control" placeholder="Slug (can not be edited)" disabled>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" name="description" value="{{ old('description', $role->description) }}" class="form-control" placeholder="Role Description">
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<h2>Permissions:</h2>
<b-form-group>
<b-form-checkbox-group v-model="permissionsSelected">
#foreach ($permissions as $permission)
<div class="form-group">
<b-form-checkbox id="permissions" name="permissions" value="{{ $permission->id }}">
<div class="form-group">
{{ $permission->display_name }} <em> ({{ $permission->description }})</em>
</div>
</b-form-checkbox>
</div>
#endforeach
</div>
</div>
<hr>
<div class="row">
<div class="col-md-1">
<div class="form-group">
<button class="btn btn-primary" type="submit" id="submit">
<i class="fa fa-check"></i> Submit
</button>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</div>
</div>
<div class="col-md-11">
<div class="form-group">
<div class="checkbox">
<label>
<input name="redirect" type="checkbox" checked> Redirect to role list after submission
</label>
</div>
</div>
</div>
</div>
<div class="clearfix"></div>
</form>
</div>
</div>
</div>
</div>
</section>
#endsection
#section('vue')
<script>
var app = new Vue ({
el: '#app',
data: {
permissionsSelected: {!!$role->permissions->pluck('id')!!}
}
});
</script>
#endsection

Update method laravel using modal box

My update method is working unfortunately it always updates the wrong data in this case it always updates the last data in my db list. I believe this occurs because my modal box directs to $user->id which always points to the last id as I have a for loop used at the top, is there a ways I could do to point it to the selected id instead?
view.blade.php
<div class="well col-xs-9 col-sm-9 col-md-9 col-lg-9 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1">
#foreach ($users as $user)
<div class="row user-row">
<!--div class="col-xs-3 col-sm-2 col-md-1 col-lg-1">
<img class="img-circle"
src="https://lh5.googleusercontent.com/-b0-k99FZlyE/AAAAAAAAAAI/AAAAAAAAAAA/eu7opA4byxI/photo.jpg?sz=50"
alt="User Pic">
</div-->
<div class="col-xs-2 col-sm-3 col-md-4 col-lg-4">
<h5 style="font-weight: bold">{{ $user->name }}</h5>
</div>
<div class="col-xs-8 col-sm-8 col-md-8 col-lg-8 dropdown-user" data-for=".{{ $user->id }}">
<h5 class="glyphicon glyphicon-chevron-down text-muted pull-right"> </h5>
</div>
</div>
<div class="row user-infos {{ $user->id }}">
<div class="col-xs-12 col-sm-12 col-md-10 col-lg-10 col-xs-offset-0 col-sm-offset-0 col-md-offset-1 col-lg-offset-1">
<div class="panel panel-info">
<div class="panel-heading">
<h2 class="panel-title">User Information</h2>
</div>
<div class="panel-body">
<div class="row">
<div class=" col-md-10 col-lg-10 hidden-xs hidden-sm">
<div class="col-xs-5">User level:</div><div class="col-xs-5"> {{ $user->role->role_description }}</div>
<div class="col-xs-5">Email:</div> <div class="col-xs-5"> {{ $user->email }}</div>
<div class="col-xs-5">Phone number: </div> <div class="col-xs-5"> {{ $user->mobile }} </div>
<div class="col-xs-5">Office extension: </div> <div class="col-xs-5"> [ TO IMPLEMENT ]</div>
</div>
</div>
</div>
<div class="panel-footer">
<button class="btn btn-sm btn-warning" type="button"
data-toggle="modal" data-target="#form">Edit <i class="glyphicon glyphicon-edit"></i></button>
<span class="pull-right">
<button class="btn btn-sm btn-danger" type="button">Inactive <i class="glyphicon glyphicon-remove"></i></button>
</span>
</div>
</div>
</div>
</div>
#endforeach
</div>
#if(Session::has('flash_message'))
<div class="alert alert-success col-xs-9 col-sm-9 col-md-9 col-lg-9 col-xs-offset-1 col-sm-offset-1 col-md-offset-1 col-lg-offset-1">
{{ Session::get('flash_message') }}
</div>
#endif
<div class="col-sm-offset-1 col-sm-2">
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-md" data-toggle="modal" data-target="#form">Register New User</button>
<!-- Modal -->
<div id="form" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">User Information</h4>
</div>
<div class="modal-body">
<form class="form-horizontal" role="form" action="/manage_accounts/{{ $user->id }}" novalidate>
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="form-group">
<label class="control-label col-sm-3" for="name">Username:</label>
<div class="col-sm-5 #if ($errors->has('name')) has-error #endif">
<input type="text" class="form-control" type="hidden" id="name" name="name" placeholder="Enter username">
#if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> #endif
</div>
</div>
...
Your modal is referencing the $user object, but it is outside of your foreach loop.
Specifically this line:
<form class="form-horizontal" role="form" action="/manage_accounts/{{ $user->id }}" novalidate>
You could register an onClick event for the modal pop-up button, that grabs a hidden input field of an user's id and dynamically updating the the action URL. Alternatively, you could just have the action URL be the same and handle the logic server side. This approach would have a hidden input field for the user ID that you would be updating, but is a lot cleaner that dealing with URL structure.
Edit:
Javascript Example:
<script type="text-javascript">
$(function() {
$('.btn--edit').on('click', function() {
var formAction = $('.form-horizontal').attr('action').replace(/(?!.*/).*, '');
var userId = $(this).closest('[name=user_id]').val();
$('.form-horizontal').attr('action', formAction + '/' + userId);
});
});
</script>
This requires you to update your modal button with a class name of .btn--edit
<button class="btn btn-sm btn-warning btn--edit" type="button" data-toggle="modal" data-target="#form">Edit <i class="glyphicon glyphicon-edit"></i></button>
This also requires you add a hidden input within the foreach somewhere. I chose after the user-infos class.
<input type="hidden" name="user_id" value="{{ $user->id }}" />
So here's how I got my update method to work.
<form class="form-horizontal" role="form" method="POST" action="/manage_accounts/" novalidate>
<input type="hidden" name="_method" value="PUT">
Javascript:
<script type="text/javascript">
$(function() {
$('.btn--edit').on('click', function(e) {
var userId = $(this).attr('data-for');
var formAction = "/manage_accounts/" + userId;
$('.form-horizontal').attr('action', formAction);
});
</script>
The modal button with class name of .btn--edit,
<button class="btn btn-sm btn-warning btn--edit" type="button"
data-toggle="modal" data-target="#update" data-for="{{$user->id}}">Edit <i class="glyphicon glyphicon-edit"></i></button>

Resources