Laravel show data from key column not from id - php

I have this route:
Route::resource('articles', 'ArticlesController');
Route::get('articles/aukcija/{key}', 'ArticlesController#aukcija');
and I have this function in Controller:
public function show($id)
{
$article = Auth::user()->articles()->findOrFail($id);
return view('articles.show', compact('article'));
}
public function aukcija($key)
{
$article = Article::findOrFail($key);
return view('articles.show', compact('article'));
}
I need both of them... but how I can get Article with token stored in key column instead ID...
so when I write localhost:8888/article/1 and localhost:8888/article/aukcija/f4576ceusyfc674wr873cr48c7sefc to get the same article becouse article with ID=1 have key=f4576ceusyfc674wr873cr48c7sefc...

You could try this:
public function aukcija($key)
{
$article = Article::where('key', $key)->firstOrFail();
// or
$article = Article::where(compact('key'))->firstOrFail();
return view('articles.show', compact('article'));
}

Related

How to get ID from a relationship table, Laravel

I have 4 table : Users, CompanyRegister, VoucherDetails, Addvoucher.
So the Authenticate Users Id will be submit as user_id in companyRegister table,and then companyRegister ID will be submit as company_id in Voucherdetails table, and lastly voucherDetails Id will be submit in addVoucher table as voucher_ID. I am new to using eloquent and also laravel, I cant understand why I cant get the id from voucherdetails and submit in addvoucher but I can get id from companyregister and submit in company_id in voucherdetails. I'm using the same method to get id but not work, I hope can get solution and explanation here,Thank you in advance!!
My users model
public function companyregisters()
{
return $this->hasOne('App\companyregisters');
}
public function voucherdetails()
{
return $this->hasMany('App\voucherdetails');
}
public function addvoucher()
{
return $this->hasMany('App\addvoucher');
}
public function roles()
{
return $this->belongsToMany('App\role');
}
public function hasAnyRoles($roles)
{
if($this->roles()->whereIn('name', $roles)->first()){
return true;
}
return false;
}
public function hasRole($role)
{
if($this->roles()->where('name', $role)->first()){
return true;
}
return false;
}
my companyregister model
public function User(){
return $this->belongsTo('App\User');
}
public function voucherdetails()
{
return $this->hasMany('App\voucherdetails');
}
my voucherdetails model
public function User(){
return $this->belongsTo('User');
}
public function companyregisters(){
return $this->belongsTo('App\companyregisters');
}
public function addvoucher()
{
return $this->hasOne('App\addvoucher');
}
my addvoucher model
public function User(){
return $this->belongsTo('App\User');
}
public function voucherdetails(){
return $this->belongsTo('App\voucherdetails');
}
my voucherdetailsController
public function store(Request $request){
$voucherdetail = new voucherdetails();
$voucherdetail->title = $request->input('title');
$voucherdetail->description = $request->input('description');
$voucherdetail->user_id = Auth::user()->id;
$id = Auth::user()->id;
$user = User::find($id);
$company = $user->companyregisters;
$companyId = $company->id;
$voucherdetail->company_id = $companyId;
$voucherdetail->save();
return redirect()->to('addvoucher');
}
my addvoucherController
public function store(Request $request){
$addvoucher = new addvoucher();
$addvoucher->voucherTitle = $request->input('voucherTitle');
$addvoucher->voucherCode = $request->input('voucherCode');
$addvoucher->user_id = Auth::user()->id;
//Here(the voucherdetails id cant get to submit in voucher_id)
$id = Auth::user()->id;
$user = User::find($id);
$voucher = $user->voucherdetails;
$voucherID = $voucher->id;
$addvoucher->voucher_id = $voucherID;
$addvoucher->save();
return redirect()->to('displayVouchers');
}
This code works because companyregisters is a hasOne relationship for which the docs say:
Once the relationship is defined, we may retrieve the related record
using Eloquent's dynamic properties.
public function companyregisters()
{
return $this->hasOne('App\companyregisters');
}
$company = $user->companyregisters; // ie this returns the single related record
$companyId = $company->id; // and it has an `id` property, all good here
However, this code fails because voucherdetails is a hasMany relationship for which the docs say:
Once the relationship has been defined, we can access the "collection"
of comments by accessing the comments property.
More info on collections
public function voucherdetails()
{
return $this->hasMany('App\voucherdetails');
}
$voucher = $user->voucherdetails; // ie this returns a "collection" of related records
$voucherID = $voucher->id; // this "collection" does NOT have an id property, but each record IN the collection does.
In summary, either your relationship is defined incorrectly (hasMany vs hasOne) or, you'll need to loop over the related records to get the id from each.

How do you display list of admin with is_admin attribute in laravel?

How do you display all list of admin that have column with the is_admin=1? with this query? because we are not passing any data in showListAdmin.
public function Admin()
{
$users = User::all();
return view('admin.admins')->with('users',$users);
}
simply use where ....
public function showListAdmin()
{
$users = User::where('is_admin',true)->get()->all();
return view('admin.admins')->with('users',$users);
}

Laravel Join other tables in relation

