View data according to class and class group - laravel

I had an admin panel where 3 types of user are there Admin, Teacher and Student. Now I want that when student logged in he/she only see data uploaded by admin
According to his/her class and class group like class=10th and group=computer science. I have no idea how can i get this type of thing. I had used following code
$paper = Paper::where('paper_type','PaperSolution' && 'class',Auth::user()->class)->get();
this is not working properly as I am dumping data
dd($paper);
it is giving me null as answer.

you can use more granular wheres passed as array:
$query->where([
['column_1', '=', 'value_1'],
['column_2', '<>', 'value_2'],
[COLUMN, OPERATOR, VALUE],
...
])
Try this
$paper = Paper::where([['paper_type','PaperSolution'],['class', Auth::user()->class]])->get();
dd($paper);
OR
$paper = Paper::where([['paper_type','=','PaperSolution'],['class','=', Auth::user()->class]])->get();
dd($paper);
Reference for where query using array
ReferenceLink

Please refer to Laravel collection docs to correct syntax.
$paper = Paper::where('paper_type','PaperSolution')
->where('class', Auth::user()->class)
->get();

I did not exactly understand structure of your DB, but at first you need to have corresponding columns to then write any query. Your first option is uploaded by admin so in your uploads table (which is Paper model as I can see from your text) you should have something like uploader_role. Then you have student_class and student_group options. I want to get your attention in fact that this 2 options are users table columns NOT uploads (Paper). So you need to check some permissions of users, then get papers uploaded by admins. You can do that with middleware or just in your controller
$user = Auth::user();
if ($user->class == 10 && $user->group == 'computer_science') {
$papers = Paper::where('uploader_role', 'admin')
// you can add extra where options if that columns exist in papers table and you need to filter them also by class and group
->where( 'class_that_has_permissions', $user->class)
->where( 'group_that_has_permissions', $user->group)
->get();
return $papers;
}
abort(404);
Also be careful with columns that have name class that can return real user class name like App\User, try to use class_name or class_number.

Related

Laravel Eloquent - Get a specific row and others

In my users table i want to get admin user and the others who are activated.
in fact the result that I want is concatenation of this two query result :
User::where('user_name','admin')->first();
User::where('is_active',1)->get();
is there any way to get this result in one query?
$query = User::where('is_active',1)->where('user_name','admin')->first();
Above Query will give you only one Active User whos user_name is admin and active.
As per my understanding from your question you want all users who are "active" and whos user_name is "admin" then try this below query.
$query = User::where('is_active',1)->orWhere('user_name','admin')->get();
Hope this may solve your problem.
You need to use a Union to put the two result sets together.
$first = User::where('user_name','admin')->first();
$users = User::where('is_active',1)->union($first)->get();
That should get you the first Admin user AND all the active users.
In fact, as they are on the same table, a OR should also work like below:
$users = User::where('is_active',1)
->orWhere('user_name', 'admin')
->get();
I'll leave both parts in as both will work.
Try this,
User::where('is_active',1)
->orWhere('user_name', 'admin')
->get();
You should try this:
$rsltUser = User::where('is_active',1)
->orWhere('user_name', 'admin')
->get();
Sorry for the late reply everybody missed the laravel out of box method which accepts array
Eloquent Version
User::where(['user_name' => 'admin','is_active' => '1'])->first();
Another Method
DB Facade Version
DB::table('users')->where(['user_name' => 'admin','is_active' => '1'])->first();
Hope its helps

Laravel firstOrCreate with advance functions

I am stuck at one situation where i have to insert record only if composite columns values are not exist with given user id; something like
UsersInvited::firstOrCreate([
'invited_by_id'=>$eventDetails->user_id,
function($query) use ($thisEmail,$thisPhone){
return $query->where('email',$thisEmail)->orWhere('phone',$thisPhone);
}
]);
If user_id =3 is inviter of event and some members are not registered in portal then they have to preserve in one table so that in future if invited user will install the app then i have to make it event member automatically.
Any other user can also invite the same members multiple times.
So in above case i wants to check that thisEmail = abc#gmail.com is invited by user_id = 3 or thisPhone=8945454545 is invited by same inviter then no need to make entry.
Is it possible in laravel - eloquent firsOrCreate ()?
Using the code to get the method firstOrCreate():
public function firstOrCreate(array $attributes, array $values = [])
Hence: you can not append extra query parameters to this method.
In your case, you should try and find the matching record yourself. If it doesnt exist, you can create one. For example:
$attributes = ['invited_by_id' => '..'];
$instance = Model::where($attributes)->where(..)->orWhere(..)->first();
if( is_null ( $instance ) ) {
$instance = Model::create($attributes);
}
Good luck!

