I installed spatie/activitylog for logging user activity,
the default table name in this package is activity_log I want change the table name to users_activity_log
I created a model and named Activity but not work:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Models;
class Activity extends Model
{
//
protected $table = "user_activity_log";
}
How can I do it?
I found the solution,
1- Create a model : php artisan make:model Activity
2- put this code in your model (change $table with your table name):
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Activity extends \Spatie\Activitylog\Models\Activity
{
//
protected $table = "user_activity_log";
}
3- publish the logactivity config file:
php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="config"
4- change the activity_model in laravel-activitylog.php :
'activity_model' => \App\Activity::class
DON'T FORGET to change table name in migration file
Thanks to #devk
Related
I have created a model by using the command php artisan make:model Patient. The command creates a model named Patient in the app folder.
In Model Patient:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Patient extends Model
{
//
public function getPurchaseOrder(){
return "Hello World";
}
}
In Controller PatientController:
namespace App\Http\Controllers;
class PatientController extends Controller
{
protected $patientModel;
public function __construct(Request $request, PatientInterface $patient)
{
parent::__construct($request);
$this->patientModel = new \App\Patient();
}
public function test(){
echo $this->patientModel->getPurchaseOrder();
}
}
It's working fine. The problem is when I create a folder named Models inside the app folder and move Patient model then call the model function it gives an error:
FatalThrowableError Class 'App\Models\Patient' not found
Any help will be appreciated.
When you move a class to a different directory, for it to be loaded by composer with the PSR-4 standard, you must also update the class' namespace to match.
namespace App\Models;
In addition, when you run the make command, you can include a namespace in that to automatically put it in the directory with the correct namespace:
php artisan make:model Models\\Patient
In Laravel 5.2 I'm trying to eager load a User along with its Usersettings with this piece of code: App\User::with('usersettings')->get(), but it fails, and I can't seem to figure out why. This is the given error.
BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::usersettings()'
I've read the laravel docs and I've watched a lot of Laracasts, and this worked before, so I get the idea I'm missing something really small and probably obvious.
User.php
<?php
namespace App;
/* User with fields: id, email */
class User extends Illuminate\Database\Eloquent\Model {
protected $table = 'users';
public function usersettings() {
return $this->hasOne("App\Usersettings", "userid");
}
}
Usersettings.php
<?php
namespace App;
/* Settings with fields: id, userid, textcolor */
class Usersettings extends Illuminate\Database\Eloquent\Model {
protected $table = 'usersettings';
public function user() {
return $this->belongsTo('App\User', 'userid');
}
}
//Edit: I already tried lowercasing the s. This typo might have snuck in copying it to SO, but the error was there, and still is there, even after fixing it.
//Edit:
<?php
namespace App;
use App\UserSettings;
class User extends Illuminate\Database\Eloquent\Model {
protected $table = 'users';
public function settings() {
return $this->hasOne(UserSettings::class, "userid");
}
}
If I run php artisan tinker>>> App\User::with('settings')->get(), it works as expected, but below
<?php
namespace App;
use App\UserSettings;
class User extends Illuminate\Database\Eloquent\Model {
protected $table = 'users';
public function usersettings() {
return $this->hasOne(UserSettings::class, "userid");
}
}
gives BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::usersettings()' when I run php artisan tinker >>> App\User::with('usersettings')->get(). Likewise when I rename the method to abc and run php artisan tinker >>> App\User::with('abc')->get() (that fails as well)
You mistyped when defining the relation on the User model:
public function usersettings() {
return $this->hasOne("App\Usersettings", "userid");
}
I would suggest some cleanup:
name the tables 'users' and 'user_settings',
rename the field 'userid' to 'user_id',
rename the 'Usersettings' model to 'UserSettings'.
That way you dont need to explicitly define table names and foreign keys, because you follow the conventions and Laravel can "guess" those.
You can also rename the the relation 'usersettings()' to 'settings()', since its obvious that they're the users' settings. Then you can fetch it like: 'User::with('settings')->get()'.
Did you try running composer dump-autoload after you changed your model relationships?
Just to make sure you're aware of this: you don't actually need to define the relationship in your UserSettings model to achieve what you want, only define the relationship in the User model. This is because you'd only need to get the settings in the context of the User, not the other way around.
Code for the User model:
<?php
namespace App;
use App\UserSettings;
class User extends Illuminate\Database\Eloquent\Model {
public function userSettings() {
return $this->hasOne(UserSettings::class);
}
}
UserSettings model: make sure it's camel case and that userid is present in your migration (the convention in your case would be to have user_id, so I would rename that column in your migration because then Eloquent will pick it up automatically and you don't need the second parameter in your User model's hasOne() definition).
Also, rename your table to user_settings if you can. That's another Eloquent convention, this one being that the model name UserSettings translates the camel case S letter in the middle of the classname to _ (and as a matter of fact, you shouldn't even need to explicitly state the table name if you've used the name user_settings).
Code for the UserSettings model:
<?php
namespace App;
/* Settings with fields: id, userid, textcolor */
class UserSettings extends Illuminate\Database\Eloquent\Model {
protected $table = 'user_settings';
}
Now, you should be able to do the below action. Note that the with parameter needs to be the relation function name from the User model.
$usersThatHaveSettingsIncludedInTheResults = User::with('userSettings')->get();
I am using laravel 5 for my small project and it seems like i'm having a problem with my model.
I have created this as my Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class tOrders extends Model
{
//
}
and this code from my controller to retrieve the data :
$test = tOrders::where('f_bank','=','ICBC')->orderBy('f_bank','ASC')->limit(10)->get();
after running the application, an error message came out saying
Invalid object name 't_orders'
i wonder where t_orders came from since my model is tOrders
Your model name should start with an uppercase letter and your file name should be the same. And I suspect Laravel is picking your model name as t_orders because your model name is starting with a lowercase letter. However, in your case for example, your filename should be Torders.php and code should be:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Torders extends Model
{
//
}
Cheers,
I´m trying to get the date from an user in my DDBB, them, I have 2 customer in a table.
The schema is called - customers
The table is called - names
And names has two columns, id and name, its very simple. I have added two persons in my table, Tony and Bob.
In laravel, in the app folder, I have created a new php file, and I put this:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model{
}
And I have this code in my .env file:
DB_HOST=localhost
DB_DATABASE=customers
DB_USERNAME=root
DB_PASSWORD=root
Then, I just want get my first customer, Tony, and in routes.php I have write this:
Route::get('customer', function(){
$customer = App\Customer::find(1);
print_r($customer);
});
When I run the application, I get this message:
FatalErrorException in routes.php line 23:
Class 'App\Customer' not found
I would like to get something, but I don´t know what happen. Thanks.
$customer = new Customer;
$customer->name = $request->name;
$customer->mobile_number = $request->mobile_number;
$customer->mail = $request->mail;
$request->name.......this name and $request->Mobile_number,$request->mail is .... form field name.
It says - Class 'App\Customer' not found.
Seems your class Customer is not loaded. Make sure, your Customer model is in App folder. Also the file name must be Customer.php. Also the class name must start with C(Capital) - Customer. And also make sure, you have defined the namespace as follows in Customer model.
namespace App;
Example
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $table = 'customer';
}
And try like this and see ?
Route::get('customer', function(){
$customer = \App\Customer::find(1);
print_r($customer);
});
I have created a model using
php artisan make:model Sessions
which creates a model named Sessions in the App\ folder. My Model (Sessions) and the controller(School) calling the model are below
Model (Sessions):
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Sessions extends Model {
//
protected $table = "sch_sessions";
protected $primaryKey = "session_id";
}
Controller (School)
<?php
namespace App\Http\Controllers;
use App;
class SchoolController extends Controller {
public function sessionManagement(){
$Sessions = Sessions::all();
}
}
I get the following error
FatalErrorException in SchoolController.php line 7: Class
'App\Http\Controllers\Session' not found
I've tried putting the model in a folder and "use"ing it but nada. I'm stumped as to why the error is referring to a controller in the first place. Can anyone see something I don't (can't) please?
add on the top
use App\Sessions;
or use it with prefix
\App\Sessions::all();
If user Ali Mohammed's answer doesn't clear it up for you, it looks to me like your error is happening elsewhere. App\Http\Controllers\Session note Session is not plural.
Are you attempting to use the Session class elsewhere in your controller? If so, make sure to prefix it so it's using the Session declared in the global namespace. You can do this with \Session.