Delete A Record From A Relationship - laravel

I want to delete a record: remove a routine,that belongs to a user, on button click (hasMany). I have set up the view, models and relationship within,delete route, and the controller method to delete.
When I try to click the button to remove the routine from the db, it does nothing. why does it not removing the record?
Here's my code: route:
Route::post('routine/delete', 'RoutineController#delete'); // Delete a routine for a user.
Controller:
public function delete(Request $request)
{
$id = $request->input("id"); // Getting the id via. the ajax request.
$routine = \App\Routine::find($id); //Fetching the routine object from the db ifentified by the id passed via ajax
if ($routine)
{
$routine->delete();
}
return ["status" => "success"];
}
View:
<div class="col-lg-2">
<!-- When this button is clicked, we determine which routine to remove. -->
<button class="btn btn-danger remove_routine" data-id="{{$routine->id}}" data-token="{{csrf_token()}}" style="display:inline">Delete</button>
</div>
User Model:
public function routine()
{
return $this->hasMany('App\Routine');
}
Routine model:
public function user()
{
return $this->belongsTo('App\User');
}
Thanks in advance!

Don't know if it exactly answers your question, and I don't use AJAX, but I always do my deletes like this:
View
#foreach($database-thing as $item)
<form method="POST" action="$yourActionHere" style="display:inline" >
<input name="_method" type="hidden" value="DELETE">
<button type="submit" class="btn btn-danger btn-xs"><i class="fa fa-trash"></i> Delete</button>
</form>
#endforeach
// Even easier with laravelcollective/forms
#foreach($database-thing as $item)
{!! Form::open([
'method'=>'DELETE',
'url' => [$yourUrl, $item->id // <-- Very important],
'style' => 'display:inline'
]) !!}
{!! Form::button('<i class="fa fa-trash"></i> Verwijder', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs']) !!}
{!! Form::close() !!}
#endforeach
Controller
public function destroy($id)
{
YourModel::destroy($id);
// All your other logic like redirects and stuff
}

Working delete, based on the code above and this updated controller function:
public function delete(Request $request,$id)
{
$user=Auth::user();
$routine = \App\Routine::findOrFail($id); // find or throw an error if you don't find the routine id in db.
// Makes if() statement redundant, since it checkes for that id already.
// Need to check that the owner of the routine is the current authenticated user.
if ($user->id != $routine->user->id)
{
Session::flash('flash_message', 'No routine found.');
}
else
{
$routine->delete();
Session::flash('routine_deleted','Routine deleted!');
}
// No need to return $routine since I am deleting it, otherwise it will throw and error of trying to get property of non-object.
return redirect()->back()->with('user') ;
//return view('view_routine')->with('user', 'routine');
}

There you go
$remove = 2;
$filtered = $c->filter(function ($value, $key) use($remove){
return $value['id']!=$remove;
});

Related

Laravel 9 how to pass parameter in url from one controller to another controller?

I was facing this problem of missing parameter when trying to pass a parameter from one controller to another controller. The parameter is $id whereby the data is originally from post method in details blade.php into function postCreateStepOne However, I want to pass the data into a new view and I return
redirect()->route('details.tenant.step.two')->with( ['id' => $id]
);}
And this is where error occur. However, it works fine if I skip it into a new route and directly return into a view with the compact parameter. For Example,
return view('document.details-step-two', compact('id', 'property'));
However, I would prefer a new url as I was doing multistep form using Laravel.
Error
web.php
Route::get('/document/details/viewing/{id}', 'ViewDetails')->name('details.tenant');
Route::post('/document/details/viewing/{id}', 'postCreateStepOne')->name('post.step-one');
Route::get('/document/details/viewing/step-2/{id}', 'ViewDetailsStep2')->name('details.tenant.step.two');
TenanatController.php
public function viewDetails($id){
$view = Properties::findOrFail($id);
return view('document.details', compact('view'));
}
public function ViewDetailsStep2(Request $request, $id){
$view = Properties::findOrFail($id);
$property = $request->session()->get('property');
return view('document.details-step-two', compact('view', 'property'));
}
public function postCreateStepOne($id, Request $request)
{
$validatedData = $request->validate([
'property-name' => 'required',
]);
if(empty($request->session()->get('property'))){
$property = new Tenancy();
$property->fill($validatedData);
$request->session()->put('property', $property);
}else{
$property = $request->session()->get('property');
$property->fill($validatedData);
$request->session()->put('property', $property);
}
return redirect()->route('details.tenant.step.two')->with( ['id' => $id] );
}
details.blade.php
<form action="{{ route('post.step-one', $view->id) }}" method="POST">
#csrf
<div class="card-body">
<div class="form-group">
<label for="title">Property Name:</label>
<input type="text" value="" class="form-control" id="property-name" name="property-name">
</div>
</div>
<div class="card-footer text-right">
<button type="submit" class="btn btn-primary">Next</button>
</div>
</form>
When you use with on a redirect the parameter is passed through the session. If you want to redirect to a route with a given route parameter you should pass that parameter in the route function itself like e.g.
return redirect()->route('details.tenant.step.two', ['id' => $id]);

couldn't update one field in laravel

this is the route in web.php:
Route::put('/users.status', [\App\Http\Controllers\dashboard\UsersController::class, 'status'])->name('users.status');
and here is the code in Controller:
public function status(User $user) {
try{
$user->is_active = 1;
$user->update();
//$user->update(['is_active' => 1 ]);
}catch (\Exception $ex) {
return redirect()->route('users.index')->with('status', "you couldn't update this record");
}
return redirect()->route('users.index')->with('status', 'User Updated successfully');
}
here is the code in a view, it just a button, when click on it, I need to change the status:
<td class="border px-4 py-2">
<form method="POST" action="{{route('users.status', $user)}}">
#csrf
<input type="hidden" name="_method" value="put"/>
<button type="submit"
class="{{ $user->is_active==1 ? "bg-green-500" : "bg-red-500" }} hover:bg-red-700 text-white font-bold py-1 px-1 rounded">
{{$user->is_active==1 ? "Active" : "Inactive"}}
</button>
</form>
</td>
after the button above has clicked, give me this message:
User Updated successfully
but nothing updated in database
First which User you are trying to fetch from route to controller method? You are trying to inject a User $user object to controller method but you did not specified a user param on route definition.
And on the update part; after you set $user->is_active = 1; you need to run $user->save();
So; you need to add a user param to your route:
Route::put('/users/{user}/status', [\App\Http\Controllers\dashboard\UsersController::class, 'status'])->name('users.status');
and run save() method on updated $user ;
public function status(User $user)
{
try {
$user->is_active = 1;
$user->save();
} catch (\Exception $ex) {
return redirect()->route('users.index')->with('status', "you couldn't update this record");
}
return redirect()->route('users.index')->with('status', 'User Updated successfully');
}
You should call $user->save(); not $user->update();
Also, you can call $user->update(['is_active'=>1]); to have only one line of code.

POST method not supported for route in Laravel 6

I am building a discussion form in Laravel 6. The route I used is a POST method and I checked it in route:list. I get the following error, why?
The POST method is not supported for this route. Supported methods:
GET, HEAD, PUT, PATCH, DELETE
View
<form action="{{ route('replies.store', $discussion->slug) }}" method="post">
#csrf
<input type="hidden" name="contents" id="contents">
<trix-editor input="contents"></trix-editor>
<button type="submit" class="btn btn-success btn-sm my-2">
Add Reply
</button>
</form>
Route
Route::resource('discussions/{discussion}/replies', 'RepliesController');
Controller
public function store(CreateReplyRequest $request, Discussion $discussion)
{
auth()->user()->replies()->create([
'contents' => $request->contents,
'discussion_id' => $discussion->id
]);
session()->flash('success', 'Reply Added.');
return redirect()->back();
}
You passed a disccussion object as parameter in order to store user_id within an array.
I think this is not a good practice to store data.
You might notice that your routes/web.php and your html action are fine and use post but you received:
"POST method not supported for route in Laravel 6". This is runtime error. This probably happens when your logic does not make sense for the compiler.
The steps below might help you to accomplish what you want:
1. Eloquent Model(App\Discussion)
protected $fillable = ['contents'];
public function user(){
return $this->belongsTo('App\User');
}
2. Eloquent Model(App\User)
public function discussions(){
return $this->hasMany('App\Discussion');
}
3. Controller
use App\Discussion;
public function store(Request $request){
//validate data
$this->validate($request, [
'contents' => 'required'
]);
//get mass assignable data from the request
$discussion = $request->all();
//store discussion data using discussion object.
Discussion::create($discussion);
session()->flash('success', 'Reply Added.');
return redirect()->back();
}
4. Route(routes/web.php)
Route::post('/replies/store', 'RepliesController#store')->name('replies.store');
5. View
<form action="{{ route('replies.store') }}" method="post">
#csrf
<input type="hidden" name="contents" id="contents">
<trix-editor input="contents"></trix-editor>
<button type="submit" class="btn btn-success btn-sm my-2">
Add Reply
</button>
</form>

get id from url and passing into form in laravel 5

i defined a route as
Route::get('roundtables/{name}/tags/store',['as'=>'tags.store','uses'=>'TagController#store','middleware'=>['owner','auth']]);
In my view, i have a form in this url
http://localhost:8000/roundtables/1/tags
<div class="col-md-3">
<div class="well">
{!! Form::open(['route'=>'tags.store','method'=>'GET']) !!}
<h2>New Tags</h2>
{{ Form::label('name','Name') }}
{{ Form::text('name',null,['class'=>'form-control']) }}
{{Form::submit('Create New Tag',['class'=>'btn btn-primary btn-block btn-h1-spacing'])}}
</div>
</div>
My problem is, how to get the id from url which is id '1' and passing into the form when user clicked submit.
My controller
public function store(Request $request,$name)
{
$this->validate($request,array('name'=>'required|max:255'));
$tag=new Tag;
$tag->name=$request->name;
$tag->roundtable_id=$name;
$tag->save();
Session::flash('success','New Tag was successfully added');
return redirect()->route('tags.index');
}
When you're using custom routes for CRUD, avoid using standard RESTful method and route names. Before building the form, you need to pass this variable to the view:
public function createTag($name)
{
....
return view('form', compact('name'));
}
Define your route as:
Route::get('roundtables/{name}/tags/storeTag',['as'=>'tags.storeTag','uses'=>'TagController#storeTag','middleware'=>['owner','auth']]);
Then pass variable to from the form:
{!! Form::open(['route' => ['tags.storeTag', $name], 'method'=>'GET']) !!}
And get it in the controller:
public function storeTag(Request $request, $name)
{
echo $name;
You get the wildcard value by using request() helper method easily.
{{request()->route('name')}}
In your TagController
public function index($name)
{
$tags= Tag::where($name)->first();
// add name parameter to return
return view('tags.index')->withTags($tags)->withName($name);
}
And in your view
<div class="col-md-3">
<div class="well">
//edit this form tag
{!! Form::open(['route'=>['tags.store',$name],'method'=>'GET']) !!}
<h2>New Tags</h2>
{{ Form::label('name','Name') }}
{{ Form::text('name',null,['class'=>'form-control']) }}
{{Form::submit('Create New Tag',['class'=>'btn btn-primary btn-block btn-h1-spacing'])}}
</div>
</div>
And in your TagController#store
public function store(Request $request,$name){
echo $name;
}
This should work.
Request::segment(2)
Or pass it from controller:
public function index($name)
{
$tags= Tag::where($name)->first();
return view('tags.index')->withTags($tags)->with('name', $name);
}
Then just pass it into the form:
{!! Form::open(['route'=>['tags.store', $name],'method'=>'GET']) !!}

Laravel return to view before edit when edit is called from multiple places

I have an edit form that gets called from 2 different views. How do I return to the correct view after I edit the form? Do I have to pass the calling view to the edit page then to the controller to return back to or is there a better way?
attendee/index.blade
<td>{{link_to_route('attendee.edit','',$attendee->id, array(
'class'=>'edit-attendee btn btn-info btn-xs glyphicon glyphicon-pencil',
'data-title' => 'Edit Attendee'))}} </td>
register/index.blade
<td>{{link_to_route('attendee.edit','',$attendee->id, array(
'class'=>'edit-attendee btn btn-info btn-xs glyphicon glyphicon-pencil',
'data-title' => 'Edit Attendee'))}} </td>
edit.blade
{{ Form::model($attendee, array('class'=>'form-horizontal', 'method' => 'PATCH', 'route' => array('attendee.update', $attendee->id))) }}
...irrelevant stuff...
<div class=pull-right>
{{Form::submit('Update',array('class'=>'btn btn-success'))}}
<a href = "{{URL::previous()}}" class = 'btn btn-warning'>Cancel</a>
</div>
{{ Form::close() }}
controller
public function update($id)
{
$attendee = Attendee::findOrFail($id);
$validator = Validator::make($data = Input::all(), Attendee::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput()->with('id', $id);
}
$attendee->update($data);
return Redirect::route('attendees.index');
}
Jeffrey Way always says, "When in doubt, create a new controller." Might be worthwhile advice in this case. AttendeesController.php can handle the "regular" edits, and a new RegisterAttendeesController.php for those coming from the registration page? For consistency between controllers, use a static method in the model for doing the updates, and return separate views.

Resources