Connection for a user - laravel

I have for my table 'Student' 5 fields id, name, firstname, email, phone.
My question: I would like the user to connect by putting his name what should I change?
public function index(Request $request)
{
$user = $request->user();
$students= Student::query()
->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
$query->where('email', $user->email);
})
->when($request->has('search'), function (Builder $query) use ($request) {
$query->where('name', 'like', '%'.$request->input('search').'%');
})
->paginate(5);
return view('admin.students.index', compact('students'))
->with('display_search', $user->hasRole('admin'));
}
Here:
$query->where('email', $user->email);
I have to put this?
$query->where('name', $user->name);
Thank you for your help.

If you want to change the authentication credential from email to another column in the user's table, override the username function in the app/Http/Controllers/Auth/LoginController.php
/**
* Get the login username to be used by the controller.
*
* #return string
*/
public function username()
{
return 'name';
}

Related

Laravel Single User Data In A Multichained Distant Relationship

I have three models as per the code below.I have a problem of returning a single savings data for authenticated users. Any help will be highly appreciated.
I want a low like this:
$savings =Savings::client()->client_users()->where('user_id', '=', Auth::user()->id)->get();
Savings Model
class Savings extends Model
{
public function client()
{
return $this->hasOne(Client::class, 'id', 'client_id');
}
}
Client Model
class Client extends Model
{
public function client_users()
{
return $this->hasMany(ClientUser::class, 'client_id', 'id');
}
}
Client User Model
class ClientUser extends Model
{
public function user()
{
return $this->hasOne(User::class, 'id', 'user_id');
}
}
You can try this query:
$user = Auth::user();
return Savings::whereHas('client.client_users', function ($query) use ($user){
$query->where('user_id', $user->id);
}])->get();
I have fixed the above error. Thanks #Maik Lowrey
$user = Auth::user();
$savings= Savings::whereHas('client.client_users', function ($query) use ($user){
$query->where('user_id', $user->id);
})->get();
return response()->json($savings);
//return response($savings);

Hasrole with 2 tables

