Laravel Audit Method audits does not exist - php

i'm trying to get the last username who update the form using LARAVEL AUDITING but i always end up with this error "Method audits does not exist." but in my database audit already exist and working but i can't make it work in my controller..
Controller: (eg. I'm trying to get the last user who updated the table)
public function show($id, Request $request)
{
//
$leads = Lead::findOrFail($id);
$diff = $request->audits()->with('user')->get()->last();
// show the view and pass the nerd to it
return view('leads.show')->with('leads', $leads)->withDiff($diff);;
}
Blade:
<div> Revision by <strong>{{ $diff->user->name }}</strong></div>

i found the answer by my self i did some mistakes..
Controller:
public function show($id, Request $request)
{
$leads = Lead::findOrFail($id)->audits()->with('user')->get()->last();
return view('leads.show')->with('leads', $leads);
}

Related

No query results for model [App\Models\XXXX]

I'm getting this error message when updating the data via the PUT method on Postman.
No query results for model [App\Models\Post].
Code
public function store(Request $request)
{
$post = $request->isMethod('put') ? Post::findOrFail
($request->post_id): new Post;
$post->id= $request->input('id');
$post->title= $request->input('title');
$post->body= $request->input('body');
if($post->save()){
return response($post);
}
}
Reference: https://github.com/Devgroup-Asia/lumenblog/blob/main/app/Http/Controllers/PostsController.php
The issue is due to this below code.
Post::findOrFail($request->post_id)
Post with $request->post_id doesnot found.
You may be missing to send post_id from form

How to relate multiple forms from different controllers?

I have Certificate and Student models, with one to many relation.
When the user is done making the certificate he is redirected to another page with student form, I dont know how to get the certificate_id or how to relate the student to the last certificate that it was created. help please.
Certificate Controller
public function store(StoreCertificateRequest $request)
{
$data = $request->validated();
$data['user_id'] = auth()->id();
Certificate::create($data);
return redirect()->route('students.create')->with(['success' => 'Certificate has been Saved']);
}
Student Controller
public function store(StoreStudentRequest $request)
{
$data = $request->validated();
Student::create($data);
return redirect()->route('users.index')->with('success', 'Students were added');
}
So the certificate is already created, you can get that id by assigning a variable to the create, and then return it to the view as $variable->id

How do delete single row in pivot table with laravel

I want to delete a single row of data in my pivot table. I don't get any error but when try to click on the button. It did not redirect me to anywhere so the delete function is not performed.
In the picture above I want to delete the highlighted id for user_id = 3
My scenario is that the user suddenly can't make it to even_id = 6 so the user wants to delete/unjoined the event.
route
Route::get('/user/event/{event}', 'HomeController#destroy')->name('user.event.destroy');
blade
#foreach ($events as $event)
<tr>
<td>{{$loop->index +1 }}</td>
<td>{{$event->event_name}}</td>
<td>{{$event->event_date}}</td>
<td>
<form method="POST" action="{{ route('user.event.destroy',$event)}}">
#csrf
#method('DELETE')
<a class="btn btn-danger">Unjoined!</a>
</form>
</td>
</tr>
#endforeach
controller
public function storeEventUser(Request $request)
{
$user = User::find(Auth::user()->id);
//how I storing my pivot data (just to show if anyone asking)
$user->events()->syncWithoutDetaching([$request->event_id]);
}
public function destroy($event)
{
$event= Event::findOrFail($event_id);
$user->events()->detach($event);
return redirect()->back()->with('success','Deleted.');
}
Event model
public function users()
{
return $this->belongsToMany(User::class,'event_user','event_id','user_id');
}
user model
public function events()
{
return $this->belongsToMany(Event::class,'event_user','user_id','event_id');
}
I am adjusting your controller method to use Route Model Binding for simplicity:
public function destroy(Event $event)
{
Auth::user()->events()->detach($event);
// or from the other side of the relationship
// $event->users()->detach(Auth::user());
return redirect()->back()->with('success', 'Deleted.');
}
As stated in the comments you need to adjust your route to Route::delete if you want to use the DELETE HTTP method that your form is spoofing via the #method('DELETE') blade directive.
Side note:
Auth::user() returns a User instance so you don't need to query for it again, in your storeEventUser method:
$user = Auth::user();

Calling function from the same controller to return the view with flash message

When the user save the object in the blade view, a function into my controller execute with post request. If the save was success I want to reload the page with the changes.
So I need to call index() again to get the refreshed data. Also, I want to show a success message. So the steps are:
index() function execute so the user can show his items.
the user choose an item and save it
save(Request $request) is called
everything save fine
save function call index() with sending a parameter with the flash success message?
I have this structure in my controller:
public function index(){
...
return view('agenda')->with(['agenda'=>$response]);
}
public function save(Request $request){
...
// here where I need to return index function with success message
}
How can I solve my problem?
Assuming the save action was called from the index page, simply redirect back
public function save(Request $request){
...
return redirect()->back()->with('message', 'Item saved');
}
If it was called from a different page, redirect to index page. Assuming your index() function matches a route /:
return redirect()->to('/')->with('message', 'Item saved');
Then display the flash message in your index view.
#if(Session::get('message'))
{{ Session::get('message') }}
#endif
For styling, if you are using bootstrap you can do
#if(Session::get('message'))
<div class="alert alert-success">
{{ Session::get('message') }}
</div>
#endif

How to store homepage in cache in laravel 4

I am new in laravel and php. I am developing a magazine site with laravel 4. I want to store home page in laravel cache . But In my homepage , there are many queries . Please see details below.:
My HomepageController index method:
public function index()
{
return View::make('homepage');
}
My Query Helper Function which is used for post by category in my Homepage:
public static function cat_post($category, $limit)
{
$posts = Post::whereHas('categories', function($q) use ($category)
{
$q->where('category_slug', 'like', $category);
})->with('categories')->take($limit)->orderBy('created_at', 'DESC')->get();
return $posts;
}
In My homepage.blade.php , I used this helper function many times. like below:
<?php $national = Helper::cat_post('national', 3); ?>
#foreach ($national as $post)
{{ Helper::img_src($post) }}
<h4>{{ $post->title }}</h4>
</div>
#endforeach
Now i want to put homepage in cache and when new post created, then delete old homepage from cache and store new one.
Please help me. Is it possible ?????
To store a value in cache you can use Laravel's Cache:
public function index()
{
return Cache::rememberForever('homepageCache', function()
{
return View::make('homepage');
});
}
This snipped tries to retrieve a cached version of View::make('homepage') and otherwise creates it.
Every time a post is created, updated or deleted you need to invalide your homepageCache:
Cache::forget('homepageCache');

Categories