Laravel Collection Filter By Multiple Concatenated Unique Fields

I have a model download which has two three properties. Those properties are 'id' , 'user_id' , 'file_id'.
Whenever a user downloads a file a download record is created.
I want to grab all downloads that have both a unique file_id and unique user_id.
How is this accomplished.
A collection might come from a relationship and needs filtering afterward. Below is a tested working solution in laravel 5.7 and most probably in later versions also:
$unique = $collection->unique(function ($item)
{
return $item['brand'] . $item['model'];
});
Here is an article about this: http://laravel.at.jeffsbox.eu/laravel-5-eloquent-collection-methods-unique
You can use the groupBy method.
$downloads = DB::table('downloads')
->groupBy('file_id')
->groupBy('user_id')
->get();
OR
You can use the distinct method.
$downloads = DB::table('downloads')->distinct()->get();

Laravel Many to Many - 3 models

Some help with many to many relationships in Laravel:
Using the example for roles and users - basically:
a table for all the roles
a table for the users
and table with user_id and role_id.
I want to add to the third table, eg Year. basically the pivot table will have user_id, role_id and year_id.
I want to be able to make a query to pull for example all users assigned a specific role in a specific year. Eg All users with role_id = 2, and year_id = 1.
Any help will be appreciated
Before answering, I would like to suggest you not to put year on database like this.
All your tables should have created_at and updated_at which should be enough for that.
To filter users like you want. You could do this:
// This queries all users that were assigned to 'admin' role within 2013.
User::join('role_users', 'role_users.user_id', '=', 'users.id')
->join('roles', 'roles.id', '=', 'role_users.role_id')
->where('roles.name', '=', 'admin')
->where(DB::raw('YEAR(role_users.created_at)', '=', '2013')
->get();
This example may not be the precise query you are looking for, but should be enough for you to come up with it.
The best way to achieve a three way relation with Eloquent is to create a model for the table representing this relation. Pivot tables is meant to be used for two way relations.
You could have then a table called roles_users_year which could have data related to this 3 way relation like a timestamp or whatever...
A very late answer to a very old question, but Laravel has supported additional intermediate (pivot) table columns of at least Laravel 5.1 judging from the documentation, which hasn't changed at least through Laravel 6.x.
You can describe these extra columns when defining your many-to-many relationship:
return $this->belongsToMany(Role::class)->withPivot('column1', 'column2');
or in your case, the below would also do the job:
return $this->belongsToMany(Role::class)->withTimestamps();
which you can then access via the pivot attribute on your model:
$user = User::find(1);
foreach ($user->roles as $role) {
echo $role->pivot->created_at;
}
Note that the pivot attribute is on the distant relationship model (a single Role) and not on the relationship itself.
To get all the Roles assigned to Users in any given year, you might create a special relationship:
// User.php
public function rolesInYear($year) {
return $this->belongsToMany(Role::class)
->wherePivot('created_at', '>=', Carbon::create($year))
->wherePivot('created_at', '<', Carbon::create($year + 1));
}

Laravel Eloquent and combing data to send to View

I'm trying to figure out a way to combine data from two tables into a single variable that I can then send to my view. I'm using Authority as my authentication bundle and so I have the following tables set up: users, roles, role_user. I want to get the following data into a single variable.
From the users table:
id, name, email
From the roles table:
name
The following returns all the user data in the users table:
$users = User::all();
But, I want to make a chain that can get the related data stored in the roles table? I want all users and all of each users roles together.
And while I can find examples that help get related data for a single record I haven't been able to find any reference to retrieving entire tables with related data.
Thanks.
$users = User::with('roles')->get()
Eager loading is what you are looking for, read the docs :)
Im not 100% sure what you are after, I think you should make relations with Eloquents methods. But if you are looking for a way to just merge the data returned by two models you could do the following:
$users = User::all();
$roles = Roles::all();
$array = array_merge($users->toArray(), $roles->toArray());
return Response::json($array);
You could make the model calls so that they return exactly what you are after and then merge them together. If you want one Model to return something specific just make a function for it.
You need to use something like that:
$user = User::get();
$roles = Roles::get();
$user .= (object) $roles; //because of that both them are object array and we have to combine them as object array...
than you can pass the data to View
return View:make('templatefile')//or Redirect to_route whatever
->with('user', $user);
But the best way is belongs_to in your case. Please read the documentation:
http://laravel.com/docs/database/eloquent#relationships

Resources