I have a Situation Like This:
User Model:
public function role() {
return $this->hasOne('App\model\Roles' , 'id' ,'role');
}
public function userMetaData() {
return $this->hasOne('App\model\UserMetaData' , 'user_id' ,'id');
}
public function userBet() {
return $this->hasMany('App\model\UserBet' , 'user_id' , 'id');
}
public function userComission() {
return $this->hasMany('App\model\UserComission' , 'user_id' , 'id');
}
public function userPartnership() {
return $this->hasMany('App\model\UserPartneShip' , 'user_id' , 'id');
}
// Self Call
public function parentData() {
return $this->hasOne('App\User','id','parent_id');
}
Controller
$userData = User::with(['userMetaData','userBet','userComission','userPartnership','role','parentData'])
->where('id',$id)
->get();
Now The Point Is In role i am getting the roles Of the User and In the parentData i am getting the creator of the user(parent) from the same user table by self calling now that parent also has a role
My Question Is How can i get that role object inside the parentData Object?
Thanks!
First of all.. the relationship you've set is wrong it should be belongsTo
public function role() {
return $this->belongsTo('App\model\Roles' ,'role', 'id');
}
public function parentData() {
return $this->belongsTo('App\User','parent_id','id');
}
Now as you want role object inside the parentData set with as below.
$userData = User::with(['userMetaData','userBet','userComission','userPartnership','parentData.role'])
->where('id',$id)
->get();

many to many relation returns null in laravel

I have a many to many relation between the tables user and clinic and the third table is user_clinics. All three tables returns their values perfectly individually, but when i call App\User::find(1)->clinics or its inverse it returns null. Moreover, user_clinic has user_id and clinic_id and also previlage_id as a foreign key.
public function users() {
return $this->belongsToMany(User::class,'user_clinics','user_id','clinic_id');
}
public function clinics() {
return $this->belongsToMany(Clinic::class,'user_clinics','clinic_id','user_id');
}
public function adminDashboard(Request $request) {
$clinic = new Clinic();
$User_clinic = new User_clinic();
$user = new User();
$clinic->name = $request->name;
$clinic->address = $request->address;
if($request->hasFile('logo')) {
$fileName = $request->logo->getClientOriginalName();
$request->logo->storeAs('public/logos',$fileName);
$clinic->logo = $request->logo;
}
$clinic->save();
$User_clinic->user_id = auth::user()->id;
$test=$User_clinic->clinic_id = $clinic->id;
//now hardcoded previlage_id but deal with it in future...
$User_clinic->previlage_id = 1;
$User_clinic->save();
$test= $clinic::find(2)->users;
dd($test);
//return view("admin.dashboard.dashboardFirstPage");
}
Your relationship is not quiet right:
public function users() {
return $this->belongsToMany(User::class,'user_clinics','user_id','clinic_id');
}
public function clinics() {
return $this->belongsToMany(Clinic::class,'user_clinics','clinic_id','user_id');
}
It should be like below:
In User model:
public function clinics() {
return $this->belongsToMany(Clinic::class,'user_clinics','user_id','clinic_id');
}
In Clinic model:
public function users() {
return $this->belongsToMany(User::class,'user_clinics','clinic_id','user_id');
}

Laravel 4.1 eager loading

I'm having trouble on the eager loading.
Let's say I have models of Members, TrainingCategory, TrainingCategoryResult and Registration
Member Model:
public function registration() {
return $this->hasMany('Registration', 'member_id');
}
public function trainingResults(){
return $this->hasMany('trainingResult', 'member_id');
}
public function trainingCategoryResults() {
return $this->hasMany('TrainingCategoryResult', 'member_id');
}
TrainingCategory Model:
public function trainings() {
return $this->hasMany('Training', 'id');
}
public function trainingCategoryResults() {
return $this->hasMany('trainingCategoryResult', 'category_id');
}
TraningCategoryResult Model:
public function category() {
return $this->belongsTo('TrainingCategory', 'id');
}
public function member() {
return $this->belongsTo('Member', 'id');
}
Registration Model:
public function course() {
return $this->belongsTo('Course', 'course_id');
}
public function member() {
return $this->belongsTo('Member', 'id');
}
I am trying to eager load all the registration info and its related info including the TraningCategoryResult info but I not sure how to get that TraningCategoryResult which required two foreign keys (category_id and member_id), is there any way to do that?
Here is my code atm:
$members= Member::where(function($query) use ($id, $site) {
$query
->where('id', '=', $id)
->where('site', '=', $site);
});
$members= $members
->with('registration.course',
'registration.course.traningCategories',
->get(['member.id']);
Thank you.
This will not work Member::with('categoryResult')->with('registration')->get()
You can make a new relation in Member Model
public function categoryResult()
{
return $this->belongsTo('Category')->with('Registration');
}
//and then call
Member::with('categoryResult')->get();
You could use a few options to achieve that:
OPTION 1: create a variable relationship
Change your relation in the Member model
public function trainingCategoryResults($category_id = null) {
if(empty($category_id))
return $this->hasMany('TrainingCategoryResult', 'member_id');
else
return $this->hasMany('TrainingCategoryResult', 'member_id')
->where('category_id', $category_id);
}
The code above might have limitations and it doesn't take advantage of many laravel features, but it will work.
OPTION 2: Access from relationship
You can keep everything as is, and load as follow:
$members= Member::where(function($query) use ($id, $site) {
$query
->where('id', '=', $id)
->where('site', '=', $site);
})
->where('id', $member_id) // set the id of the memeber
->with(array(
'traningCategoryResults' => function($q)use($category_id){
$q->where('category_id', $category_id); // This makes sure you get only desired results
}
))
In this way you will have only what you need, assuming you know the $category_id

Categories