I am trying to use controller's update function on my dashboard.blade.php page, but when I press on "Save" it switches to show.blade.php page instead of updating.
My dashboard.blade.php page:
#if(count($posts) > 0 )
<table class="table table-striped">
<tr>
<th>Title</th>
<th>Body</th>
<th>Employee Number</th>
<th>Edit</th>
<th>Save</th>
</tr>
#foreach($posts as $post)
<tr>
<td>{{$post->title}}</td>
<td><input type='text' name='body'class='form-control' value='{{$post->body}}'></td>
<td>{{$post->employee_no}}</td>
<td><a href="{{ action("PostsController#update", $post->id) }}" >Save</a></td>
<td>Edit</td>
</tr>
#endforeach
</table>
#else
<p>You Have No Posts</p>
#endif
My update function on PostsController.php page:
public function update(Request $request, $id)
{
$this->validate($request,[
//'title' => 'required',
'body' => 'required'
]);
//Update Post
$post = Post::find($id);
//$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('/posts')->with('success','Post Updated');
}
I read that "action" will go to the first route that matches the pattern, but I do not know how to solve this problem.
My web.php page:
Route::get('/', 'PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/services', 'PagesController#services');
Route::resource('posts','PostsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');
How can I call the update function correctly from dashboard.blade.php page?
To update data you have to use form with put method.
and route will be like this,
Route::put('dashboard/update/{id}',[
'uses' => 'DashboardController#update',
'as' => 'dashboard.update'
]);
For more Laravel Routing
Hope it helps :)
You can solve this by using form submit with hidden PUT method.
<form action="{{ url('customers') }}/{{$data['customer']->id}}" method="POST">
<input type="hidden" name="_method" value="PUT">
#csrf
</form>
How about having a simple form for each row in the table :
#if(count($posts) > 0 )
<table class="table table-striped">
<tr>
<th>Title</th>
<th>Body</th>
<th>Employee Number</th>
<th>Edit</th>
<th>Save</th>
</tr>
#foreach($posts as $post)
<tr>
<td>{{$post->title}}</td>
<td>{{$post->employee_no}}</td>
<td>
<!-- Form for each row -->
<form action="{{ route("posts.update", [ 'post' => $post->id ]) }}" method="POST">
#method('PUT')
#csrf
<input type='text' name='body'class='form-control' value='{{$post->body}}'></td>
<button type="submit">Save</button>
</form>
<!-- Form for each row -->
<td>Edit</td>
</tr>
#endforeach
</table>
#else
<p>You Have No Posts</p>
#endif
Related
I faced an issue where I'm unable to retrieve data through the pivot table,
In my application there's a separate table to store the files data and a separate table to store the competition and a separate table to store the teams.
And there's a table named competition_file which has the competition_id, team_id, file_id.
a user can add multiple files to a competition by selecting a team or by not selecting a team as show in the below image
It will be synced to the competition_file table as shown in the below image
I need to show all data in the UI as a table shown in below image
I'm wondering how can I fill the team name selected using the pivot table
Relationship in File Model
public function teams()
{
return $this->belongsToMany(Team::class,'competition_file')->wherePivot('type','document');
}
Relationship in Competition Model
public function documents()
{
return $this->belongsToMany(File::class,'competition_file','competition_id', 'file_id')->wherePivot('type','document');
}
And this is my controller index function
public function index($id)
{
$competition = $this->competitionsRepo->find($id,[
'team',
'documents',
]);
return view('competitions.document',[
'competition'=>$competition,
]);
}
My Blade
#foreach ($competition->documents as $key=>$document)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $document->name }}</td>
<td>-</td>
<td>{{ $document->created_at->toDateString() }}</td>
<td>
<form class="confirm" action="{{ route('documents.destroy',['competition'=>$competition,'document'=> $document->id]) }}" method="POST"
data-confirm-title="Remove Document?"
data-confirm-message="Are you sure you want to remove this document?">
#csrf
#method('DELETE')
<button type="submit" name="button" class="button">Remove</button>
</form>
</td>
</tr>
#endforeach
Someone please help me out to get the team names to my foreach in the blade
I hope my question is clear, please comment if it isn't ill edit the question
I was able to fix my issue by learning Eager loading through a recommendation
Here are the changes I did
In controller
public function index($id)
{
$competition = $this->competitionsRepo->findOrFail($id);
$files = $competition->documents()->with(
[
'teams' => function($q) use ($competition) {
$q->with([
'documents' => function($q) use($competition) {
$q->where([
'competition_id' => $competition->id,
]);
}
]);
},
]
)->get();
return view('competitions.document',[
'id'=>$id,
'competition'=>$competition,
'files'=>$files,
]);
}
In my Blade table i used $file->teams->implode('name')
<table class="table light-td sports-tbl tb-chg2 table-condensed table-borderless">
<thead>
<tr>
<th>#</th>
<th>Document Name</th>
<th>Assigned Team</th>
<th>Date Added</th>
<th>Action</th>
</tr>
</thead>
<tbody>
#foreach ($files as $key=>$file)
<tr>
<td>{{ $key+1 }}</td>
<td>
<a href="{{ asset('storage/'. $file->file_path) }}" class="text-info" target="_blank">
<img src="{{ asset('/images/sketches/1x/file.png') }}" alt="file">
</a>
{{ $file->name }}
</td>
<td>
{!! $file->teams->isNotEmpty() ? $file->teams->implode('name') : '<span class="text-secondary">No teams selected</span>' !!}
</td>
<td>{{ $file->created_at->toDateString() }}</td>
<td>
<form class="confirm" action="{{ route('documents.destroy',['competition'=>$id,'document'=> $file->id]) }}" method="POST"
data-confirm-title="Remove Document?"
data-confirm-message="Are you sure you want to remove this document?">
#csrf
#method('DELETE')
<button type="submit" name="button" class="button">Remove</button>
</form>
</td>
</tr>
#endforeach
</tbody>
</table>
I'm Doing a "take home assignment" for job interview. While I have some experience in web development, its not my forte. I am trying to delete a row in a SQLite table using a HTML DELETE button. I am using Laravel-php framework.
I've tried different solutions on google and stack Overflow, but none seem to solve the problem. I modeled my approach after this Laracasts video
Link to my code
The blade seems to be passing the correct info ($id from $contact->id) & the controller seems to be receiving. But the given contact associated with the id isn't being deleted.
FROM BLADE:
<div class="col-md-6">
<table class="table table-striped table-hover">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Address</th>
</tr>
#foreach($contacts as $contact)
<tr>
<td> {{$contact->f_name}} </td>
<td> {{$contact->l_name}} </td>
<td> {{$contact->address}} </td>
<td>
<form method="POST" action="/delete/{{ $contact->id }}">
#method('DELETE')
#csrf
<div class="field">
<div class="control">
<button type="submit" class="button">Delete Contact</button>
</div>
</div>
</form>
</td>
</tr>
#endforeach
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</table>
</div>
FROM CONTROLLER:
public function delete($id) {
Contact::find($id)->delete();
return view('index');
}
FROM ROUTE:
Route::delete('/delete', [
'uses'=>'ContactController#delete',
'as'=>'contacts.delete'
]);
you're not getting the id on your delete method, you need to include it on the url like
Route::delete('/delete/{id}', [
'uses'=>'ContactController#delete',
'as'=>'contacts.delete'
]);
in this case, you don't need to change the delete method.
you can also retrieve the id from the request object without changing the delete route, int this case you need to include the id as a hidden input on your view and your delete method will look like this
public function delete(Request $request) {
Contact::find($request->id)->delete();
return view('index');
}
I'm using resource controller to delete record in row by passing a collection into the view.
View:
<tbody>
#php $count=1; #endphp
#forelse ($products as $product)
<tr>
<td>{{ $count }}</td>
<td>{{ $product->name }}</td>
<td>{{ $product->slug }}</td>
<td>{{ $product->updated_at }}</td>
<td><span class="label label-success">Published</span></td>
<td>
<div class="btn-group">
View
Edit
Delete
<form id="delete-product" method="POST" action="{{ route('products.destroy', $product->slug) }}" style="display: none;">
#csrf
#method('DELETE')
</form>
</div>
</td>
</tr>
#php $count++; #endphp
#empty
<tr>
<td colspan="6">No products yet.</td>
</tr>
#endforelse
</tbody>
Controller:
public function products()
{
$products = Product::orderBy('created_at', 'desc')->paginate(10);
return view('vendor.products')->with('products', $products);
}
public function destroy(Product $product)
{
$product->delete();
return redirect('/account/products')->with('success', 'Product deleted successfully.');
}
When I click any of the "Delete" button, it deletes the last post (the first post in database, since it is sorted in descending).
Can someone tells me where it is wrong? I think initally the code work just fine, until I make some other modification and it is "magically" did not work as expected.
Edited:
route:
Route::prefix('/account')->group(function () {
Route::get('/products', 'AccountController#products');
Route::get('/corporate-info', 'AccountController#corporateInfo');
Route::get('/add-product', 'ProductController#create');
Route::get('/edit-product-{product}', 'ProductController#edit');
});
Route::resource('products', 'ProductController');
Product model:
public function getRouteKeyName()
{
return 'slug';
}
Ohh here is your issue :
Delete
<form id="delete-product" method="POST" action="{{ route('products.destroy', $product->slug) }}" style="display: none;">
#csrf
#method('DELETE')
</form>
You are giving the same id 'delete-product' to each form while looping therefore whenever you are
document.getElementById('delete-product').submit();
Its getting and submitting the form with the delete-product id which is the last one due to overriding issue to solve your issue :
Delete
<form id="delete-product-{{$product->slug}}" method="POST" action="{{ route('products.destroy', $product->slug) }}" style="display: none;">
#csrf
#method('DELETE')
</form>
I trying make changing role in my panel admin. But when i made it my form dont sending post request. Now only working showing user role. I cant fix it becouse when i click button to switch i dont get any error and role is not changing.
I use HttpRequester when i use url /admin/change its showing this error:
MethodNotAllowedHttpException in RouteCollection.php line 233
There is my code:
View:
#extends('layouts.app')
#section('content')
<h2>Panel Admina</h2>
<div class="panel-group">
<div class="panel panel-default">
<div class="panel-body">
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Imię</th>
<th>Nazwisko </th>
<th>E-mail </th>
<th>Nr.tele </th>
<th>Użytkownik </th>
<th>Moderator </th>
<th>Admin </th>
</tr>
</thead>
<tbody>
#foreach($users as $user)
<tr>
<form action="{{route('change')}}" method="post">
{{ csrf_field() }}
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->lastname }}</td>
<td>{{ $user->email }}<input type="hidden" name="email" value="{{ $user->email }}"></td>
<td>{{ $user->phonenumber }}</td>
<td><input type="checkbox" {{ $user->hasRole('User') ? 'checked' : '' }} name="role_user"/></td>
<td><input type="checkbox" {{ $user->hasRole('Moderator') ? 'checked' : '' }} name="role_moderator"/></td>
<td><input type="checkbox" {{ $user->hasRole('Admin') ? 'checked' : '' }} name="role_admin"/></td>
<td><input type="submit" class="btn btn-default btn-sm" value="Przydziel rolę"></td>
</form>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endsection
Controller:
public function change(Request $request)
{
$user = User::where('email', $request['email'])->first();
$user->roles()->detach();
if ($request['role_user']) {
$user->roles()->attach(Role::where('name', 'User')->first());
}
if ($request['role_moderator']) {
$user->roles()->attach(Role::where('name', 'Moderator')->first());
}
if ($request['role_admin']) {
$user->roles()->attach(Role::where('name', 'Admin')->first());
}
return redirect()->back();
}
Routing:
Route::get('/admin', [
'uses' => 'AdminController#index',
'middleware' => 'roles',
'roles' => ['Admin']
]);
Route::post('/admin/change', [
'uses' => 'AdminController#change',
'as' => 'change',
'middleware' => 'roles',
'roles' => ['Admin']
]);
I really dont know how i can resolve my problem.
try to pass the user id to your route and your form action and instead of post make it put for example:
<form action="{{route('change', $user->id)}}" method="post">
and in your route:
Route::put('/admin/change/{id}', [
'uses' => 'AdminController#change',
'as' => 'change',
'middleware' => 'roles',
'roles' => ['Admin']
]);
and in your controller:
public function change(Request $request, $id)
I am having some trouble implementing server side pagination in Laravel 5.
Do you have any idea please tell
try with this one
//in controller
$users = \App\User::paginate(15)
return view('your desired view file name', compact('users'));
// in view
<div class="text-center text-muted" role="status" aria-live="polite">Showing {{$users->firstItem()}} to {{$users->lastItem()}} of {{$users->total()}} entries</div>
<div style="display:flex;justify-content:center;align-items:center;" >
<p></p>
{!! with(new Illuminate\Pagination\BootstrapThreePresenter($users))->render()!!}
</div>
In controller you do somrthing like following
$users = \App\User::paginate(15)
return view('template.file', compact('users'));
and in View file you put following
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
#foreach ($users as $user)
<tr>
<td> {{ $user->name }} </td>
</tr>
#endforeach
</tbody>
</table>
{!! $users->appends(\Input::except('page'))->render() !!}
Last line renders pagination links.