spatie / activitylog

A very simple activity logger to monitor the users of your website or application
https://spatie.be/opensource/laravel
MIT License
582 stars 75 forks source link

Unable to clear log using Activity::cleanLog(); #31

Closed nehulagr closed 8 years ago

nehulagr commented 8 years ago

Thank you for such a nice module. It works great. Its simple and working.

I just need to know why Activity::cleanLog(); is not working? Its showing this error

BadMethodCallException in Builder.php line 2161:
Call to undefined method Illuminate\Database\Query\Builder::cleanLog()

I am just using it normally, like below i have a controller with name LogsController and i have used use Spatie\Activitylog\Models\Activity; in which i have just 2 functions as below:

public function index()
    {
        $latestActivities = Activity::with('user')->latest()->get();
        return view('logs.logs')->with('logs', $latestActivities);
    }

    public function clearlog(){
        Activity::cleanLog();
        Activity::log('Logs cleared By : '.Auth::user()->name);
        \Session::flash('success', 'Logs deleted successfully');
    }

I have defined a route for this:

Route::get('logs/clearlog', 'LogsController@clearlog');
Route::resource('logs', 'LogsController');

I dont know where i am making mistake. Please help me get this thing straight. I am new to laravel.

Thank you!

freekmurze commented 8 years ago

Did you import the Activity-facade at the top of your file?

use Activity;
nehulagr commented 8 years ago

No its not allowing me to import two files. I have already import

use Spatie\Activitylog\Models\Activity;

What should i do?

nehulagr commented 8 years ago

Here is how my full controller looks like...

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Spatie\Activitylog\Models\Activity;
//use Activity;

class LogsController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $latestActivities = Activity::with('user')->latest()->get();
        return view('logs.logs')->with('logs', $latestActivities);
    }

    public function headerLogs()
    {
        $latestActivities = Activity::with('user')->latest()->limit(10)->get();
        return view('layouts.header')->with('logs', $latestActivities);
    }

    public function clearlog(){
        Activity::cleanLog();
        Activity::log('Logs cleared By : '.Auth::user()->name);
        \Session::flash('success', 'Logs deleted successfully');
        return redirect('logs');
    }
}
sebastiandedeyne commented 8 years ago

You'll need to rename one of your imports with the as keyword.

use Activity;
use Spatie\Activitylog\Models\Activity as ActivityModel;