So I have 2 User models, one is User and other is OrgUser for company users.
I'm trying to pass OrgUser to a OrgUserController and display single company user in show method. But I always get an empty object.
User Model
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password', 'verified', 'verification_token',
];
protected $hidden = [
'password', 'remember_token', 'verification_token',
];
OrgUser Model
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class OrgUser extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password', 'verified', 'verification_token', 'admin'
];
protected $hidden = [
'password', 'remember_token', 'verification_token',
];
OrgUserController
<?php
namespace App\Http\Controllers\OrgUser;
use App\Http\Resources\OrgUserResource;
use App\OrgUser;
use Illuminate\Http\Request;
use App\Http\Controllers\ApiController;
class OrgUserController extends ApiController
{
public function show(OrgUser $orgUser)
{
// OrgUserResource::withoutWrapping();
//
// return new OrgUserResource($orgUser);
return $orgUser;
}
Routes
Route::resource('orgusers', 'OrgUser\OrgUserController', ['only' => ['index', 'show']]);
Is there something that I would need to add to OrgUser model so I could pass data to controller like that? Because it works if I pass $id.
So It seems the problem was with naming in show method. I need to pass $orguser and not $orgUser. Because in my route it was /orgusers/{orguser}.
The show function should be accepting the $id rather than the model. If you change your code to the following, it should work:
public function show(Request $request, $id)
{
$orgUser = OrgUser::find($id);
return $orgUser;
}
Related
I want to create a model for my project but i don't know what's the different between the original MVC style and laravel default auth model
there's a directory different too. The original MVC was in Model folder, while the defaulth auth is in controller folder. I don't know why make a model inside the Controller folder
This is the code that's work ( laravel default auth model )
source : https://www.5balloons.info/changing-authentication-table-laravel/
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class CustomUser extends Authenticatable
{
use Notifiable;
protected $table = 'customusers';
protected $fillable = [
'name','username','email','passcode','active'
];
protected $hidden = [
'passcode',
];
}
while this code is not work with error Class 'App\Models\Authenticatable' not found
i can create a work around to use the class, but i need to understand why
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class TestUser extends Authenticatable
{
use HasFactory, Notifiable;
protected $table = 'testuser';
protected $fillable = [
'username','password',
];
protected $hidden = [
'password',
];
/*
public funtion getAuthPassword()
{
return $this-> passcode;
}
*/
}
I'm kinda new to laravel (5.7) and I'm stuck trying to create a registration form. the validation works great but then the page refresh and nothing happens, no errors, nothing changes on the registration form with no records on the database.
Here is my controller, I tweaked the basic form that laravel gives you to start with so it matches my database. And I created a folder for my models, so I changed the path to the "users" model that's all the tweaks I've done on this.
namespace App\Http\Controllers\Auth;
use App\Models\users;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/home';
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,User_Email',
'passowrd' => 'required|string|min:2|confirmed',
]);
}
protected function create(array $data)
{
return users::create([
'User_Nom' => $data['name'],
'User_Email' => $data['email'],
'User_pwd' => Hash::make($data['password']),
]);
}
}
I'm guessing that it has something to do with my Model but I can't put my finger in it. so here is my Model :
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class users extends Authenticatable
{
use Notifiable;
protected $primaryKey = 'User_Id';
protected $table = 'users';
protected $fillable = [
'User_Id', 'User_Nom', 'User_Prenom', 'User_pwd', 'User_Email', 'User_Tel', 'User_Fax', 'User_Post'
];
protected $hidden = [
'User_pwd', 'remember_token',
];
}
(Sorry for the indentation the copy - past messed it up)
Thank you
In your model, the namespace should also be changed to match the folder it's in. So: namespace App\Models;. Other than that, you have a typo in your validation: passowrd.
Check out https://laravel.com/docs/5.7/requests to see how you should receive the data in the controller.
How about calling the model like this ?
use App\users;
I have a table "articles" in this table a column name "user_id", I have another table "users" where store username, profile_pic etc... I want to fetch username on home page where I already fetched articles title.
Article Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
public function category()
{
return $this->belongsTo('App\Category');
}
public function tags()
{
return $this->belongsToMany('App\Tag');
}
}
View Page(Home Page)
{{$article->user_id}}
Here I want to Display username
User Model
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
}
Add the following to Article class:
public function user() {
return $this->belongsTo(User::class);
}
Then you can access related user data with $article->user->name
Am using Zizaco entrust package and trying to get all users together with their roles
User model i have
use Laravel\Passport\HasApiTokens;
use Illuminate\Database\Eloquent\Model;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Model implements AuthenticatableContract
{
use Authenticatable,HasApiTokens, EntrustUserTrait;
protected $fillable = [
'name', 'email', 'password','status'
];
protected $hidden = [
'password', 'remember_token',
];
public function role()
{
return $this->belongsTo('App\Role', 'id');
}
}
Now the App\Role
use Zizaco\Entrust\EntrustRole;
class Role extends EntrustRole
{
}
Whenver i fetch my users with
$sortval = explode("|",$request->sort);
return \App\User::orderBy( $sortval[0], $sortval[1])
->with("role") //added the relationship
->paginate($request->per_page);
The above role always returns null even when there are assigned roles to users.
I'm trying to build my first Laravel application by following a few guides on the internet and I'm feeling I'm missing something obvious. Here is the code.
Error
BadMethodCallException in Builder.php line 2450: Call to undefined
method Illuminate\Database\Query\Builder::addresses()
User-Model
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Sentinel;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'email', 'password',
];
protected $hidden = [
'password',
'remember_token'
];
public function addresses()
{
return $this->hasMany('App\CustomerAddress');
}
}
CustomerAddress-model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class CustomerAddress extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
CustomerAddress-controller
<?php
namespace App\Http\Controllers;
use App\CustomerAddress;
use Illuminate\Http\Request;
class CustomerAddressController extends Controller
{
public function create(Request $request)
{
$address = new CustomerAddress();
$address->address = $request['address'];
$request->user()->addresses()->save($address);
}
}
Error appears after this piece of code:
$request->user()->addresses()->save($address);
Any ideas? Thanks and cheers
In .config/cartalyst.sentinel.php, change 'model' => 'Cartalyst\Sentinel\Users\EloquentUser' to 'model' => 'App\User' to use your user model with the addresses relation defined
In ./app/User change User extends Authenticatable to User extends \Cartalyst\Sentinel\Users\EloquentUser to extend sentinel's user to your app's User model
Finally, your controller code should now be
`$address = new CustomerAddress();
$address->address = $request->input('address');
$request->user()->addresses()->save($address);`
and everything should be peachy