I have a model named Student with the field name , here is the function, which is correct.
public function index(Request $request)
{
$user = $request->user();
$students = Student::query()
->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
$query->where('email', $user->email);
})
->when($request->has('search'), function (Builder $query) use ($request) {
$query->where('name', 'like', '%'.$request->input('search').'%');
})
->paginate(5);
return view('admin.students.index', compact('students'))
->with('display_search', $user->hasRole('admin'));
}
Now, in my model Payment I have a request with 2 tables (Payment & Student).
Here is my function index()
public function index(Request $req)
{
if ($req->search == "") {
$payments = Payment::paginate(5);
return view('admin.payments.index', compact('payments'));
} else {
$validated = $req->validate([
'search' => 'alpha',
]);
$payments = payment::whereHas('students', function($query) use($req) {
$query->where('name', 'like', '%' . $req->search . '%');
})->paginate(5);
return view('admin.payments.index', compact('payments'));
}
}
My problem is I want to adapt my function for connect me with an user.
I have this for now ???
public function index(Request $request)
{
$user = $request->user();
$payments = Payment::query()
->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
$query->where('email', $user->email);
})
->when($request->has('search'), function (Builder $query) use ($request) {
how to include this:
$payments = payment :: whereHas ('students', function ($ query) use ($ req) {
$query-> where ('name', 'like', '%'. $ req-> search. '%');

Retrieve a feedback after connection of an user

When the user is connected and wishes to consult the feedback section, the user see each feedbacks for eachs users. I would like to know if it's possible to limit this?
For example, if the user is jeremy#gmail.com, Jeremy can see only his feedback.
Here is an idea of my code, I thank you in advance for your help.
public function index(Request $request)
{
$user = $request->user();
$feedbacks = Feedback::query()
->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
\Auth::user()->load('feedbacks');
$feedbacksForThisUser = \Auth::user()->feedbacks;
})
->when($request->has('search'), function (Builder $query) use ($request) {
$query->join('eleves', 'feedbacks.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->where('eleves.nom','like','%'.$request->input('search').'%');
})
->paginate(5);
return view('admin.feedbacks.index', compact('feedbacks'))
->with('display_search', $user->hasRole('admin'));
}
Edit
User Model
public function retours()
{
return $this->hasMany('App\Retour', 'user_id', 'id');
}
User Feedback
public function students(){
return $this->belongsTo('App\Student', 'fk_student');
}
public function feedbacks()
{
return $this->hasManyThrough(
'App\Feedback',
'App\Student',
'fk_seance',
'fk_student',
'id',
'id'
);
}
public function user()
{
return $this->belongsTo('App\User', 'id', 'user_id');
}
And
$table->increments('id');
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->string('instruction', 30);
$table->text('description', 80);
$table->integer('fk_student')->unsigned();
$table->foreign('fk_student')->references('id')->on('students');
Sure. When its a normal user, you can just use feedbacks = \Auth::user()->feedbacks;. This will limit to only the logged in user's feedbacks.
If you want to allow an admin to see all feedbacks, then check for admin, and then provide all. So, for just the user or admin (without the search code) something like this:
public function index(Request $request)
{
if(\Auth::user()->hasRole('admin')){
$feedbacks = Feedback::all();
}else{
\Auth::user()->load('feedbacks');
$feedbacks = \Auth::user()->feedbacks;
}
return view('admin.feedbacks.index', compact('feedbacks'));
}
You can add the search code into either of the if-blocks, depending on how you want to allow users to see the search. You can use when() on the query, but I'll demonstrate with just if to make it easier to understand:
public function index(Request $request)
{
if(\Auth::user()->hasRole('admin')){
if($request->has('search'))
$feedbacks = Feedback::orderBy('nom', 'asc')->where('nom','like','%'.$request->input('search').'%');
else
$feedbacks = Feedback::all();
}else{
\Auth::user()->load('feedbacks');
$feedbacks = \Auth::user()->feedbacks;
}
return view('admin.feedbacks.index', compact('feedbacks'));
}

Request in Laravel for my function index()

I have a problem with my function index() concerning a request. In fact, I have to adapt my old function with a recent.
For information, my function index() (old) below was written like this and it works !!
public function index(Request $req)
{
if ($req->search == "") {
$retours = Retour::join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->select('retours.*')->paginate(5);
return view('admin.retours.index', compact('retours'));
} else {
$validated = $req->validate([
'search' => 'alpha',
]);
$retours = Retour::join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->where('eleves.nom','like', '%' . $req->search . '%')->orderBy('eleves.nom', 'asc')->select('retours.*')->paginate(5);
return view('admin.retours.index', compact('retours'));
}
}
Now, I have to change a party of code and I must to adapt my request:
$retours = Retour::join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->where('eleves.nom','like', '%' . $req->search . '%')->orderBy('eleves.nom', 'asc')->select('retours.*')->paginate(5);
Here is an idea of my new code, my problem is that the search bar doesn't filter ? ^^
public function index(Request $request)
{
$user = $request->user();
$retours = Retour::query()
->when($user->hasRole('admin') !== true, function (Builder $query) use ($user) {
$query->where('email', $user->email);
})
->when($request->has('search'), function (Builder $query) use ($request) {
$query->join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->select('retours.*');
})
->paginate(5);
return view('admin.retours.index', compact('retours'))
->with('display_search', $user->hasRole('admin'));
}
My problem is perhaps here ?
->when($request->has('search'), function (Builder $query) use ($request) {
$query->join('eleves', 'retours.fk_eleve', '=', 'eleves.id')->orderBy('eleves.nom', 'asc')->select('retours.*');
})
->paginate(5);
Do have you an idea please?
Thank you

How to show Laravel model with belongsToMany connection if has specified connection?

I have this product model:
public function categories() {
return $this->belongsToMany('App\Category', 'product_categories');
}
And in my controller:
public function search(Request $request) {
return Product::with([
'categories' => function($categories) use ($request) {
// ...
}
]);
}
If I try to use $request in the categories function it's search only in categories, but shows all products.
How do I show only that products whitch has defined categories in $request->category_id?
you may use the whereHas keyword in laravel:
public function search(Request $request) {
return Product::with('categories')
->whereHas('categories', function ($query) use ($request){
$query->where('category_id', $request->category_id);
})->get();
}
Here is the docs
You can search it in following way:
public function search(Request $request) {
return Product::with('categories')
->whereHas('categories', function ($q) use (request) {
$q->where('id', $request->category_id);
});
}

Resources